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

  • 相关阅读:
    Jmeter中的几个重要测试指标释义
    接口测试考虑
    树Hash学习笔记
    kali linux主题美化
    Kali Linux 终端字体配色
    解决github无法打开
    Python requests数据的content和text的区别
    Python+adb操作移动端自动化测试
    linux后台运行python程序
    洛谷P2791 幼儿园篮球题 另解
  • 原文地址:https://www.cnblogs.com/diysoul/p/10733445.html
Copyright © 2011-2022 走看看