在一个本地主机中有几个终端,我在其中ssh到相同的用户和相同的IP地址。我想要找到远程主机登录的所有终端,终止在这些终端上运行的所有进程,然后注销该远程主机。我成功地使用了以下shell脚本。
#Find list of terminals in which the remote host is logged in.
openedTerminals=`ssh $user@$publicIP "ps -aux | grep -i $user@pts | grep -v grep | cut -d' ' -f 3"`
#close all the ssh sessions to that remote host
i=1
terminalPID=`echo $openedTerminals | cut -d' ' -f $i`
while [[ -n "$terminalPID" ]]
do
ssh $user@$publicIP "kill $terminalPID"
i=`expr $i + 1`
terminalPID=`echo $openedTerminals | cut -d' ' -f $i`
done我使用以下命令打开一个新的终端,并使用ssh进入远程主机,在命令提示符下执行该命令时,该命令工作正常:
gnome-terminal -window-with-profile=NOCLOSEPROFILE -e "ssh -X $user@$publicIP"
除了完成第一个代码的工作之外,我还想为第一个代码终止的每个远程计算机打开一个新的终端(通过ssh连接到另一台远程计算机)。所以我试着在第一段代码中插入上面的命令:
#Find list of terminals in which the remote host is logged in.
openedTerminals=`ssh $user@$publicIP "ps -aux | grep -i $user@pts | grep -v grep | cut -d' ' -f 3"`
#close all the ssh sessions to that remote host
i=1
terminalPID=`echo $openedTerminals | cut -d' ' -f $i`
while [[ -n "$terminalPID" ]]
do
ssh $user@$publicIP "kill $terminalPID"
gnome-terminal -window-with-profile=NOCLOSEPROFILE -e "ssh -X $newUser@$newPublicIP"
i=`expr $i + 1`
terminalPID=`echo $openedTerminals | cut -d' ' -f $i`
done但这开始在无限循环中运行,并打开无限数量的新终端。
请告诉我我哪里错了,并提出了一种纠正的方法,以便获得所需的解决方案。
另外,我希望在相同的shell脚本(第一个代码)中添加一个命令,以关闭远程计算机注销的终端。有人能在这方面给我指点一下吗?
先谢谢你,Saeya
发布于 2013-10-24 17:14:55
当只打开一个通过ssh连接到远程机器的终端时,由于"cut“命令,这将在无限循环中运行。如果有一个单独的情况来处理一个终端,这将很好地工作。
https://stackoverflow.com/questions/19549873
复制相似问题