zoukankan      html  css  js  c++  java
  • 简单的堡垒机链接

    import paramiko
    import threading
    
    
    class ftpclient(object):
        def __init__(self):
            self.ssh = paramiko.SSHClient()
    
        def sftp_connect(self, hostname, port, username, passwd):
            transprot = paramiko.Transport((hostname, port))
            transprot.connect(username=username, password=passwd)
            self.sftp = paramiko.SFTPClient.from_transport(transprot)
    
        def ssh_connect(self, hostname, port, username, passwd):
            self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            self.ssh.connect(hostname=hostname, port=port, username=username, password=passwd)
    
        def cmd_ssh(self, *args):
            while True:
                cmd = input(">>>")
                if cmd == 'q':
                    exit()
                stin, stdout, stderr = self.ssh.exec_command(cmd)
                res, err = stdout.read(), stderr.read()
                result = res if res else err
                print(result.decode())
    
        def interactive(self):
            while True:
                cmd = input(">>>").strip()
                if not cmd:
                    continue
                if cmd == 'q':
                    exit()
                cmd_str = cmd.split()[0]
                if hasattr(self, "%s" % cmd_str):  #判断用户输入的命令是否存在
                    func = getattr(self, "%s" % cmd_str) # 如果self对象中有属性cmd_str则打印cmd_str的值
                    func(cmd)
    
        def get(self, *args):
            filename = args[0].split()[1]
            self.sftp.put(filename, filename)
    
    
    host_list = ['192.168.15.94', '192.168.15.210']
    host_action = ["Action", "Put"]
    
    
    def login():
        while True:
            port = input("请输入端口>>>").strip()
            if port.isdigit():
                port = int(port)
            else:
                continue
            username = input("请输入用户名>>>").strip()
            if not username:
                print("用户为空")
                continue
            elif username.isdigit():
                print("无数字开头用户")
                continue
            passwd = input("请输入密码>>>").strip()
            if not passwd:
                print("密码为空")
                continue
            return [port, username, passwd]   #转为一个列表
    
    
    def main():
        while True:
            for index, host in enumerate(host_list):
                print(index, host)
            choice_host = input("请输入你要进去的主机列表>>>").strip()
            if choice_host.isdigit() and int(choice_host) < len(host_list):
                choice_host = int(choice_host)
                print('33[41;1m %s 33[0m' % (host_list[choice_host]))
                print("	")
            else:
                continue
    
            for index2, host2 in enumerate(host_action):
                print(index2, host2)
            choice_host_action = input("请输入你做的操作>>>").strip()
            if choice_host_action.isdigit() and int(choice_host_action) < len(host_action):
                choice_host_action = int(choice_host_action)
                print('33[41;1m %s 33[0m' % (host_action[choice_host_action]))
                print("	")
    
            hostname = host_list[int(choice_host)]
            if choice_host_action == 0:
                while True:
                    connect_list = login()
                    port = connect_list[0]
                    username = connect_list[1]
                    passwd = connect_list[2]
                    try:
                        ftp = ftpclient()
                        ftp.ssh_connect(hostname, port, username, passwd)
                        ftp.cmd_ssh()
                    except ImportError as e:
                        print("有错误")
            elif choice_host_action == 1:
                while True:
                    connect_list = login()
                    port = connect_list[0]
                    username = connect_list[1]
                    passwd = connect_list[2]
                    try:
                        sftp = ftpclient()
                        sftp.ssh_connect(hostname, port, username, passwd)
                        sftp.cmd_ssh()
                    except ImportError as e:
                        print("有错误")
            else:
                print("没有这个编号!!")
    
    p = threading.Thread(target=main,args=())
    p.start()
    

      

  • 相关阅读:
    UI-iOS开发中@property的属性weak nonatomic strong readonly等介绍
    UIView之userInteractionEnabled属性介绍
    UI-target...action设计模式,手势识别器.UIimageview
    UI-事件处理
    IOS开发—事件处理,触摸事件,UITouch,UIEvent,响应者链条,手势识别
    UI-事件,触摸与响应者链(一)
    第47月第11天 iOS 文件下载Download,支持断点续传、后台下载、设置下载并发数
    第47月第10天 telnet rpm包安装
    第47月第5天 openresty
    第47月第4天 arkit录制
  • 原文地址:https://www.cnblogs.com/jesse-gong/p/8183845.html
Copyright © 2011-2022 走看看