【问题标题】:Safely remembering ssh credentials in bash script [duplicate]在 bash 脚本中安全地记住 ssh 凭据 [重复]
【发布时间】: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 可能会有所帮助。

标签: linux bash ssh


【解决方案1】:

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

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

请参阅ControlPath 选项下的man ssh_config,了解有关如何为控制套接字创建唯一路径的信息。

【讨论】:

  • 都不是;使用主模式进行连接共享:)
  • “3 号门”不是一个选项 ;) 这是社区 wiki,因此请随时提供更多详细信息。
猜你喜欢
  • 2011-09-05
  • 1970-01-01
  • 2021-08-01
  • 2019-05-23
  • 2019-06-05
  • 1970-01-01
  • 2018-05-31
  • 2012-07-13
  • 1970-01-01
相关资源
最近更新 更多