In WinSCP is an option to edit the SFTP server command/path (in the protocol options):
Is there also such an option in pysftp/Paramiko or in an another SFTP package for Python?
Thanks!
What that option in WinSCP does is that it runs SFTP over the "exec" channel, instead of the "sftp subsystem" channel. An (untested) equivalent in Python Paramiko:
ssh = paramiko.SSHClient()
# authenticate here
chan = ssh.get_transport().open_session()
chan.exec_command("/path/to/sftp-server")
sftp = paramiko.SFTPClient(chan)
For my SSH connections, I use this ~/.ssh/config:
Host gwhost
Hostname gw.hostname.com
User user
IdentityFile /home/user/.ssh/priv_key
ControlMaster auto
ControlPath ~/.ssh/%h-%p-%r.sock
ControlPersist 120
Host *.my-example.com
User user
IdentityFile /home/user/.ssh/priv_key
StrictHostKeyChecking no
ProxyCommand ssh -q 'gwhost' -W %h:22
From the terminal I can connect to the host like this:
ssh one.my-example.com
I want to execute some commands on a remote host using Paramiko.
I tried to do it like this:
host = 'one.my-example.com'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
user_config_file = os.path.expanduser("~/.ssh/config")
config = SSHConfig.from_path(user_config_file)
ssh.connect(hostname=host)
stdin, stdout, stderr = ssh.exec_command('ls')
lines = stdout.readlines()
print(lines)
After starting I got this error
in <lambda>
retry_on_signal(lambda: sock.connect(addr))
TimeoutError: [Errno 110] Connection timed out
So how can I use ~/.ssh/config or maybe I shouldn't ~/.ssh/config?
Paramiko has only very limited support for OpenSSH ssh_config configuration file.
If definitely won't use ssh_config automatically, as OpenSSH ssh does.
You would have to instantiate SSHConfig class using SSHConfig.from_path. And then use SSHConfig.lookup to lookup configuration for your hostname. And then use the returned dictionary to feed the arguments of SSHClient.connect.
Obligatory warning: Do not use AutoAddPolicy – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".
I have some problems with Python and Paramiko. I want to connect to a SSH Server to automatically read some information from different systems.
I tried some Tips I found here but nothing works. I checked the hosts file, reinstall Python using brew, update/upgrade Python and Paramiko, used the ssh-server on my localhost to test. Also running pip to update paramiko and python.
I am very confused. Please let me ask you.
This is my working environment:
MacOS Sierra 10.12.6
Python 2.7
Eclipse
SSH Library: Paramiko (paramiko-2.2.1-py2.py3-none-any.whl)
The Script: Basic Paramiko SSH Connection
'''
Created on 16.09.2017
'''
import sys
import telnetlib
import paramiko
host = '213.000.000.123' #
user = "user"
password = "password"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, user, password)
Running the Script on Eclipse results with this output:
Traceback (most recent call last):
File "/Users/tschaefer/Documents/workspace/PythonCMTSLibs/basicSSHSessionModule.py", line 17, in <module>
ssh.connect(host, user, password)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/paramiko/client.py", line 301, in connect
to_try = list(self._families_and_addresses(hostname, port))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/paramiko/client.py", line 199, in _families_and_addresses
hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM)
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
Ok, I find out gai=getaddrinfo, also getaddrbyname and so on. So I think there is a problem resolving the host-string to an IP Address. But it doesn't make sense resolving an IP Address to an IP Address. I used explicit IP.
Using the "ssh 213.000.000.123" command directly on CLI works fine.
I also tried to connect to "localhost" or "127.0.0.1" or "google.com" or other. The hosts-file-entry is not commented so it's active. Result: the same situation.
NSLOOKUP works correct, so the DNS Lookup works and a DNS server is available.
It seems Python could find the paramiko Library starting in Eclipse, but there is a problem in the Library.
Can anyone help me the see what my eye doesn't see.
Thank a lot!
Greetings
The error message is very misleading. It actually has nothing to do with DNS error.
Apparently the default port that paramiko uses is something other than 22. Once I specified a port, the problem goes away.
port = 22
k = paramiko.RSAKey.from_private_key_file(key_filename)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
hostname = os.environ['DOCKER_EE_MANAGER_NODE']
username = os.environ['UCP_USER']
ssh.connect(hostname, port, username, pkey = k)
Works! Enjoy!
In the Paramiko documentation you can see that the second positional argument to connect is port
connect(hostname, port=22, username=None, password=None, pkey=None, key_filename=None, timeout=None, allow_agent=True, look_for_keys=True, compress=False, sock=None, gss_auth=False, gss_kex=False, gss_deleg_creds=True, gss_host=None, banner_timeout=None, auth_timeout=None, gss_trust_dns=True, passphrase=None, disabled_algorithms=None)
You are passing user as the second argument, but connect tries to interpret it as port causing the connection to fail. Here is how you can fix the call to connect using keyword arguments.
ssh.connect(host, username=user, password=password)
I am using paramiko to establish an SFTP connection with a public/private key exchange. They key is an SSH2 RSA key. When I try to connect I'm receiving the error BadAuthenticationType: Bad authentication type (allowed_types=['']). Does anyone have an idea what might be causing this?
key = paramiko.RSAKey.from_private_key_file(key, password=passphrase)
transport = paramiko.Transport((host, port))
transport.start_client()
transport.auth_publickey(username, key)
sftp = paramiko.SFTPClient.from_transport(transport)
According to the documentation for Paramiko, the server you're trying to connect to isn't configured properly (it doesn't allow public-key authentication for the user you're using to connect). Here is a link to the portion of the documentation that I referenced, hopefully it will be of use. http://www.lag.net/paramiko/docs/paramiko.Transport-class.html#auth_publickey
I recommend that you check your server config and make sure everything is set up properly.
I am trying to copy files from remote server to my local directory.
I am using Python paramiko's sftp get to copy the files.
sftp.get(remote_pate, local_path)
After copying very few files, I get the following exception.
SSHException('Server connection dropped')
Establishing connection using,
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=username, password=password)
sftp = ssh.open_sftp()
Why does the connection get dropped? How can I handle this? Thanks in advance.
This would arise (as mentioned in the source, line no 667, http://www.lag.net/paramiko/docs/paramiko.sftp_client-pysrc.html) when there is any error reading the packet or when there is EOFError