首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何避免多个ssh联系远程?

如何避免多个ssh联系远程?
EN

Stack Overflow用户
提问于 2017-01-19 20:46:23
回答 1查看 86关注 0票数 0
代码语言:javascript
复制
ssh researcher@192.168.1.1 'ls somefile' > folders.txt
echo "Trying to connect and show files from Remote"
scp researcher@192.168.1.1:somefile somefile

从代码中我们可以看到,iam首先使用ssh会话查找所有文件,然后再建立另一个ssh会话来下载files.Ignore任何内容或语法错误(我已经替换了一些机密信息)。

所以每次我尝试连接到远程,它会问我密码,这个过程需要3-4秒,我的脚本有4个ssh调用,这需要大量的time.So,而不是连接4次,有没有办法让我只连接一次,并保持会话和做剩余的调用。

请给我一些建议。

EN

回答 1

Stack Overflow用户

发布于 2017-01-19 21:57:59

有时,需要按顺序或并行打开多个连接。对于这些情况,您可以在主模式下运行一个ssh,这将建立连接,并在控制模式下运行其他you,这会在第一个连接上“搭载”,从而避免再次进行身份验证。

代码语言:javascript
复制
# * -M puts the connection in master mode
#
# * ControlPersist keeps the connection alive after the current client
#   exits, for use by other clients
#
# * ControlPath specifies the socket to use. %C expands to a combination
#   of the local and remote host names, the user id, and the port.
#   It should be in a directory writeable only by you, but for this
#   example we just put it in the current directory. The same socket
#   is used by each client wishing to piggyback on the open connection.

ssh -M -o ControlPersist=yes -o ControlPath=%C researcher@192.168.1.1  ls somefile > folders.txt

scp -o ControlPath=%C researcher@192.168.1.1:somefile somefile
# -O simply sends a command to the master connection, in this case
# closing it.
ssh -o ControlPath=%C -O exit researcher@192.168.1.1  # end the session

通过在.ssh/config文件中添加适当的选项,而不是在命令行中重复这些选项,可以自动执行很多操作。有关更多详细信息,请参阅man ssh_config中的各种Control*选项。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41742146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档