shell脚本实现ssh登录之后的while循环

脚本功能:ssh远程登录某服务器后查看某个端口是否开启,如果开启就退出,没有的话就打印语句,然后瑞出ssh登录

说明:

远程执行的内容在“<< testtt ” 至“ testtt”之间,在远程机器上的操作就位于其中,注意的点:

1.<< testtt,ssh后直到遇到testtt这样的内容结束,testtt可以随便修改成其他形式。
2. > /dev/null 2>&1 重定向目的在于不显示远程的输出了
3.在结束前,加exit退出远程节点
 

脚本如下:

#!/bin/bash
DEPEND_HOST=outwitcom05
ssh root@$DEPEND_HOST > /dev/null 2>&1 << testtt
while [[ "netstat -tnlp|grep 1111" == "" ]]
do
  echo "222222"
done
exit
testtt
echo "11111111"


输出为 :11111111

注意事项:

1.就算以上内容在一个自定义函数中,结束标志 testtt 也必须顶格写,不然会报以下错误:

./gateway.bash: line 95: warning: here-document at line 32 delimited by end-of-file (wanted `testtt')
./gateway.bash: line 96: syntax error: unexpected end of file

     错误表明:testtt前后不能有空格或制表符

2. 上述脚本中 "netstat -tnlp|grep 1111" == "" 必须不成立,不然就是一个死循环脚本

你可能感兴趣的:(shell脚本编写)