python中的怎么利用paramiko ssh到指定的机器上并调用自己写的方法?
importosimportparamikodefcheck():output=os.popen('servicentpdstatus')forfileinoutput:...
import os
import paramiko
def check():
output=os.popen('service ntpd status')
for file in output:
if 'stopped' in file:
print "ntpd is stopped"
else:
print "ntpd is running"
if __name__ == 'main':
for ip in ('10.0.0.1','10.0.0.2'):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip)
check()
# stdin, stdout, stderr = ssh.exec_command("你的号令")# print stdout.readlines()
ssh.close()
这里调用自己写的check()方法并没有ssh到指定的ip上执行,而是在本机上执行的chenk()方法,怎么才能利用paramiko ssh到指定的机器上执行自己写的方法呢? 展开
import paramiko
def check():
output=os.popen('service ntpd status')
for file in output:
if 'stopped' in file:
print "ntpd is stopped"
else:
print "ntpd is running"
if __name__ == 'main':
for ip in ('10.0.0.1','10.0.0.2'):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip)
check()
# stdin, stdout, stderr = ssh.exec_command("你的号令")# print stdout.readlines()
ssh.close()
这里调用自己写的check()方法并没有ssh到指定的ip上执行,而是在本机上执行的chenk()方法,怎么才能利用paramiko ssh到指定的机器上执行自己写的方法呢? 展开
1个回答
展开全部
import os
import paramiko
def check(output):
# output=os.popen('service ntpd status')
for file in output:
if 'stopped' in file:
print "ntpd is stopped"
else:
print "ntpd is running"
if __name__ == 'main':
for ip in ('10.0.0.1','10.0.0.2'):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip)
# check()
stdin, stdout, stderr = ssh.exec_command("service ntpd status")
check(stdout.readlines())
ssh.close()
import paramiko
def check(output):
# output=os.popen('service ntpd status')
for file in output:
if 'stopped' in file:
print "ntpd is stopped"
else:
print "ntpd is running"
if __name__ == 'main':
for ip in ('10.0.0.1','10.0.0.2'):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip)
# check()
stdin, stdout, stderr = ssh.exec_command("service ntpd status")
check(stdout.readlines())
ssh.close()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询