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.
expect
as you are here. You can do everything with just a vanillassh
and a one-liner perl script. – slm Aug 04 '13 at 18:13