- 论坛徽章:
- 18
|
30可用积分
本帖最后由 bikkuri 于 2015-04-28 22:43 编辑
大家好!
我有一个问题向大家请教。
我写了一个脚本,通过expect连到一些服务器上去执行一条命令,然后对命令的输出进行处理。
这个脚本的expect子程序如下:- run_sam_cmd()
- {
- samitem=$1
- ip=$(echo "$sam"|awk -vs=$samitem '{if($1==s)print$2}')
- user=$(echo "$sam"|awk -vs=$samitem '{if($1==s)print$3}')
- pass=$(echo "$sam"|awk -vs=$samitem '{if($1==s)print$4}')
- # cmd='[ $(grep -c samadmin /etc/passwd) -gt 0 ] && su - samadmin -c \"/opt/5620sam/server/nms/bin/nmsserver.bash nms_status\"'
- cmd='if \[ `grep -c samadmin /etc/passwd` -gt 0 \] ; then su - samadmin -c \"/opt/5620sam/server/nms/bin/nmsserver.bash nms_status\"; fi'
- [ -z $user ] && user="root"
- [ -z $pass ] && pass="password"
- sed -i "/$ip/d" ~/.ssh/known_hosts
- expect -c "
- set timeout 1000;
- log_file /var/www/html/lab/sam.log
- spawn /usr/bin/ssh -l $user $ip
- expect {
- \"yes/no\" {send \"yes\r\"; exp_continue}
- \"assword\" {send \"$pass\r\";}
- }
- expect {
- \"assword\" {send \"$user\r\";exp_continue}
- \"#\" {send \"\r\";}
- }
- expect \"#\"
- send \"date\r\"
- expect \"#\"
- send \"$cmd\r\"
- expect \"#\"
- send \"exit\r\r\"
- "
- }
复制代码 这个程序在命令行手工执行是没有问题的,可以得到正常的输出。
但是当我把它放在crontab里每个小时执行一次的时候,发现有的服务器可以正常得到命令的执行结果,但是有的服务器却得不到命令的执行结果。
检查log发现,远程服务器可以正常登录,但是在输入命令的时候,不知道为什么命令输到一半就被截断了,导致命令没有被正确执行。
以下是部分log内容:- spawn ssh -l root 135.240.146.161^M
- The authenticity of host '135.240.146.161 (135.240.146.161)' can't be established.^M
- RSA key fingerprint is bb:db:aa:19:04:26:3c:98:7a:16:33:9d:f5:38:5b:27.^M
- Are you sure you want to continue connecting (yes/no)? yes^M
- Warning: Permanently added '135.240.146.161' (RSA) to the list of known hosts.^M^M
- root@135.240.146.161's password: ^M
- Last login: Mon Apr 27 22:03:27 2015 from 135.240.146.162^M^M
- [^[[1;31mroot^[[32m@hp7:^[[33m~^[[m]# ^M
- [^[[1;31mroot^[[32m@hp7:^[[33m~^[[m]# if [ `grep -c samadmin /etc/passwd` -gt 0 ]^M<~^[[m]# if [ `grep -c samadmin /etc/passwd` -gt 0 ] ^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H; thspawn ssh -l root 135.240.146.162^M
- The authenticity of host '135.240.146.162 (135.240.146.162)' can't be established.^M
- RSA key fingerprint is 1f:73:58:91:44:db:02:7f:b2:4c:04:b7:a9:9e:fd:9e.^M
- Are you sure you want to continue connecting (yes/no)? yes^M
- Warning: Permanently added '135.240.146.162' (RSA) to the list of known hosts.^M^M
- root@135.240.146.162's password: ^M
- Last login: Mon Apr 27 22:03:29 2015 from 135.240.146.162^M^M
- [^[[1;31mroot^[[32m@hp8:^[[33m~^[[m]# ^M
- [^[[1;31mroot^[[32m@hp8:^[[33m~^[[m]# if [ `grep -c samadmin /etc/passwd` -gt 0 ]^M<~^[[m]# if [ `grep -c samadmin /etc/passwd` -gt 0 ] ^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H; t
复制代码 大家帮我分析一下为什么会出现这种情况呢?
这一大串的^H是怎么回事啊?
为什么只在crontab里会出现这种情况,在命令行手工执行就没问题呢?
作为对比,以下是在命令行手工执行expect脚本情况下的log文件内容:- spawn ssh -l root 135.240.146.161^M
- The authenticity of host '135.240.146.161 (135.240.146.161)' can't be established.^M
- RSA key fingerprint is bb:db:aa:19:04:26:3c:98:7a:16:33:9d:f5:38:5b:27.^M
- Are you sure you want to continue connecting (yes/no)? yes^M
- Warning: Permanently added '135.240.146.161' (RSA) to the list of known hosts.^M^M
- root@135.240.146.161's password: ^M
- Last login: Mon Apr 27 23:03:10 2015 from 135.240.146.162^M^M
- ^[]0;root@hp7:~^G^[[?1034h[^[[1;31mroot^[[32m@hp7:^[[33m~^[[m]# ^M
- ^[]0;root@hp7:~^G[^[[1;31mroot^[[32m@hp7:^[[33m~^[[m]# if [ `grep -c samadmin /etc/passwd` -gt 0 ] ; then su - samadmin -c "/opt/5620sam/server/nms/bin/nmsserver.bash nms_stat ^Mus" ; fi^M
- 5620 Main Server Information^M
- -- Build Information^M
- -- 5620 SAM Version 12.0.R5.0 - Built on Tue Sep 23 19:06:30 EDT 2014^M
- -- System Information^M
- -- Host/IP: 135.240.146.161^M
- -- Startup time: Fri Apr 03 00:07:45 CST 2015^M
- -- Current time: Mon Apr 27 23:55:07 CST 2015^M
- -- Up time: 24d 23:47:21.833^M
- ......
- ^[]0;root@hp7:~^G[^[[1;31mroot^[[32m@hp7:^[[33m~^[[m]# spawn ssh -l root 135.240.146.162^M
- The authenticity of host '135.240.146.162 (135.240.146.162)' can't be established.^M
- RSA key fingerprint is 1f:73:58:91:44:db:02:7f:b2:4c:04:b7:a9:9e:fd:9e.^M
- Are you sure you want to continue connecting (yes/no)? yes^M
- Warning: Permanently added '135.240.146.162' (RSA) to the list of known hosts.^M^M
- root@135.240.146.162's password: ^M
- Last login: Mon Apr 27 23:03:12 2015 from 135.240.146.162^M^M
- ^[]0;root@hp8:~^G^[[?1034h[^[[1;31mroot^[[32m@hp8:^[[33m~^[[m]# ^M
- ^[]0;root@hp8:~^G[^[[1;31mroot^[[32m@hp8:^[[33m~^[[m]# if [ `grep -c samadmin /etc/passwd` -gt 0 ] ; then su - samadmin -c "/opt/5620sam/server/nms/bin/nmsserver.bash nms_stat ^Mus" ; fi^M
- su: warning: cannot change directory to /opt/5620sam/auxserver: No such file or directory^M
- -bash: /opt/5620sam/server/nms/bin/nmsserver.bash: No such file or directory^M
- ^[]0;root@hp8:~^G[^[[1;31mroot^[[32m@hp8:^[[33m~^[[m]#
复制代码 可以看到在命令行手工执行expect脚本的情况下,命令可以正常被输入,并得到命令执行的结果。
为什么会有这种差异呢?
从两个log的对比中可以看出在手工运行时,命令被完整输入;而在crontab运行时,命令被一大帮不明来历的^H君截断了。
- [box@ruizi:~/sam]$ grep -A2 date sam_cli.log
- [root@cat1:/]#date^M
- Tue Apr 28 12:07:59 BST 2015^M
- [root@cat1:/]#if [ `grep -c samadmin /etc/passwd` -gt 0 ] ; then su - samadmin -c "/opt/5620sam/server/nms/bin/nmsserver.bash nms_status"; fi^M
- --
- [root@cat2:/]#date^M
- Tue Apr 28 12:23:16 GMT 2015^M
- [root@cat2:/]#if [ `grep -c samadmin /etc/passwd` -gt 0 ] ; then su - samadmin -c "/opt/5620sam/server/nms/bin/nmsserver.bash nms_status"; fi^M
- --
- [root@cat3:/]#date^M
- Tue Apr 28 12:28:29 GMT 2015^M
- [root@cat3:/]#if [ `grep -c samadmin /etc/passwd` -gt 0 ] ; then su - samadmin -c "/opt/5620sam/server/nms/bin/nmsserver.bash nms_status"; fi^M
- --
- [root@cat4:/]#date^M
- Tue Apr 28 12:25:43 GMT 2015^M
- [root@cat4:/]#if [ `grep -c samadmin /etc/passwd` -gt 0 ] ; then su - samadmin -c "/opt/5620sam/server/nms/bin/nmsserver.bash nms_status"; fi^M
- --
- ......
- [box@ruizi:~/sam]$ grep -A2 date sam_crontab.log
- [root@cat1:/]#date^M
- Tue Apr 28 12:18:25 BST 2015^M
- [root@cat1:/]#if [ `grep -c samadmin /etc/passwd` -gt 0 ] ; then su - samadmi^M< samadmin /etc/passwd` -gt 0 ] ; then su - samadmin ^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H -c "/opt/5620sam/server/^M< 0 ] ; then su - samadmin -c "/opt/5620sam/server/n ^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^Hms/bin/nmsserver.bash nms^M<-c "/opt/5620sam/server/nms/bin/nmsserver.bash nms_ ^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^Hstatus"; fi^M
- --
- [root@cat2:/]#date^M
- Tue Apr 28 12:33:43 GMT 2015^M
- [root@cat2:/]#if [ `grep -c samadmin /etc/passwd` -gt 0 ] ; then su - samadmi^M< samadmin /etc/passwd` -gt 0 ] ; then su - samadmin ^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H -c "/opt/5620sam/server/^M< 0 ] ; then su - samadmin -c "/opt/5620sam/server/n ^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^Hms/bin/nmsserver.bash nms^M<-c "/opt/5620sam/server/nms/bin/nmsserver.bash nms_ ^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^Hstatus"; fi^M
- --
- [root@cat3:/]#date^M
- Tue Apr 28 12:38:55 GMT 2015^M
- [root@cat3:/]#if [ `grep -c samadmin /etc/passwd` -gt 0 ] ; then su - samadmi^M< samadmin /etc/passwd` -gt 0 ] ; then su - samadmin ^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H -c "/opt/5620sam/server/^M< 0 ] ; then su - samadmin -c "/opt/5620sam/server/n ^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^Hms/bin/nmsserver.bash nms^M<-c "/opt/5620sam/server/nms/bin/nmsserver.bash nms_ ^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^Hstatus"; fi^M
- --
- [root@cat4:/]#date^M
- Tue Apr 28 12:36:09 GMT 2015^M
- [root@cat4:/]#if [ `grep -c samadmin /etc/passwd` -gt 0 ] ; then su - samadmi^M< samadmin /etc/passwd` -gt 0 ] ; then su - samadmin ^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H -c "/opt/5620sam/server/^M< 0 ] ; then su - samadmin -c "/opt/5620sam/server/n ^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^Hms/bin/nmsserver.bash nms^M<-c "/opt/5620sam/server/nms/bin/nmsserver.bash nms_ ^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^Hstatus"; fi^M
- --
- ......
复制代码 附件是crontab运行时以及手工执行时的expect的完整log,请帮我分析一下。
samlog.zip
(14.79 KB, 下载次数: 31)
- [box@ruizi:~/sam]$ l
- 总用量 124
- -rw-r--r-- 1 box box 73413 4月 28 20:17 sam_cli.log
- -rw-r--r-- 1 box box 50851 4月 28 20:28 sam_crontab.log
- [box@ruizi:~/sam]$ wc *
- 1853 11148 73413 sam_cli.log
- 1223 7456 50851 sam_crontab.log
- 3076 18604 124264 总用量
- [box@ruizi:~/sam]$ file *
- sam_cli.log: UTF-8 Unicode English text, with CRLF, CR, LF line terminators, with escape sequences
- sam_crontab.log: UTF-8 Unicode English text, with CRLF, CR, LF line terminators, with escape sequences, with overstriking
- [box@ruizi:~/sam]$
复制代码 谢谢。 |
最佳答案
查看完整内容
估计还是环境的问题你可以在命令行和cron分别set对比一下,看看是不是一样,特别是TERM COLUMNS等变量或者直接在expect里面加 stty -echo 就可以眼不见为净了
|