当前位置:   article > 正文

[paramiko] ssh通过跳板机连接服务器 - 方式一_paramiko 跳板机

paramiko 跳板机

通过跳板机连接服务器

方式一:
使用exec_command()

Python + Paramiko 的SSH 和 SFTP基本使用:

  • 执行命令
  • 上传文件
  • 下载文件
#!/usr/bin/env python3

import os

import paramiko

# exec_command(cmd)
# 文件传输
class connect_ssh:

    def __init__(self):
        jumpbox_host_ip = "103.115.76.**"  # 跳板机
        ssh_user = "sshuser"
        ssh_key_filename = os.getenv('HOME') + '/.ssh/id_rsa'
        target_host_ip = '10.169.**.**'  # 目的服务器
        print(ssh_key_filename)

        print("connecting to the jump host")
        # 创建一个实例化
        jumpbox_ssh = paramiko.SSHClient()
        # 自动添加策略,保存远端主机的主机名和密钥信息,如果不添加,那么不在本地knows_hosts文件中记录的主机将无法连接,默认拒接
        jumpbox_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        # 连接跳板机
        jumpbox_ssh.connect(hostname=jumpbox_host_ip, username=ssh_user, key_filename=ssh_key_filename)
        print("\n** connected jumpbox! **")

        print("\nopening a tunnel for connecting router host")
        # 创建一个中间连接的通道
        jumpbox_transport = jumpbox_ssh.get_transport()
        src_addr = (jumpbox_host_ip, 22)
        dest_addr = (target_host_ip, 22)
        jumpbox_channel = jumpbox_transport.open_channel(kind="direct-tcpip", dest_addr=dest_addr, src_addr=src_addr, )
        print("\n** opened a tunnel! **")

        print("\nconnecting a router host")
        # 去连接远端服务器
        target_ssh = paramiko.SSHClient()
        target_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        target_ssh.connect(hostname=target_host_ip, username=ssh_user, key_filename=ssh_key_filename,
                              sock=jumpbox_channel)
        print("\n** connected to the destination host! **")
		# 创建一个SFTP客户端通道
        trans = target_ssh.get_transport()
        sftp = paramiko.SFTPClient.from_transport(trans)
        print(sftp)

        self.target_ssh = target_ssh
        self.sftp = sftp
    
    def execute(self, cmd):    
        stdin, stdout, stderr = self.target_ssh.exec_command(cmd)
        print(stdout.read())
        return stdout.read()
  
    def upload_file(self, local_path, remote_path):    
        self.sftp.put(localpath=local_path, remotepath=remote_path)
        print("** Upload file succeefully! **")
    
    def download_file(self, remote_path, local_path):
        self.sftp.get(remotepath=remote_path, localpath=local_path)
        print("** download file succeefully! **")

    def close(self, cmd):    
        self.target_ssh.close()
        self.jumpbox_ssh.close()
        self.sftp.close()
        print("Closed!")


if __name__ == '__main__':
    server = connect_ssh()
    server.execute("pwd\n")
    remote_path = '/ldap_home/user/test_data/test_simple_data.csv'
    local_path = '/Users/user/testfolder/test_simple_data.csv'
    server.upload_file(local_path, remote_path)
    server.download_file(remote_path, local_path)
    
    print("done")

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/418600
推荐阅读
相关标签
  

闽ICP备14008679号