【发布时间】:2018-08-20 02:34:56
【问题描述】:
感谢您阅读本文。
对象
使用 Python3,我想将 SSH 命令发送到我的本地 Mac osx 计算机。经过研究,我发现 Paramiko 可以帮助我,但我对其他图书馆持开放态度
设置
两台计算机(我的 windows 计算机和目标 mac osx 计算机)都有彼此知道的私钥/公钥,所以要恢复,我可以手动 ssh 没有密码并且它可以工作。 我只使用 python 3 来做这些操作。
尝试
您可以在下面找到我的以下代码:
import paramiko
hostname = 'MyUser@macbook-air-de-louis.home'
port = 22
username = 'MyUserName'
pkey_file = 'C:/Users/MyUserName/.ssh/id_rsa'
if __name__ == "__main__":
key = paramiko.RSAKey.from_private_key_file(pkey_file)
s = paramiko.SSHClient()
s.load_system_host_keys()
s.connect(hostname, port, pkey=key)
stdin, stdout, stderr = s.exec_command('ifconfig')
print(stdout.read())
s.close()
结果错误
以下错误如下:
Traceback (most recent call last):
File "jr.py", line 23, in <module>
s.connect(hostname, port, pkey=key)
File "D:\Documents\ana\lib\site-packages\paramiko\client.py", line 329, in connect
to_try = list(self._families_and_addresses(hostname, port))
File "D:\Documents\ana\lib\site-packages\paramiko\client.py", line 200, in _families_and_addresses
hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM)
File "D:\Documents\ana\lib\socket.py", line 745, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11003] getaddrinfo failed
编辑
特别感谢@Kenster 我更改了 IP 地址,但出现以下错误, 你知道我想念什么吗?
s.connect(hostname, port, pkey=key)
File "D:\Documents\ana\lib\site-packages\paramiko\client.py", line 424, in connect
passphrase,
File "D:\Documents\ana\lib\site-packages\paramiko\client.py", line 714, in _auth
raise saved_exception
File "D:\Documents\ana\lib\site-packages\paramiko\client.py", line 691, in _auth
self._transport.auth_publickey(username, key))
File "D:\Documents\ana\lib\site-packages\paramiko\transport.py", line 1450, in auth_publickey
return self.auth_handler.wait_for_response(my_event)
File "D:\Documents\ana\lib\site-packages\paramiko\auth_handler.py", line 226, in wait_for_response
raise e
paramiko.ssh_exception.AuthenticationException: Authentication failed.
谢谢,
【问题讨论】: