如何使用 python 从一个系统 SSH 到另一个系统
How to SSH from one system to another using python
我正在尝试使用 python
中的 paramiko 从一个系统执行 SSH 到另一个系统
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
ssh.connect('127.0.0.1', username='jesse',
password='lol')
使用此引用 (http://jessenoller.com/blog/2009/02/05/ssh-programming-with-paramiko-completely-different )
当我们知道你要登录的系统的密码时就是这样但是
如果我想登录我的 public-key 被复制但我不知道密码的系统。有没有办法做到这一点
提前致谢
SSHClient.connect
接受 kwarg key_filename
,它是本地私钥文件(或文件,如果给定路径列表)的路径。见 docs.
key_filename (str) – the filename, or list of filenames, of optional private key(s) to try for authentication
用法:
ssh.connect('<hostname>', username='<username>', key_filename='<path/to/openssh-private-key-file>')
将密钥添加到已配置的 SSH 代理将使 paramiko 自动使用它而无需更改您的代码。
ssh-add <your private key>
您的代码将按原样运行。或者,可以使用
以编程方式提供私钥
key = paramiko.RSAKey.from_private_key_file(<filename>)
SSHClient.connect(pkey=key)
此代码应该有效:
import paramiko
host = "<your-host>"
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, username='<your-username>', key_filename="/path/to/.ssh/id_rsa" , port=22)
# Just to test a command
stdin, stdout, stderr = client.exec_command('ls')
for line in stdout.readlines():
print line
client.close()
Here is the documentation of SSHClient.connect()
编辑:/path/to/.ssh/id_rsa
是您的私钥!
我正在尝试使用 python
中的 paramiko 从一个系统执行 SSH 到另一个系统import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
ssh.connect('127.0.0.1', username='jesse',
password='lol')
使用此引用 (http://jessenoller.com/blog/2009/02/05/ssh-programming-with-paramiko-completely-different )
当我们知道你要登录的系统的密码时就是这样但是 如果我想登录我的 public-key 被复制但我不知道密码的系统。有没有办法做到这一点
提前致谢
SSHClient.connect
接受 kwarg key_filename
,它是本地私钥文件(或文件,如果给定路径列表)的路径。见 docs.
key_filename (str) – the filename, or list of filenames, of optional private key(s) to try for authentication
用法:
ssh.connect('<hostname>', username='<username>', key_filename='<path/to/openssh-private-key-file>')
将密钥添加到已配置的 SSH 代理将使 paramiko 自动使用它而无需更改您的代码。
ssh-add <your private key>
您的代码将按原样运行。或者,可以使用
以编程方式提供私钥key = paramiko.RSAKey.from_private_key_file(<filename>)
SSHClient.connect(pkey=key)
此代码应该有效:
import paramiko
host = "<your-host>"
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, username='<your-username>', key_filename="/path/to/.ssh/id_rsa" , port=22)
# Just to test a command
stdin, stdout, stderr = client.exec_command('ls')
for line in stdout.readlines():
print line
client.close()
Here is the documentation of SSHClient.connect()
编辑:/path/to/.ssh/id_rsa
是您的私钥!