感谢阅读。
使用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.
谢谢
答案 0 :(得分:1)
hostname = 'MyUser@macbook-air-de-louis.home'
username = 'MyUserName'
...
socket.gaierror: [Errno 11003] getaddrinfo failed
您使用的主机名MyUser@macbook-air-de-louis.home
无效。您的脚本试图将您的主机名解析为一个地址,但失败。看来您已经给它加上了用户名,这是不正确的。
尝试使用“ macbook-air-de-louis.home”作为主机名,而不使用“ MyUser @”部分。如果仍然无法解决问题,请尝试使用远程系统的IP地址。