python 多线程远程执行命令
2017-06-09 17:12
211 查看
如题,代码如下:
import paramiko,threading,sys,time,os class SSHThread(threading.Thread): def __init__(self, ip, port,user,pwd,timeout,cmd): threading.Thread.__init__(self) self.ip = ip self.port = port self.user = user self.pwd = pwd self.timeout = timeout self.cmd = cmd self.LogFile = "/home/linxw/temp/test.log" def run(self): print("Start try ssh => %s" % self.ip) try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(self.ip, self.port, username=self.user, password=self.pwd, timeout=self.timeout) print("[%s] Login %s => %s " % (self.ip, self.user, self.pwd)) open(self.LogFile, "a").write("[ %s ] IP => %s, port => %d, %s => %s" % (time.asctime( time.localtime(time.time()) ), self.ip, self.port, self.user, self.pwd)) print("[%s] exec : %s" % (self.ip,self.cmd)) open(self.LogFile,"a").write("[%s] exec : %s" % (self.ip,self.cmd)) stdin,stdout,stderr = ssh.exec_command(self.cmd) print("[%s] exec result : %s" % (self.ip,stdout.read())) return True except: print("[%s] Error %s => %s" % (self.ip, self.user, self.pwd)) open(self.LogFile, "a").write("[%s] Error %s => %s" % (self.ip, self.user, self.pwd)) return False def ViolenceSSH(ip, port,user,pwd,timeout,cmd): ssh_scan = SSHThread(ip, port, user, pwd, timeout,cmd) ssh_scan.start() if __name__ == '__main__': ipList = ['192.168.163.128','127.0.0.1'] for ip in ipList: threading.Thread(target = ViolenceSSH, args = (ip, 22,'root','1234',3,'uptime' )).start()
相关文章推荐
- 利用python多线程执行远程linux上命令
- python paramiko实现多线程远程执行命令、多线程远程上传文件、多线程远程下载文件
- python paramiko实现多线程远程执行命令、多线程远程上传文件、多线程远程下载文件
- 利用python多线程执行远程linux上命令
- 项目总结(三)----------Python实现SSH远程登陆,并执行命令!
- python写的批量操作远程主机脚本(命令执行,上传、下载文件)
- python写的批量操作远程主机脚本(命令执行,上传、下载文件)
- 利用python fabric模块写的批量操作远程主机脚本(命令执行,上传、下载文件)
- python 远程执行命令、发布文件
- python 远程执行命令、发布文件
- python 实现远程主机执行命令函数
- python 利用pexpect进行多机远程命令执行
- python写的批量操作远程主机脚本(命令执行,上传、下载文件)
- python实现Telnet远程登陆到设备并执行命令
- Python脚本远程批量执行命令
- python ssh2远程登录节点并执行命令
- python socket 实现远程执行命令 上传 下载文件
- python 利用pexpect进行多机远程命令执行
- python写的批量操作远程主机脚本(命令执行,上传、下载文件)
- python写的批量操作远程主机脚本(命令执行,上传、下载文件)