zoukankan      html  css  js  c++  java
  • 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 是我们需要连接的服务器. 某些情况下, 我们无法直接连接到服务器, 而有其它主机可以连接到该服务器时就可以发挥作用了.

  • 相关阅读:
    通过wireshark抓包来讲解HTTP中Connection: keep-alive头部的作用
    spring cloud连载第三篇补充之Zuul
    Redis管理各类型存储数据命令
    Redis管理key命令
    Redis命令
    Redis数据类型
    Redis配置文件
    Redis安装
    Redis简介
    SpringBoot项目报错Cannot determine embedded database driver class for database type NONE
  • 原文地址:https://www.cnblogs.com/diysoul/p/10733445.html
Copyright © 2011-2022 走看看