在UNIX脚本中捕获SSH异常

时间:2014-12-12 00:28:23

标签: shell unix ssh openssh

我的服务器中有后台进程,经常更新我的〜/ .ssh / authorized_keys。如果我从客户端机器ssh,它将失败

$ ssh my_server date SSH版本:OpenSSH_5.3p1 user @ my_server的密码

并且经过多次尝试后,ssh会将脚本标记为失败。

我希望在发生ssh故障时,将其中的异常处理片段放入休息状态30秒。

这样的东西
             *ssh -o 'StrictHostKeyChecking no' appsrvr01.myserv.com "date" 2> /tmp/error
             if [ $? -ne 0 ]
             then
               echo -e "\n Please wait..\n\n"
               sleep 1s
             else
               echo -e "\n The Environment is ready to use!\n\n"
               exit 0
             fi*

是否有更好的方法,因为上面的代码片段仍会提示输入密码

1 个答案:

答案 0 :(得分:2)

也许你可以在一个shell脚本中通过" flock"在锁定文件上,然后" flock"在上面运行的shell脚本中:

在更新授权密钥的脚本中:

   (flock 200
   # commands here that modify your authorized_keys file
   ) 200>/tmp/authkey_lock

围绕上面发布的剧本文章:

   (flock 200
   ssh -o 'StrictHostKeyChecking no' appsrvr01.myserv.com "date" 2> /tmp/error
   if [ $? -ne 0 ]
   then
     echo -e "\n Please wait..\n\n"
     sleep 1s
   else
     echo -e "\n The Environment is ready to use!\n\n"
     exit 0
   fi
   ) 200>/tmp/authkey_lock

请参阅" man flock"有关鸡群的信息。