I want to transfer files between two Ubuntu Servers using scp, i have tested scp between the two systems and it worked perfectly fine.So i dont want to execute the command everytime i need to get files so i want to write a python script which automatically downloads files from other host using scp.
While searching online i found this Paramiko module and i have trouble installing this and i have rectified this using module cryptography.Now the real trouble is explained with the terminal below.
>>> from paramiko import SSHClient
>>> from scp import SCPClient
>>> ssh = SSHClient()
>>> ssh
<paramiko.client.SSHClient object at 0x1a41c90>
>>> ssh.load_system_host_keys()
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect('somename#192.168.100.100')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 296, in c onnect
to_try = list(self._families_and_addresses(hostname, port))
File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 200, in _ families_and_addresses
addrinfos = socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_S TREAM)
socket.gaierror: [Errno -2] Name or service not known
>>> ssh.connect('192.168.100.100')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 361, in c onnect
server_key)
File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 672, in m issing_host_key
raise SSHException('Server %r not found in known_hosts' % hostname)
paramiko.ssh_exception.SSHException: Server '192.168.100.100' not found in known_hos ts
I have changed the ip and username for safe use somename is replaced but i have tried with original username.So i tried this several times but i still getting the same error.
Any suggestions on this problem?Please Help.
Maybe you are missing the missing_host_key_policy
What about this one:
proxy = None
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host['hostname'], username=host['user'], sock=proxy)
more examples here: www.programcreek.com
For me the solution was:
client = SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(AutoAddPolicy())
client.connect(host, username=user,password=password)
Try using this:
ssh.connect('host', username='username',password='password')
You can also add your public key to known hosts in server, if you wish to skip password and connect without giving your password.
In that case use:
ssh.connect('host', username='username')
Related
I am trying to sync two folders via FTP, yes I know there are better or different ways but for now I need to implement it this way, I was trying the example code from pyftpsync since well, a sample code should work easily right? I am just trying to connect between some test folders I made, one is empty(local) and the remote has a single text file that I want to fetch. It tries to connect but after about 2 minutes I get this error.
Well, my FTP does work outside of python. I can connect over WinSCP just fine.
Some places mentioned that a proxy could possibly cause this, but it seems I am not behind a proxy currently, but maybe I did not set that properly and it believes there should be a proxy somehow?
Here is my code, just using commands on the prompt for pyftpsync produces the same errors for me. So it is possible some input parameter is off causing all of this.
import time
import os
import re
import shutil
import string
import sys
from ftpsync.targets import FsTarget
from ftpsync.ftp_target import FtpTarget
from ftpsync.synchronizers import DownloadSynchronizer
#synchronize a local folder with ftp
local = FsTarget( "C:\\testfolder\\" )
user = "login"
passwd = "password"
remote = FtpTarget("/my/folder/location/testfold/", "126.0.0.1",port=22, username=user,password=passwd,tls=False,timeout=None,extra_opts=None)
opts = {}
s=DownloadSynchronizer(local, remote, opts)
s.run()
This is the output I am getting, I have edited out the folder names and IP addresses.
INFO:keyring.backend:Loading KWallet
INFO:keyring.backend:Loading SecretService
INFO:keyring.backend:Loading Windows
INFO:keyring.backend:Loading chainer
INFO:keyring.backend:Loading macOS
INFO:pyftpsync:Download to C:\testfolder
from ftp://126.0.0.1/.../testfold
INFO:pyftpsync:Encoding local: utf-8, remote: utf-8
Traceback (most recent call last):
File "c:\..\.py", line 30, in <module>
s.run()
File "C:\\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\ftpsync\synchronizers.py", line 1268, in run
res = super(DownloadSynchronizer, self).run()
File "C:\\AppData\Local\Programs\Python\Python37-
32\lib\site-packages\ftpsync\synchronizers.py", line 827, in run
res = super(BiDirSynchronizer, self).run()
File "C:\\AppData\Local\Programs\Python\Python37-
32\lib\site-packages\ftpsync\synchronizers.py", line 211, in run
self.remote.open()
File "C:\\AppData\Local\Programs\Python\Python37-
32\lib\site-packages\ftpsync\ftp_target.py", line 141, in open
self.ftp.connect(self.host, self.port)
File "C:\\AppData\Local\Programs\Python\Python37-
32\lib\ftplib.py", line 155, in connect
self.welcome = self.getresp()
File "C:\\Local\Programs\Python\Python37-
32\lib\ftplib.py", line 236, in getresp
resp = self.getmultiline()
File "C:\\AppData\Local\Programs\Python\Python37-
32\lib\ftplib.py", line 226, in getmultiline
nextline = self.getline()
File "C:\\AppData\Local\Programs\Python\Python37-
32\lib\ftplib.py", line 210, in getline
raise EOFError
EOFError
Anyways any possible troubleshooting ideas would help. Thank you.
Pyftpsync uses FTP protocol.
You are connecting to port 22, which is used for SSH/SFTP.
So if your server is actually SFTP server, not FTP server, you cannot use Pyftpsync with it.
Not able to take ssh of a device using python getting below error.
Tried reinstalling python paramiko but didnt worked
import paramiko
import sys
import time
paramiko.client.SSHClient()
HOST = "192.168.1.11"
USER = "cisco"
PASS = "cisco"
client1=paramiko.SSHClient()
client1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client1.connect(HOST,username=USER,password=PASS)
print "SSH connection to %s established" %HOST
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/testssh.py", line 1, in
import paramiko
File "C:\Python27\lib\site-packages\paramiko__init__.py", line 31, in
from paramiko.client import SSHClient, MissingHostKeyPolicy, AutoAddPolicy, RejectPolicy, WarningPolicy
File "C:\Python27\lib\site-packages\paramiko\client.py", line 24, in
import getpass
File "C:/Users/Administrator/Desktop\getpass.py", line 11, in
remote_conn_pre=paramiko.SSHClient()
AttributeError: 'module' object has no attribute 'SSHClient'
Change this
client1=paramiko.SSHClient()
to this
client1=paramiko.client.SSHClient()
Your best clue is the last line of the stack trace:
line 11, in remote_conn_pre=paramiko.SSHClient()
AttributeError: 'module' object has no attribute 'SSHClient'
(Added some whitespace for clarity)
Here it is saying that on this line, it can't find the property SSHClient in the paramiko class.
I would say to check the paramiko documentation, but you can see that you have successfully called this function on line 4:
paramiko.client.SSHClient()
Which will probably also work on line 11 - as other answers have pointed out, you need to access it through paramiko.client, not just paramiko. You probably also don't need line 4.
I am trying to connect ssh server using code below
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts"))
ssh.connect("10.1.3.2", username = "root")
but I get the following error message
Exception during testMethod !!
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/Qb/QbUnittest/QbUnittest.py", line 624, in __call__
testMethod()
File "testDropBear_send_file.py", line 31, in runMe
ssh.connect("10.1.3.2", username = "root")
File "/usr/local/lib/python2.7/dist-packages/paramiko-1.12.0-py2.7.egg/paramiko/client.py", line 326, in connect
self._policy.missing_host_key(self, server_hostkey_name, server_key)
File "/usr/local/lib/python2.7/dist-packages/paramiko-1.12.0-py2.7.egg/paramiko/client.py", line 71, in missing_host_key
client.save_host_keys(client._host_keys_filename)
File "/usr/local/lib/python2.7/dist-packages/paramiko-1.12.0-py2.7.egg/paramiko/client.py", line 192, in save_host_keys
if self.known_hosts is not None:
AttributeError: 'SSHClient' object has no attribute 'known_hosts'
Can someone help me to understand what is the problem?
This is a known bug and the fix was released to version 1.12.1; you seem to be using 1.12.0. Try upgrading to the latest release:
pip install --upgrade paramiko
A quick fix:
ssh = paramiko.SSHClient()
ssh.known_hosts = None
...
...
The bug was fixed here.
I'm trying to use paramiko for SSH, but got an error:
>>> import paramiko
>>> ssh = paramiko.SSHClient()
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect('54.***.***.110', key_filename='D:\Keys\MyOWN\priv.ppk')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build\bdist.win32\egg\paramiko\client.py", line 366, in connect
File "build\bdist.win32\egg\paramiko\client.py", line 515, in _auth
File "build\bdist.win32\egg\paramiko\agent.py", line 343, in __init__
File "build\bdist.win32\egg\paramiko\agent.py", line 66, in _connect
File "build\bdist.win32\egg\paramiko\agent.py", line 83, in _send_message
File "build\bdist.win32\egg\paramiko\win_pageant.py", line 123, in send
File "build\bdist.win32\egg\paramiko\win_pageant.py", line 89, in _query_pageant
File "build\bdist.win32\egg\paramiko\_winapi.py", line 273, in get_security_attributes_for_user
File "build\bdist.win32\egg\paramiko\_winapi.py", line 222, in descriptor
NameError: global name 'descriptor' is not defined
Regarding this issue - it was solved, but - I still have this error (latest paramiko version, downloaded from it's Github).
May be - there is some other libs, to wok via SSH with RSA-key authorization?
Or - any way to solve this NameError...
Seems like the issue is not really solved (I too downloaded latest zip: it can also be seen on [GitHub]: paramiko/paramiko - (v1.15.2) paramiko/paramiko/_winapi.py), so you'll have to fix it yourself in your paramiko installation files (fixed in v1.15.3):
Edit your ${PYTHON_DIR}\build\bdist.win32\egg\paramiko\_winapi.py (${PYTHON_DIR} is just a placeholder for your Python installation directory),
and at lines 222 and 223 simply replace descriptor by value:
self._descriptor = descriptor
self.lpSecurityDescriptor = ctypes.addressof(descriptor)
should become:
self._descriptor = value
self.lpSecurityDescriptor = ctypes.addressof(value)
I used to get this type of errors. I restarted the machine and it got solved!
But I think there is a bug in the paramiko library.
Changing descriptor by value as explained by CristiFati works fine.
I met the issue too, please try to set the allow_agent=False, and it should be resolved.
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('54.***.***.110', key_filename='D:\Keys\MyOWN\priv.ppk', allow_agent=False)
I'm trying to upload a simple file from my local host (Windows) to a remote machine (UNIX) using Python 3.3
Here's the code:
import os
import Crypto
import paramiko
import pysftp
localpath = "C:\\py.txt"
remotepath = "/tmp/py.txt"
s = pysftp.Connection(host='10.1.1.1', username='user', password='pass')
s.put(localpath, remotepath)
The error which returns is:
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
s.put(localpath, remotepath)
File "C:\Python33\lib\site-packages\pysftp.py", line 349, in put
confirm=confirm)
File "C:\Python33\lib\site-packages\paramiko-1.14.0- py3.3.egg\paramiko\sftp_client.py", line 585, in put
file_size = os.stat(localpath).st_size
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C://py.txt'
I've tried different prefix for localpath, like 'C:\py.txt' but I get the same results.
Thanks in advance
Python uses backslash to quote characters, like \n = newline and \t = tab; thus \\\\ = one slash.
Use an r prefix to make a raw string literal:
localpath = r"C:\\py.txt"
I've found the problem. The problem was with the file location, as it's name was 'py.txt' but my windows didn't show file extensions, so the actual file name was py.txt.txt