如果两台服务器之间使用了RSA秘钥免密码登录的方式,可以先查找出rsa秘钥的对应目录(如find / -name id_rsa 或者locate id_rsa),

接着通过python中paramiko模块可以这样实现scp功能:

 

def scp_by_key(host_ip, host_port, remote_path, local_path, username, pkey_path):
    try:
        key=paramiko.RSAKey.from_private_key_file(pkey_path)
        t = paramiko.Transport((host_ip, host_port))
        t.connect(username=username, pkey=key)

        sftp = paramiko.SFTPClient.from_transport(t)
        src = remote_path
        des = local_path
        sftp.get(src,des)

        t.close()
    except Exception as e:
        print e


我们可以这样使用该方法: 

 

 

scp_by_key('192.168.0.33', 22, '/xx/xxx/a.txt', 'xx/xxx/b.txt', 'xiaomo', '/home/xiaomo/.ssh/id_rsa')

 

 

用起来是不是很爽呢?不过前提是要有rsa密钥...如果需要密码,则只需将pkey参数换为password传入即可:

 

t = paramiko.Transport((host_ip, host_port))
t.connect(username=username, password='xxx')


 

 

相关文章:

  • 2022-12-23
  • 2022-02-25
  • 2021-06-12
  • 2021-05-03
  • 2021-11-15
  • 2021-07-28
  • 2021-06-25
猜你喜欢
  • 2022-12-23
  • 2021-08-26
  • 2022-12-23
  • 2021-11-22
  • 2021-12-18
相关资源
相似解决方案