单线程的模式,只能执行一条语句
多线程的模式,可以执行多条语句
#-*- coding: utf-8 -*-
#!/usr/bin/python
import paramiko
import threading
def ssh2(ip,username,passwd,cmd):
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,22,username,passwd,timeout=5)
for m in cmd:
stdin, stdout, stderr = ssh.exec_command(m)
# stdin.write("Y") #简单交互,输入 ‘Y’
out = stdout.readlines()
#屏幕输出
for o in out:
print o,
print '%s\tOK\n'%(ip)
ssh.close()
except :
print '%s\tError\n'%(ip)
if __name__=='__main__':
cmd = ['sys','blacklist ip 192.168.2.12']
ip = "119.255.248.26"
username = "sz1card1-admin"
passwd = "sz1card12015@admin"
threads = []
print "Begin......"
a=threading.Thread(target=ssh2,args=(ip,username,passwd,cmd))
a.start()