Skip to main content
 首页 » 编程设计

python之将 sshpass 更改为更安全的解决方案

2025年02月15日7softidea

我有一个包含以下函数的 python 脚本:

def upload2server(file): 
    host_name = 'example.ex.am.com' 
    port_num = '432' 
    user_name = 'user' 
    password = 'passw' 
    web_path = '/example/files/' 
    full_webpath = user_name + '@' + host_name + ':' + web_path + args.key 
    pre_command = 'sshpass -p "' + password + '" scp -P' + ' ' + port_num + ' '  
 
    scp_comm = pre_command + file + ' ' + full_webpath  
 
    os.system(scp_comm) 

我有两个问题:

  1. 如果我使用端口转发从远程网络运行此脚本有多不安全?
  2. 我可以通过哪些方式使上传更安全?

谢谢!

请您参考如下方法:

就个人而言,我会为每个主机生成一个 SSH key 对,然后您就可以完全忘记在 scp 命令中使用密码了。内联密码不是问题,但这确实意味着您的密码将记录在该用户的 ~/.bash_history 文件中。