码迷,mamicode.com
首页 > 编程语言 > 详细

python | 实现控制多台机器的脚本

时间:2020-07-05 23:22:42      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:print   bsp   cli   self   exception   word   put   脚本   net   

这个控制多台机器的思路值得学习

# -*- coding: utf-8 -*-
import optparse
import pxssh

class Client:
    def __init__(self,host,password,username):
        self.host = host
        self.password = password
        self.username = username
        self.session = self.connect()

    def connect(self):
        try:
            s = pxssh.pxssh()
            s.login(self.host , self.username, self.password)
            return s
        except Exception , e:
            print e
            print ‘[-]error connecting‘

    def send_command(self, cmd):
        self.session.sendline(cmd)
        self.session.prompt()
        return self.session


def botnetCommand(command):
    for client in botNet:
        output = Client.send_command(command)
        print ‘[*] Output from‘  + Client.host
        print ‘[+]‘ + output +‘\n‘

def addClient(host,user,password):
    client = Client(host,user,password)
    botNet.append(client)

botNet = []
addClient(‘10.1.1.0‘,‘root‘,‘tooy‘)
addClient(‘10.1.1.2‘,‘root1‘,‘tooy2‘)
addClient(‘10.1.1.1‘,‘root3‘,‘tooy4‘)
botnetCommand(‘uname -v‘)
botnetCommand(‘cat /etc/issue‘)

 

python | 实现控制多台机器的脚本

标签:print   bsp   cli   self   exception   word   put   脚本   net   

原文地址:https://www.cnblogs.com/J0ng/p/13252157.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!