在期望脚本中,我可以设置任何命令或字符在远程机器上运行它但可悲的是,expect无法发送与预期脚本中定义的字符相同的字符
例如
我想运行expect脚本中的这一行,以便将IP地址从10.10.10.10更改为至1.1.1.1
expect # {send "perl -i -pe 's/Q10.10.10.10E/1.1.1.1/' /etc/hostsr"}
但当我运行预期屏幕时,我实际上看到控制台上运行着这行:
[root@localhost ~]# perl -i -pe 's/Q10.10.10.10E/1.1.1.1/' /etc/hosts
请注意,Q和E之前的反斜杠是消失的
所以我想知道如何从预期的剧本中逃脱这些角色?
因此expect将在控制台上运行与以下相同的行
[root@localhost ~]# perl -i -pe 's/Q10.10.10.10E/1.1.1.1/' /etc/hosts
- 备注在反斜杠之前设置反斜杠"\"没有帮助
我的脚本:
#!/bin/ksh
#
expect=`cat << EOF
set timeout -1
spawn ssh 192.9.200.10
expect {
")?" { send "yesr" ; exp_continue }
word: {send secret1r}
}
expect # {send "perl -i -pe 's/\Q10.10.10.10\E/1.1.1.1/' /etc/hostsr"}
expect # {send exitr}
expect eof
EOF`
expect -c "$expect"
结果(在我运行脚本后:)
spawn ssh 192.9.200.10
root@'192.9.200.10 s password:
Last login: Sun Aug 4 22:46:53 2013 from 192.9.200.10
[root@localhost ~]# perl -i -pe 's/Q10.10.10.10E/1.1.1.1/' /etc/hosts
[root@localhost ~]# exit
logout
Connection to 192.9.200.10 closed.
使用不同的Tcl引号将起的作用
expect # {
# send text verbatim here
send {perl -i -pe 's/Q10.10.10.10E/1.1.1.1/' /etc/hosts}
# interpret backslash sequence as carriage return here
send "r"
}
要么用转义,要么把整个东西放在
{}
中
expect # {send "perl -i -pe 's/\Q10.10.10.10\E/1.1.1.1/' /etc/hostsr"}
(用{}
封装整个内容会将r
作为这2个字符发送,而不是作为行终止符,因此在这里不合适。)
请参阅有关Tcl语法的手册页面
还有一个注意事项:
您可以对Tcl做同样的事情,只要您不通过SSH 发送命令