python 多线程远程执行命令

如题,代码如下:

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()


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSH(Secure Shell)是一种网络协议,用于在不安全的网络上安全地进行远程登录和执行命令。在Python中,可以使用paramiko库来实现SSH连接和执行命令的功能。下面是一个使用多线程实现SSH连接和执行命令Python代码示例: ```python import paramiko import threading def ssh_command(ip, username, password, command): client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(ip, username=username, password=password) stdin, stdout, stderr = client.exec_command(command) print(f"Output from {ip}:") for line in stdout: print(line.strip()) client.close() # 定义要连接的主机列表 hosts = [ {"ip": "192.168.1.1", "username": "user1", "password": "password1"}, {"ip": "192.168.1.2", "username": "user2", "password": "password2"}, # 添加更多主机... ] # 定义要执行的命令 command = "ls -l" # 创建线程列表 threads = [] # 创建并启动线程 for host in hosts: t = threading.Thread(target=ssh_command, args=(host["ip"], host["username"], host["password"], command)) t.start() threads.append(t) # 等待所有线程执行完毕 for t in threads: t.join() ``` 上述代码中,我们首先导入了paramiko库,然后定义了一个`ssh_command`函数,该函数用于建立SSH连接并执行命令。接下来,我们定义了要连接的主机列表和要执行的命令。然后,我们创建了一个线程列表,并使用多线程的方式分别连接每个主机并执行命令。最后,我们等待所有线程执行完毕。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值