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()
    

      

  • 相关阅读:
    Java后端知识体系
    HashMap底层实现整理
    Java线程池
    Spring Boot+Dubbo 入门
    Go 代码记录(一)
    Servlet 复习
    Spring Cloud(二)Eureka:服务注册与发现
    Spring Cloud (一)概述
    数据结构基础知识
    容器技术-Docker入门
  • 原文地址:https://www.cnblogs.com/jesse-gong/p/8183845.html
Copyright © 2011-2022 走看看