要求:
要实现,就必须达到要求。我用下面的东西
在python脚本的行下面添加了
['ssh', '-t', '1.1.1.1', 'python -u pull_details.py username password 10.10.10.10--get_name']
这正常工作,没有任何问题。
这种方法的问题是,当脚本因某种原因失败时显示密码,如下所示:
File "/usr/lib64/python2.7/subprocess.py", line 575, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['ssh', '-t', '1.1.1.1', 'python -u pull_details testuser testpassword 10.10.10.10 --get_name']' returned non-zero exit status 1
我传递的款项不应该显示所有我需要的密码。
建议很少有成员使用paramiko来解决上述问题。
我尝试使用以下脚本在paramiko中实现相同的功能:
import os
import sys
import paramiko
class Connect_Remote:
def connect(self, username, password, hostname):
self.client=paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(hostname,username=username,password=password, look_for_keys=False, allow_agent=False)
def send_cmd_output(self, cmd):
stdin, stdout, stderr = self.client.exec_command(cmd)
return stdout.read()
def execute(self):
#connect to remote machine 100.100.100.100
self.connect("testuser", "testpassword", "1.1.1.1")
print self.send_cmd_output("ls -l")
print self.send_cmd_output("hostname")
self.connect("testuser", "testpassword", "10.10.10.10")
print self.send_cmd_output("sh interface brief")
remote_connect = Connect_Remote()
remote_connect.execute()
当我尝试执行脚本时,出现错误提示。
Traceback (most recent call last):
File "test2.py", line 23, in <module>
remote_connect.execute()
File "test2.py", line 20, in execute
self.connect("testuser", "testpassword", "10.10.10.10")
File "test2.py", line 9, in connect
self.client.connect(hostname,username=username,password=password, look_for_keys=False, allow_agent=False)
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 338, in connect
retry_on_signal(lambda: sock.connect(addr))
File "/usr/lib/python2.7/site-packages/paramiko/util.py", line 279, in retry_on_signal
return function()
File "/usr/lib/python2.7/site-packages/paramiko/client.py", line 338, in <lambda>
retry_on_signal(lambda: sock.connect(addr))
File "/usr/lib64/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 110] Connection timed out
任何人都可以提出正确的方法或解决上述问题的方法