python ssh 远程执行代码_python使用paramiko模块实现ssh远程登陆上传文件并执行

本文介绍了一个使用Python的Paramiko库实现的SSH上传文件并执行脚本的功能,利用线程池并发处理多个目标主机。通过`MyThread`类实现任务分发,`ThreadPool`管理线程,适用于批量远程文件操作和自动化运维场景。
摘要由CSDN通过智能技术生成

#function:upload files through ssh protocal and excute the files

#lib:paramiko

#MyThread:init a thread to run the function

#ThreadPol:init a thread pool

#uploadAndExecu:upload file and excute

#readConf:read config file

#-*- coding = utf-8 -*-

import Queue

import sys

import threading

import paramiko

import socket

from threading import Thread

import time

class MyThread(Thread):

def __init__(self, workQueue, timeout=1):

Thread.__init__(self)

self.timeout = timeout

self.setDaemon(False)

self.workQueue = workQueue

self.start()

#print 'i am runnning ...'

def run(self):

emptyQueue = 0

while True:

try:

callable, username, password, ipAddress, port,comms = self.workQueue.get(timeout = self.timeout)

#print 'attacking :',ipAddress,username,password,threading.currentThread().getName(),' time : '

callable(username,password, ipAddress, port,comms)

except Queue.Empty:

print threading.currentThread().getName(),":queue is empty ; sleep 5 seconds\n"

emptyQueue += 1

#judge the queue,if it is empty or not.

time.sleep(5)

if emptyQueue == 5:

print threading.currentThread().getName(),'i  quit,the queue is empty'

break

except Exception, error:

print error

class ThreadPool:

def __init__(self, num_of_threads=10):

self.workQueue = Queue.Queue()

self.threads = []

self.__createThreadPool(num_of_threads)

#create the threads pool

def __createThreadPool(self, num_of_threads):

for i in range(num_of_threads):

thread = MyThread(self.workQueue)

self.threads.append(thread)

def wait_for_complete(self):

#print len(self.threads)

while len(self.threads):

thread = self.threads.pop()

if thread.isAlive():

thread.join()

def add_job(self, callable, username, password, ipAddress, Port,comms):

self.workQueue.put((callable, username, password, ipAddress, Port,comms))

def uploadAndExecu(usernam,password,hostname,port,comm):

print usernam,password,hostname,port,comm

try:

t = paramiko.Transport((hostname,int(port)))

t.connect(username=username,password=password)

sftp=paramiko.SFTPClient.from_transport(t)

sftp.put(comm['local_dir'],comm['remote_dir'])

except Exception,e:

print 'upload files failed:',e

t.close()

finally:

t.close()

try:

ssh = paramiko.SSHClient()

ssh.load_system_host_keys()

ssh.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())

ssh.connect(hostname, port=int(port), username=username, password=password)

ssh.exec_command(comm['alter_auth'])

ssh.exec_command(comm['exec_program'])

except Exception,e:

print 'chang file auth or execute the file failed:',e

ssh.close()

def readConf():

comm={}

try:

f = file('command.txt','r')

for l in f:

sp = l.split(':')

comm[sp[0]]=sp[1].strip('\n')

except Exception,e:

print 'open file command.txt failed:',e

f.close()

return comm

if __name__ == "__main__":

commandLine = readConf()

print commandLine

#prepare the ips

wm = ThreadPool(int(commandLine['ThreadNum']))

try:

ipFile = file('ipandpass.txt','r')

except:

print "[-] ip.txt Open file Failed!"

sys.exit(1)

for line in ipFile:

IpAdd,username,pwd = line.strip('\r\n').split(' ')

wm.add_job(uploadAndExecu,username,pwd,IpAdd,commandLine['port'],commandLine)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值