Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.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
Linux 用于从脚本断开SSH连接的命令_Linux_Shell_Exit - Fatal编程技术网

Linux 用于从脚本断开SSH连接的命令

Linux 用于从脚本断开SSH连接的命令,linux,shell,exit,Linux,Shell,Exit,我有这个剧本: #!/bin/bash #!/usr/bin/expect dialog --menu "Please choose the server to connect to:" 10 30 15 1 RAS01 2>temp #OK is pressed if [ "$?" = "0" ] then _return=$(cat temp) # /home is selected if [ "$_return" = "1" ]

我有这个剧本:

#!/bin/bash
#!/usr/bin/expect
dialog --menu "Please choose the server to connect to:" 10 30 15 1 RAS01 2>temp

#OK is pressed
if [ "$?" = "0" ]
then
        _return=$(cat temp)

        # /home is selected
        if [ "$_return" = "1" ]
        then
                dialog --infobox "Connecting to server ..." 5 30  ; sleep 2
                telnet XXX
        fi

# Cancel is pressed
else
        exit
fi

# remove the temp file
rm -f temp

在代码中说:
#取消被按下
的部分,我想插入某种类型的命令来断开会话并自动关闭终端。我尝试了不同的
退出
退出1
退出5
关闭
,等等。但似乎没有一个能起到作用

kill -9 "$(ps --pid $$ -oppid=)"
但我绝对建议你不要用这种方式。更好的解决方案是获取脚本的退出代码,并在需要时退出。比如说

yourscript

#... ...
else
    exit 1
fi
在ssh连接中,您可以执行以下操作:

./myscript || exit

这是正确的方法。尝试使用它

谢谢!我还有一个问题。这解决了我在处理“取消”按钮时的整个问题。但是,当用户选择OK时,它会启动到另一台服务器的telnet连接。。。一旦与外部服务器的telnet会话关闭,运行脚本的连接将自动注销。基本上尝试使用相同的命令:“kill-9”$(ps--pid$-oppid=)”,但当与外部服务器的telnet会话关闭时。@WiloMaldonado对不起,我没有听清楚。你能提供一个例子吗?简而言之,我们希望这个脚本能够自动为用户加载,而不允许他们做任何其他事情。它会自动加载,如果他们在对话框屏幕上点击“取消”,它会断开会话。因此,如果我在服务器1上,并且我选择启动到服务器2的telnet连接(如上面的代码所示),一旦我在服务器2上完成了所有操作并关闭了在服务器2上的会话,连接将返回到服务器1,但脚本不再运行。一旦用户从Server2返回,我们希望终止与Server1的活动连接。这更清楚吗?我解决了。我刚刚在telnet连接后添加了指令:“exit 5”,一旦脚本返回,它就会退出脚本。@WiloMaldonado太棒了!感谢您告知我们解决方案。:)