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次,有没有办法让我只连接一次,并保持会话和做剩余的调用。
请给我一些建议。
发布于 2017-01-19 21:57:59
有时,需要按顺序或并行打开多个连接。对于这些情况,您可以在主模式下运行一个ssh,这将建立连接,并在控制模式下运行其他you,这会在第一个连接上“搭载”,从而避免再次进行身份验证。
# * -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*选项。
https://stackoverflow.com/questions/41742146
复制相似问题