• ssh 通过跳板机连接到远程服务器


    ssh 通过跳板机连接到远程服务器

    import paramiko
    from sshtunnel import SSHTunnelForwarder
    import threading
    
    
    def readData(shell):
        while True:
            data = shell.recv(2048)
            if not data:
                print("quit now")
                break
            data = data.decode()
            print(data, end = "")
    
    
    def main():
        port104 = 22
        port105 = 22
        localport = 10022
        with SSHTunnelForwarder(ssh_address_or_host=('192.168.1.104', port104), ssh_username="104usr", ssh_password="104pwd",
                                remote_bind_address=("192.168.1.105", port105), local_bind_address=('127.0.0.1', localport)) as tunnel:
            client = paramiko.SSHClient()
            client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            client.connect(tunnel.local_bind_host, tunnel.local_bind_port, username = "105usr", password = "105pwd")
            shell = client.invoke_shell()
    
            thread = threading.Thread(target=readData, args=(shell,))
            thread.start()
    
            shell.sendall("pwd
    ")
            shell.sendall("ifconfig
    ")
            shell.sendall("exit
    ")
    
            thread.join()
            client.close()
    
    
    if __name__ == '__main__':
        main()

    上面的代码通过, 192.168.1.104 连接到 192.168.1.105

    其中 192.168.1.104 是跳板机 ip, 192.168.1.105 是我们需要连接的服务器. 某些情况下, 我们无法直接连接到服务器, 而有其它主机可以连接到该服务器时就可以发挥作用了.

  • 相关阅读:
    linux内核之情景分析mmap操作
    linux内核情景分析之匿名管道
    linux内核情景分析之命名管道
    linux内核情景分析之信号实现
    Linux内核情景分析之消息队列
    linux2.4内核调度
    聊聊程序的配置文件
    汽车引擎是怎么工作的
    Go对OO的选择
    为而不争
  • 原文地址:https://www.cnblogs.com/diysoul/p/10733445.html
Copyright © 2020-2023  润新知