zoukankan      html  css  js  c++  java
  • python3 批量管理Linux服务器 下发命令与传输文件

    #!/usr/bin/env python3
    
    # -*- coding: utf-8 -*-
    
    import paramiko
    import os, stat
    import sys
    import operator as op
    from string import Template
    
    def ssh_connect( _host, _username, _password ):
        _ssh_fd = paramiko.SSHClient()
        _ssh_fd.set_missing_host_key_policy( paramiko.AutoAddPolicy() )
        _ssh_fd.connect( _host, username = _username, password = _password, timeout = 5 )
        return _ssh_fd
    
    def ssh_exec_cmd( _ssh_fd, _cmd ):
        return _ssh_fd.exec_command( _cmd )
    
    def ssh_sftp( _ssh_fd, _local_path, _remote_path ):
        _sftp = paramiko.SFTPClient.from_transport(_ssh_fd.get_transport())
        _sftp = _ssh_fd.open_sftp()
        _sftp.put(_local_path, _remote_path)
        #_sftp.get(_remote_path, _local_path)
        _sftp.close()
    
    def ssh_close( _ssh_fd ):
        _ssh_fd.close()
    
    def main(ip, password, fiperror, cmds, flag):
        username = 'root'
     
        fip = open(fiperror,'a')
    
        cmds = cmds
        flag = flag
    
        try:
            sshd = ssh_connect( ip, username, password )
    
            if flag == '1':
                local_path=input('local file path: ')
                remote_path=input('remote host path: ')
            
                local_path = os.path.join(os.getcwd(), local_path)
                try:
                    ssh_sftp(sshd, local_path, remote_path)
                except Exception as e:
                    print('Error: sftp failed')
    
            for cmd in cmds:
                stdin, stdout, stderr = ssh_exec_cmd( sshd, cmd )
                err_list = []
                err_list = stderr.readlines()
    
                items = []
                items = stdout.readlines()
    
                for item in items:
                    print("{} {}".format(ip, item), end='')
                    s = Template("nova list --${host_name}")
                    s = s.safe_substitute(host_name=item)
    
            ssh_close( sshd )
        except Exception as e:
            print( 'ssh %s@%s: %s' % (username, ip, e) )
            fip.writelines([ip,"	",password,"
    "])
            fip.close()
    
    if __name__ == "__main__":
        with open('iplist') as f:
        
            errfile = "/tmp/err.log"
            fd = open(errfile, 'w')
            fd.truncate()
            fd.close()
            
            scp_flag = input('scp regular file? yes(input 1), no(input 0): ')
            cmds = []
            cmd = input("input the cmd you want to execute(end with 0): ")
            while cmd != '0':
                cmds.append(cmd)
                cmd = input('input the cmd you want to execute(end with 0): ')
            for line in f:
                x = line.split()
                ip = x[0]
                if len(x) == 1:
                    password = '123456'
                else:
                    password = x[1]
                print('---------------ip address: %s--------------' % ip)
                main(ip=ip, password=password, fiperror=errfile, cmds=cmds, flag=scp_flag)
  • 相关阅读:
    HDU 3436 Queuejumpers
    POJ 3580 SuperMemo
    HDU 2871 Memory Control
    Android 实现显示文字的Gallery
    Android 使用地图
    Android 深入的研究一下蓝牙
    手机录制视频demo
    用android来实现图片的绘制以及旋转缩放案例分析
    TextView 自动滚动(跑马灯)
    Android腾讯微博客户端开发一:在下方的Tab的实现
  • 原文地址:https://www.cnblogs.com/donggongdechen/p/10972841.html
Copyright © 2011-2022 走看看