Chinaunix
标题:
expect在执行命令返回结果中,有时候只返回一些结果,并卡住
[打印本页]
作者:
xjxh829
时间:
2013-12-04 19:51
标题:
expect在执行命令返回结果中,有时候只返回一些结果,并卡住
在bash中调用expect登录远程主机执行命令。发现expect在执行命令find时,有时候返回正常结果,有时候只返回一些结果,并卡住,然后就不退出。
登录的是同一个远程主机。同一时间执行脚本10次,基本上有5次是卡住的。
将 |xargs -I {} last -f {} 改为 -exec last -f \; 也是一样的情况。请指教下原因。谢谢。
expect <<EOF
set timeout -1
log_file ${tmpdir}/${atype}_tmp.txt
spawn ssh ${user}@${ip}
expect {
"yes/no" { send "yes\r";exp_continue}
"assword" { send "${pw}\r"}
}
expect "#"
send "touch -t ${last_time} last_time.txt \r"
expect "#"
send "find /var/log -type f -name \"wtmp*\" -newer \"last_time.txt\" |xargs -I {} last -f {} \r"
expect "#"
send " rm -f last_time.txt \r"
expect "#"
send "exit\r"
expect eof
EOF
作者:
itfly3
时间:
2013-12-05 10:00
SLES11_SP3:~/shell/expect # cat ssh.sh
#!/bin/bash
tmpdir=/tmp
atype=expect_f
pw=suse
user=root
ip="192.168.10.12"
last_time="201301010101"
expect <<EOF
set timeout -1
log_file ${tmpdir}/${atype}_tmp.txt
spawn ssh ${user}@${ip}
expect {
"yes/no" { send "yes\r";exp_continue}
"assword" { send "${pw}\r"}
}
expect "#"
send "touch -t ${last_time} last_time.txt \r"
expect "#"
# send "find /var/log -type f -name \"wtmp*\" -newer \"last_time.txt\" |xargs last -f \r"
send "find /var/log -type f -name \"wtmp*\" -newer \"last_time.txt\" |xargs -I {} last -f {}\r"
expect "#"
send " rm -f last_time.txt \r"
expect "#"
send "exit\r"
expect eof
EOF
作者:
itfly3
时间:
2013-12-05 10:05
我用你这个测试时,执行倒是没问题,,xargs -I...改成个last -f更好看点
唯一遇到的问题是: 连续多次执行,就报错
SLES11_SP3:~/shell/expect # ./ssh.sh
spawn ssh
root@192.168.10.12
ssh: connect to host 192.168.10.12 port 22: Cannot assign requested address
expect: spawn id exp5 not open
while executing
"expect "#""
这个是端口不够用,很多端口都处于TIME_WAIT阶段,
如过是这个问题,你可以先看下端口范围
SLES11_SP3:~/shell/expect # sysctl -a |grep ip_local_port_range
net.ipv4.ip_local_port_range = 9000 9001
小的话,就在/etc/sysctl.conf后面添加,扩大后的端口范围
net.ipv4.ip_local_port_range = 9000 30000
保存退出
执行sysctl -p后再试试
作者:
xjxh829
时间:
2013-12-05 10:26
回复
3#
itfly3
你脚本显示没有登录远程主机。我的是登录上远程主机,并find执行命令返回了部分结果,然后卡住不动,也不退出主机。两者情况不一样。
你的脚本返回多少结果?我成功执行的话,完整返回180行;只返回部分结果,有时候就返回100行就不动了。
作者:
waker
时间:
2013-12-05 10:26
1. 加上 exp_internal 1 开启调试自己观察一下
2. 通常expect "#" 会写成 expect "# "
3.send "find /var/log -type f -name \"wtmp*\" -newer \"last_time.txt\" |xargs -I {} last -f {} \r"
改成
send {find /var/log -type f -name "wtmp*" -newer "last_time.txt" |xargs -I {} last -f {}}
send " \r"
作者:
itfly3
时间:
2013-12-05 10:55
我用200个文件作为目标文件,,for执行脚本10次也是没问题
返回信息就是200个
不知道你那是什么情况了
你可以把那个脚本内容复制下,粘贴到一个新的文件中,,有时可能在编辑时格式有些不对
作者:
xjxh829
时间:
2013-12-05 12:38
回复
6#
itfly3
我是直接编辑,所以不是有乱码问题。并且有时候是能执行成功的。
作者:
xjxh829
时间:
2013-12-05 12:48
回复
5#
waker
都尝试了,还是不行。使用 -d 还是 exp_internal 1 进行调试时,发现貌似卡在expect_out(buffer)。我设置 set match_max 500000,还是不行。运行结果如下
..................
root pts/0 1.1.1.1 Mon Nov 21 17:10 - 17:12 ( 00:01)
root pts/0 1.1.1.2
expect: does “2 (01:50) \r\n root pts/0 1.1.1.1 Mon Nov 21 17:00 - 17:02 ( 00:01) \r\n root pts/0 1.1.1.1 Mon Nov 21 17:01 - 17:02 ( 00:01)
................
(spawn_id exp5) match glob pattern "# "? no
expect: set expect_out(spawn_id) "exp5"
expect: set expect_out(buffer) " Nov 21 17:03 - 17:04 ( 00:01) \r\n root pts/0 1.1.1.1 Mon Nov 21 17:04 - 17:05 ( 00:01) \r\n
...............
root pts/0 1.1.1.1 Mon Nov 21 17:00 - 17:02 ( 00:01) \r\n"
---------------------------然后卡住就不动了。
作者:
waker
时间:
2013-12-05 13:58
expect没有等到"# "
建议你先不要xargs 看看find的结果有什么不同
作者:
xjxh829
时间:
2013-12-05 14:47
回复
9#
waker
使用 -exec last -f {} \; 或者重定向到一个文件再cat出来,情况依然如此。
作者:
xjxh829
时间:
2013-12-08 22:43
....大家有新建议吗?
作者:
shintoky
时间:
2013-12-09 09:37
回复
11#
xjxh829
把 find 命令作为变量使用试试?
set command "find ....."
...
send "$command"
作者:
xjxh829
时间:
2013-12-10 00:31
回复
12#
shintoky
没有结果。测试失败。
作者:
fufelixzh
时间:
2014-01-09 15:55
回复
1#
xjxh829
用interpreter 调试一下
然后把log贴出来。
在调用find之前,先将exect_out(bffer)清空
如果是find的少的话,可能是中间有#出现提前匹配结束了
作者:
xjxh829
时间:
2014-05-28 17:42
回复
14#
fufelixzh
怎么使用这个呢
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2