Chinaunix
标题:
shell中调用expect传值问题
[打印本页]
作者:
水底游
时间:
2013-08-28 10:36
标题:
shell中调用expect传值问题
[root@localhost expect]# cat f.sh
#!/bin/sh
while read i j
do
expect <<EOF
set ip $i
set password $j
set timeout 3
spawn ssh root@$ip
expect {
"*yes/no" {send "yes\r";exp_continue}
"*Password:" {send "$password\r"}
}
expect eof
EOF
done <ip.txt
复制代码
执行后报错
[root@localhost expect]# ./f.sh
spawn ssh root@
ssh: Could not resolve hostname : Name or service not known
expect: spawn id exp4 not open
while executing
复制代码
预计是$i,$j的值没有传入进去
这个传值要怎么搞?
如果用单个文件直接写明ip的话是可以正常运行的
[root@localhost expect]# cat e.exp
#!/usr/bin/expect -f
set ip 192.168.1.212
set password qwe123
set timeout 10
spawn ssh root@$ip
expect {
"*yes/no" {send "yes\r";exp_continue}
"*Password:" {send "$password\r"}
}
interact
[root@localhost expect]#
复制代码
作者:
guogang225
时间:
2013-08-28 10:51
本帖最后由 guogang225 于 2013-08-28 10:59 编辑
回复
1#
水底游
#!/bin/sh
while read ip password
do
expect <<EOF
set timeout 3
spawn ssh root@"$ip"
expect {
"*yes/no" {send "yes\r";exp_continue}
"*Password:" {send "$password\r"}
}
expect eof
EOF
done <ip.txt
复制代码
作者:
水底游
时间:
2013-08-28 11:08
我的脚本那么写,为什么不行的?参数转了两次不行还是什么原因的?
回复
2#
guogang225
作者:
wxq1090691643
时间:
2013-08-28 14:10
本帖最后由 wxq1090691643 于 2013-08-28 14:12 编辑
#!/bin/sh
while read ip passwd
do
expect <<EOF
set timeout 3
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$passwd\r" }
}
expect eof
EOF
done <ip.txt
复制代码
作者:
guogang225
时间:
2013-08-28 17:46
回复
3#
水底游
set变量在here document里不生效
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2