这个问题已经在这里有了答案: > Setup and use SSH ControlMaster Session in a Shell Script 1个
想象一下,我有一个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
有关如何为控制套接字创建唯一路径的信息,请参见man ssh_config,在ControlPath选项下.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。