ssh函数:
def ssh_cmd(ip,port,user,passwd,cmd):
out = None
ssh = None
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,user,passwd,timeout=5)
stdin, stdout, stderr = ssh.exec_command(cmd)
out = stdout.readlines()
print out
# log('\n'.join(out))
except Exception, e:
error('ssh_cmd error: ' %e)
finally:
if ssh:
ssh.close()
return out
执行:ssh_cmd(proxyIp,proxyPort,user,passwd,cmd)-------
正确理解:
proxyPort应该为远程登陆机器proxyIP的ssh端口,默认是22,不是代理服务的端口(
错误理解:其中proxyIP是代理服务器的IP地址,
proxyPort为代理服务器的端口)
提示:
No handlers could be found for logger "paramiko.transport"
Traceback (most recent call last):
File "http_request.py", line 135, in
main()
File "http_request.py", line 127, in main
get_squid_log("192.168.10.74",80,"root","redhat")
File "http_request.py", line 116, in get_squid_log
tmp=ssh_cmd(proxyIp,proxyPort,user,passwd,cmd)
File "http_request.py", line 33, in ssh_cmd
error('ssh_cmd error: ' %e)
NameError: global name 'error' is not defined
原因:
ssh_cmd(ip,
port,user,passwd,cmd)
我端口写错了,写成80了,默认应该为22
再次执行程序,不再报错,成功了!
扩充:
修改ssh的端口号:vim /etc/ssh/sshd_config
使修改生效:/etc/init.d/sshd restart
阅读(12219) | 评论(0) | 转发(0) |