安全地记住 bash 脚本中的 ssh 凭据[重复]

2024-05-06

假设我有一个 bash 脚本,它通过 ssh 在远程计算机上执行命令:

# Do something here
ssh otheruser@host command1
# Do something else
ssh otheruser@host command2
# Do most local tasks

该脚本提示我多次输入 otheruser@host 的凭据。是否有一种安全、简单且可接受的方法来在脚本的生命周期内缓存这些凭据,但保证它们在脚本结束后(正常情况下或发生错误时)丢失?也许解决方案会使用 ssh-agent ?

我正在寻找这样的东西:

special_credential_saving_command_here # This will prompt for credentials
ssh otheruser@host command1 # This will not prompt now
ssh otheruser@host command2 # This will not prompt either

我的动机是避免在同一脚本中多次输入凭据,同时又避免冒脚本终止后这些凭据仍然存在的风险。输入凭据不仅很麻烦,而且还需要我等待脚本完成,以便我可以输入凭据,而不是让它自行运行(这是一个长时间运行的脚本)。


使用控制套接字在多个进程之间共享经过身份验证的连接:

ssh -fNM -S ~/.ssh/sock otheruser@host  # Will prompt for password, then exit
...
ssh -S ~/.ssh/sock otheruser@host command1
ssh -S ~/.ssh/sock otheruser@host command2
...
ssh -S ~/.ssh/sock -O exit otheruser@host  # Close the master connection

See man ssh_config, 在下面ControlPath选项,了解如何为控制套接字创建唯一路径的信息。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

安全地记住 bash 脚本中的 ssh 凭据[重复] 的相关文章

随机推荐