问题
in expect script I can set any command or character to run it on remote machine but the sad thing is that expect cant send the same character as they defined in the expect script
for example
I want to run this line from expect script in order to change IP address from 10.10.10.10 to 1.1.1.1
expect # {send "perl -i -pe 's/\Q10.10.10.10\E/1.1.1.1/' /etc/hosts\r"}
but when I run the expect screen actually I see this line runing on the console:
[root@localhost ~]# perl -i -pe 's/Q10.10.10.10E/1.1.1.1/' /etc/hosts
pay attention that the backslash before Q and before E was Disappeared
so I wonder hoW to escape those characters from expect script?
so expect will run the same line on the console as following
[root@localhost ~]# perl -i -pe 's/\Q10.10.10.10\E/1.1.1.1/' /etc/hosts
- REMARK set a backslash "\" before backslash doesn’t help!!!
my script:
#!/bin/ksh
#
expect=`cat << EOF
set timeout -1
spawn ssh 192.9.200.10
expect {
")?" { send "yes\r" ; exp_continue }
word: {send secret1\r}
}
expect # {send "perl -i -pe 's/\\Q10.10.10.10\\E/1.1.1.1/' /etc/hosts\r"}
expect # {send exit\r}
expect eof
EOF`
expect -c "$expect"
RESULTS ( after I run my script: )
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.
回答1:
Using different Tcl quotes will work
expect # {
# send text verbatim here
send {perl -i -pe 's/\Q10.10.10.10\E/1.1.1.1/' /etc/hosts}
# interpret backslash sequence as carriage return here
send "\r"
}
回答2:
Either escape it with \
or enclose the whole thing in {}
expect # {send "perl -i -pe 's/\\Q10.10.10.10\\E/1.1.1.1/' /etc/hosts\r"}
(Enclosing the entire thing with {}
would send \r
as this 2 characters, not as line terminator, so not appropriate here.)
See the the manual page about the Tcl Syntax
And an other note:
You could do the same thing with Tcl, as long as you don't send the commands over SSH
来源:https://stackoverflow.com/questions/18044771/how-to-escape-unusual-uniq-characters-from-expect-scripts