Program execution requires reading two files Command.txt and ipandpass.txt. The format is as follows:
The code is as follows:
Command.txt:
Threadnum:1
Port:22
Local_dir:hello_mkdir
Remote_dir:hello_mkdir
Alter_auth:chmod 755 Hello_mkdir
Exec_program:./hello_mkdir
Ipandpass.txt:
IP username Password
The queue operation in the program is the modification of other programs, written really good.
This procedure is also evil, if you take to do bad things, I first declare nothing to do with me, I just share my code.
Interested comrades are expected to discuss technology applications.
This uses the Paramiko, queue, multi-threaded, follow-up will also write some of these three aspects of things. Welcome criticism.
In fact, some areas of this program still need to be optimized.
The code is as follows:
#function: Upload files through ssh protocal and excute the files
#lib:p Aramiko
#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:
Emptyqueue = 0
while True:
Try:
callable, username, password, ipAddress, Port,comms = s Elf.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 an empty or not.
Time.sleep (5)
If Emptyqueue = = 5:
Print Threading.currentthread (). GetName (), ' I quit,the queue is empty '
B Reak
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)