'SSHClient' object has no attribute 'known_hosts' on ssh.connect() - python

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.

Related

Most recent SFTP python package and best practices

I've been looking for SFTP python packages, ftpretty works fine for me:
https://pypi.org/project/ftpretty/
but I want to use a more secure protocol.
PySftp is obviously a bit outdated (Edit: it seems that pysftp is still frequently used, about the error please see below):
https://bitbucket.org/dundeemt/pysftp/src/master/
And throws me several errors on Win10, PyCharm, Python3.6:
C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py:61: UserWarning: Failed to load HostKeys from C:\Users\bobin\.ssh\known_hosts. You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
warnings.warn(wmsg, UserWarning)
Traceback (most recent call last):
File "C:/Users/bobin/PycharmProjects/classtest/pysftptest.py", line 7, in <module>
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword) as sftp:
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py", line 132, in __init__
self._tconnect['hostkey'] = self._cnopts.get_hostkey(host)
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py", line 71, in get_hostkey
raise SSHException("No hostkey for host %s found." % host)
paramiko.ssh_exception.SSHException: No hostkey for host s233.goserver.host found.
Exception ignored in: <bound method Connection.__del__ of <pysftp.Connection object at 0x00000235B0695048>>
Traceback (most recent call last):
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py", line 1013, in __del__
self.close()
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py", line 784, in close
if self._sftp_live:
AttributeError: 'Connection' object has no attribute '_sftp_live'
Process finished with exit code 1
This thread seemed relevant to me but it's already 12years old:
SFTP in Python? (platform independent)
And the paramiko package is also throwing me errors:
Traceback (most recent call last):
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp_client.py", line 130, in __init__
server_version = self._send_version()
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp.py", line 134, in _send_version
t, data = self._read_packet()
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp.py", line 201, in _read_packet
x = self._read_all(4)
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp.py", line 188, in _read_all
raise EOFError()
EOFError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/bobin/PycharmProjects/classtest/paramikotest.py", line 12, in <module>
sftp = paramiko.SFTPClient.from_transport(transport)
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp_client.py", line 170, in from_transport
return cls(chan)
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\paramiko\sftp_client.py", line 132, in __init__
raise SSHException("EOF during negotiation")
paramiko.ssh_exception.SSHException: EOF during negotiation
Process finished with exit code 1
I have come so far to understand that I probably need a keyfile that I can find out by first connecting to my webspace using e.g. filezilla:
How To Extract SFTP SSH Key From Key Cache in FileZilla FTP Client
My question is: how do I establish an SFTP connection to my host, which is webgo: https://www.webgo.de/hilfe/content/76/52/de/was-ist-sftp.html
EDIT: providing no host_key as follows:
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
myHostname = "host"
myUsername = "user"
myPassword = "pass"
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts, port=22) as sftp:
print("Connection succesfully stablished ... ")
sftp.put('C:\TEMP\Capture.PNG', preserve_mtime=True)
still throws me an error for providing no host_keys:
C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\pysftp\__init__.py:61: UserWarning: Failed to load HostKeys from C:\Users\bobin\.ssh\known_hosts. You will need to explicitly load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None).
warnings.warn(wmsg, UserWarning)
EDIT2: tried prettyftp but my provider is refusing the connection:
Traceback (most recent call last):
File "C:/Users/bobin/PycharmProjects/classtest/testftp.py", line 15, in <module>
f.put('C:\TEMP\Capture.PNG', 'Capture230.PNG')
File "C:\Users\bobin\PycharmProjects\classtest\venv\lib\site-packages\ftpretty.py", line 119, in put
self.conn.storbinary('STOR %s' % remote_file, local_file)
File "C:\Users\bobin\AppData\Local\Programs\Python\Python36\lib\ftplib.py", line 513, in storbinary
return self.voidresp()
File "C:\Users\bobin\AppData\Local\Programs\Python\Python36\lib\ftplib.py", line 249, in voidresp
resp = self.getresp()
File "C:\Users\bobin\AppData\Local\Programs\Python\Python36\lib\ftplib.py", line 242, in getresp
raise error_temp(resp)
ftplib.error_temp: 425 Unable to build data connection: Operation not permitted
Used following code snippet, setting secure=False worked again:
from ftpretty import ftpretty
# Minimal
f = ftpretty('host','user','pass', port=21, secure=True)
f.put('C:\TEMP\Capture.PNG', 'Capture230.PNG')
f.close()
For the first error, it seems like a bug in pysftp.
You can have a look at the Connection class here on line 76, and the attribute _sftp_live is defined on line 134, so this is definitely an error occurring at runtime without being validated correctly. I have also been able to find this related error, which likely explains the cause of this issue; the solution is also mentioned in the error if you want to explicitly fix it.
I would still consider using ftpretty. It does use TLS for security and a pretty safe wrapper, you can simply enable it by setting the secure parameter to True (secure=True) - which by default is set as False.

ciscoaxl api python errors

I am starting out working on looking into using Python for some Cisco CUCM automation. I found the plugin ciscoaxl here, I installed it and programed the following script:
from ciscoaxl import axl
cucm = "10.10.20.1"
username = "axlusr"
password = "password1"
version = "12.5"
ucm = axl(username, password, cucm, version)
for phone in ucm.get_phones():
print(phone.name)
I am connected to Cisco's DevNET Sandbox and all the login and configuration for the AXL user appear to be correct, however I get the following output when I attempt to run the script:
Traceback (most recent call last):
File "%home%\AppData\Local\Programs\Python\Python39\axl-test.py", line 7, in <module>
for phone in ucm.get_phones():
File "%home%\AppData\Local\Programs\Python\Python39\lib\site-packages\ciscoaxl\axl.py", line 1877, in get_phones
for each in inner(skip):
File "%home%\AppData\Local\Programs\Python\Python39\lib\site-packages\ciscoaxl\axl.py", line 1869, in inner
res = self.client.listPhone(
File "%home%\AppData\Local\Programs\Python\Python39\lib\site-packages\zeep\proxy.py", line 40, in __call__
return self._proxy._binding.send(
File "%home%\AppData\Local\Programs\Python\Python39\lib\site-packages\zeep\wsdl\bindings\soap.py", line 130, in send
return self.process_reply(client, operation_obj, response)
File "%home%\AppData\Local\Programs\Python\Python39\lib\site-packages\zeep\wsdl\bindings\soap.py", line 195, in process_reply
return self.process_error(doc, operation)
File "%home%\AppData\Local\Programs\Python\Python39\lib\site-packages\zeep\wsdl\bindings\soap.py", line 283, in process_error
raise Fault(
zeep.exceptions.Fault: Unknown fault occured
I have run it on Windows10 in an IDLE enviornment, from the Linux-Subsystem (Ubuntu 20.04) via python and ipython3.
After some additional research this is a know issue with CUCM 12.5. It should be fixed in CU1 see here: https://github.com/mvantellingen/python-zeep/issues/989
I still receive this error on 12.5.1.12900-115, but I receive it when I don't have the appropriate permissions. Fixing user permissions for AXL access resolves it.

getting error while ssh using python

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.

Unable to connect to remote host using paramiko?

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')

paramiko: NameError: global name 'descriptor' is not defined

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)

Categories

Resources