• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python使用paramiko模块实现ssh远程登陆上传文件并执行

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

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python paramiko模块,python paramiko,python3 paramiko,python paramiko下载,python paramiko安装等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

程序执行时需要读取两个文件command.txt和ipandpass.txt。格式如下:

ipandpass.txt:
ip username password
</div>

程序中的队列操作是修改的别的程序,写的确实不错。
该程序亦正亦邪,如果拿去做坏事,我先声明与我无关,我只是分享我的代码罢了。
希望有兴趣的同志们来讨论技术应用。
这其中用到了paramiko,队列,多线程,后续也会写一些这三个方面的东西。欢迎批评指正。
其实这个程序有些地方还有待优化。


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:
     &nb

您可能想查找下面的文章:

  • Python Paramiko模块的安装与使用详解
  • Python使用Paramiko模块编写脚本进行远程服务器操作
  • python下paramiko模块实现ssh连接登录Linux服务器
  • paramiko模块安装和使用(远程登录服务器)
  • python使用paramiko模块实现ssh远程登陆上传文件并执行
  • python paramiko实现ssh远程访问的方法

相关文章

  • python web框架学习笔记
  • 使用相同的Apache实例来运行Django和Media文件
  • python实现上传样本到virustotal并查询扫描信息的方法
  • python使用socket远程连接错误处理方法
  • 浅析Python中的getattr(),setattr(),delattr(),hasattr()
  • Python实现遍历windows所有窗口并输出窗口标题的方法
  • Python批量修改文件后缀的方法
  • 举例讲解Python中的list列表数据结构用法
  • Linux上安装Python的PIL和Pillow库处理图片的实例教程
  • Python文件和目录操作详解

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python中对列表排序实例
    • Python中最常用的操作列表的几种方法归纳
    • Python中Collections模块的Counter容器类使用教程
    • Python操作CouchDB数据库简单示例
    • Python递归遍历列表及输出的实现方法
    • 两个使用Python脚本操作文件的小示例分享
    • python实现2048小游戏
    • python实现的希尔排序算法实例
    • python 文件操作api(文件操作函数)
    • python中将阿拉伯数字转换成中文的实现代码

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有