Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
shell脚本中SSH上的持续通信_Shell_Ssh_Zsh - Fatal编程技术网

shell脚本中SSH上的持续通信

shell脚本中SSH上的持续通信,shell,ssh,zsh,Shell,Ssh,Zsh,我正在编写一个zsh脚本,我想在其中建立一个SSH连接,向远程主机发送一些命令并读取输出 我可以做类似的事情 cat commands.list | ssh foobar.com | { process output } 但是,这将一次性执行,连接将关闭。相反,我想做一些像- while [ some condition ]; do write command to ssh process read output from ssh process make some d

我正在编写一个zsh脚本,我想在其中建立一个SSH连接,向远程主机发送一些命令并读取输出

我可以做类似的事情

cat commands.list | ssh foobar.com | { process output }
但是,这将一次性执行,连接将关闭。相反,我想做一些像-

while [ some condition ]; do
    write command to ssh process
    read output from ssh process
    make some decision about the next command to write to ssh process
do 
我已经看过了,但还没有弄清楚如何准确地利用这些优势

有一种写作的形式,但没有一种形式可以让我同时读写。如果有一个,我可以将这个过程与自定义文件描述符联系起来吗

我还尝试将ssh进程的stdin和stdout绑定到两个fifo(管道),但是向stdin管道写入(使用echo)也会发送一个EOF(当echo进程结束时),在执行所写入的一个命令后关闭连接

如果/如何实现我在这里的目标,有什么想法吗


谢谢。

谢谢切普纳的评论

我还发现了ssh的ControlMaster特性。


在我的配置文件中配置了它,我不介意为每个远程命令再次运行ssh

一些选项:
coproc ssh-tt foobar.com
(阅读
man zshmisc
中的
coproc
修饰符),将脚本复制到远程主机并在那里执行,或者使用
ssh
-M
选项设置主
ssh
会话;它将进行一次身份验证,并允许后续的
ssh
会话共享该通道,从而大大加快启动速度。