欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python使用paramiko模塊實現(xiàn)ssh遠(yuǎn)程登陸上傳文件并執(zhí)行

 更新時間:2014年01月27日 09:52:38   作者:  
使用paramiko,ssh遠(yuǎn)程登陸,長傳文件并執(zhí)行。其中用到了多線程和隊列,paramiko是用python語言寫的一個模塊,遵循SSH2協(xié)議,支持以加密和認(rèn)證的方式,進(jìn)行遠(yuǎn)程服務(wù)器的連接。

程序執(zhí)行時需要讀取兩個文件command.txt和ipandpass.txt。格式如下:

復(fù)制代碼 代碼如下:

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

程序中的隊列操作是修改的別的程序,寫的確實不錯。
該程序亦正亦邪,如果拿去做壞事,我先聲明與我無關(guān),我只是分享我的代碼罷了。
希望有興趣的同志們來討論技術(shù)應(yīng)用。
這其中用到了paramiko,隊列,多線程,后續(xù)也會寫一些這三個方面的東西。歡迎批評指正。
其實這個程序有些地方還有待優(yōu)化。

復(fù)制代碼 代碼如下:

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

相關(guān)文章

  • python接口自動化如何封裝獲取常量的類

    python接口自動化如何封裝獲取常量的類

    這篇文章主要介紹了python接口自動化如何封裝獲取常量的類,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • Python+Tkinter創(chuàng)建一個簡單的鬧鐘程序

    Python+Tkinter創(chuàng)建一個簡單的鬧鐘程序

    這篇文章主要為大家詳細(xì)介紹了如何使用 Python 的 Tkinter 庫創(chuàng)建一個簡單的鬧鐘程序,它可以在指定的時間播放一個聲音來提醒你,感興趣的可以學(xué)習(xí)一下
    2023-04-04
  • 基于Python爬蟲采集天氣網(wǎng)實時信息

    基于Python爬蟲采集天氣網(wǎng)實時信息

    這篇文章主要介紹了基于Python爬蟲采集天氣網(wǎng)實時信息,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • 解決tensorflow模型參數(shù)保存和加載的問題

    解決tensorflow模型參數(shù)保存和加載的問題

    今天小編就為大家分享一篇解決tensorflow模型參數(shù)保存和加載的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • python分割一個文本為多個文本的方法

    python分割一個文本為多個文本的方法

    這篇文章主要為大家詳細(xì)介紹了python分割一個文本為多個文本,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • PyTorch預(yù)訓(xùn)練Bert模型的示例

    PyTorch預(yù)訓(xùn)練Bert模型的示例

    這篇文章主要介紹了PyTorch預(yù)訓(xùn)練Bert模型的示例,幫助大家更好的進(jìn)行機器學(xué)習(xí),訓(xùn)練模型,感興趣的朋友可以了解下
    2020-11-11
  • 淺談Python中的全局鎖(GIL)問題

    淺談Python中的全局鎖(GIL)問題

    今天小編就為大家分享一篇淺談Python中的全局鎖(GIL)問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • pycharm安裝圖文教程

    pycharm安裝圖文教程

    這篇文章主要為大家詳細(xì)介紹了pycharm安裝圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Python遠(yuǎn)程桌面協(xié)議RDPY安裝使用介紹

    Python遠(yuǎn)程桌面協(xié)議RDPY安裝使用介紹

    這篇文章主要介紹了Python遠(yuǎn)程桌面協(xié)議RDPY安裝使用介紹,本文講解了RDPY的安裝、RDPY的簡單使用兩部份內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Python基于Serializer實現(xiàn)字段驗證及序列化

    Python基于Serializer實現(xiàn)字段驗證及序列化

    這篇文章主要介紹了Python基于Serializer實現(xiàn)字段驗證及序列化,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11

最新評論