我有四个带有raspbian的树莓味。现在我需要在每个主机上执行相同的shell命令。我无法使用cron自动执行作业,因为每次我需要它时它们都是不同的。 所以我想我可以写一个python脚本为我做这个:
hosts = ["host1", "host2", "host3", "host4"]
usrs = ["usr1", "usr2", "usr3", "usr4"]
passwords = ["pass1", "pass2", "pass3", "pass4"]
def do_ssh_stuff(host, usr, pwd):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(hostname=host, username=usr, password=pwd)
# Do the stuff on the remote machine
except paramiko.ssh_exception.AuthenticationException:
print("Failed on machine: " + host)
i = 0
for host in hosts:
do_ssh_stuff(host, usr[i], passwords[i])
i += 1
i = 0
事情有时似乎有效,有时我得到这个错误信息:
Exception: Error reading SSH protocol banner Traceback (most recent call last):
File "C:\Users\usr\AppData\Local\Programs\Python\Python36-32\lib\site-packages\paramiko\transport.py", line 1893, in _check_banner
buf = self.packetizer.readline(timeout)
File "C:\Users\usr\AppData\Local\Programs\Python\Python36-32\lib\site-packages\paramiko\packet.py", line 331, in readline
buf += self._read_timeout(timeout)
File "C:\Users\usr\AppData\Local\Programs\Python\Python36-32\lib\site-packages\paramiko\packet.py", line 488, in _read_timeout
raise EOFError()
EOFError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\usr\AppData\Local\Programs\Python\Python36-32\lib\site packages\paramiko\transport.py", line 1749, in run
self._check_banner()
File "C:\Users\usr\AppData\Local\Programs\Python\Python36-32\lib\site-packages\paramiko\transport.py", line 1897, in _check_banner
raise SSHException('Error reading SSH protocol banner' + str(e))
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
Traceback (most recent call last):
File "C:\Users\usr\AppData\Local\Programs\Python\Python36-32\lib\site-packages\paramiko\transport.py", line 1893, in _check_banner
buf = self.packetizer.readline(timeout)
File "C:\Users\usr\AppData\Local\Programs\Python\Python36-32\lib\site packages\paramiko\packet.py", line 331, in readline
buf += self._read_timeout(timeout)
File "C:\Users\usr\AppData\Local\Programs\Python\Python36-32\lib\site packages\paramiko\packet.py", line 488, in _read_timeout
raise EOFError()
EOFError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/usr/ownCloud/Server/Hacking in Python/Ssh-Cracker.py", line 45, in <module>
do_ssh_stuff("192.168.178.49", "usr", "passwd")
File "C:/Users/usr/ownCloud/Server/Hacking in Python/Ssh-Cracker.py", line 26, in do_ssh_stuff
ssh.connect(hostname=host, username=usr, password=pwd)
File "C:\Users\usr\AppData\Local\Programs\Python\Python36-32\lib\site packages\paramiko\client.py", line 338, in connect
t.start_client(timeout=timeout)
File "C:\Users\usr\AppData\Local\Programs\Python\Python36-32\lib\site packages\paramiko\transport.py", line 500, in start_client
raise e
File "C:\Users\usr\AppData\Local\Programs\Python\Python36-32\lib\site packages\paramiko\transport.py", line 1749, in run
self._check_banner()
File "C:\Users\usr\AppData\Local\Programs\Python\Python36-32\lib\site-packages\paramiko\transport.py", line 1897, in _check_banner
raise SSHException('Error reading SSH protocol banner' + str(e))
paramiko.ssh_exception.SSHException: Error reading SSH protocol banner
我尝试将transport.py中的横幅超时设置为15到30秒,但没有成功。
有人能指出我正确的方向吗?