【发布时间】:2018-05-01 03:53:55
【问题描述】:
假设我有一个通过 ssh 在远程机器上执行命令的 bash 脚本:
# 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-agent。 -
有没有办法做到这一点,不需要以任何方式修改主机?
-
我想不出很多不使用私钥身份验证的原因。
-
sshpass' 选项-e可能会有所帮助。