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

  • 相关阅读:
    vue实现通过链接跳转到页面
    vue-cli2-项目的创建
    平均数
    Spring-Spring简介
    vue + element-ui 表单校验封装公用方法
    Python(一)数据结构和算法的20个练习题问答
    Python包中__init__.py作用
    if __name__=="__main__":
    execute immediate
    oracle基础知识过一遍(原创)
  • 原文地址:https://www.cnblogs.com/diysoul/p/10733445.html
Copyright © 2011-2022 走看看