python Paramiko 模块远程管理主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/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, local_path, remote_path):
    username = 'root'
  
    fip = open(fiperror,'a')
 
    cmds = cmds
    flag = flag
 
    try:
        sshd = ssh_connect( ip, username, password )
 
        if flag == '1':
            _local_path = local_path
            _remote_path = remote_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,"\t",password,"\n"])
        fip.close()
 
if __name__ == "__main__":
    with open('iplist') as f:
     
        errfile = "/tmp/err.log"
        fd = open(errfile, 'w')
        fd.truncate()
        fd.close()
        local_path = ''
        remote_path = ''
         
        scp_flag = input('scp regular file? yes(input 1), no(input 0): ')
        if scp_flag == '1':
            local_path=input('local file path: ')
            remote_path=input('remote host path: ')
 
            local_path = os.path.join(os.getcwd(), local_path)
        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()
            length = len(x)
            ip = x[0]
            if length == 1:
                password = '123456'
            else:
                password = x[1]
            print('---------------ip address: %s--------------' % ip)
            main(ip=ip, password=password, fiperror=errfile, cmds=cmds, flag=scp_flag, local_path=local_path, remote_path=remote_path)

 

posted @   东宫得臣  阅读(178)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示