2

I wrote the following expect script in order to replace the IP address on remote Linux machine

I use a perl one-liner for this task

I get errors about can't read "HOME": no such variable ,

Please advise on what I need to change in my expect script so I can change the requested IP ?

 #!/bin/ksh


 expect_transfer=`cat << EOF
 set timeout -1
 spawn  ssh  12.219.102.43
       expect {
                 ")?"   { send "yes\r"  ; exp_continue  }

                 word:  {send pass123\r}
              }
  expect #  {send "export OLD=10.10.10.10 ; export NEW=1.1.1.1 ; perl -i -pe 's/\Q$ENV{OLD}\E/$1$ENV{NEW}$2/' /etc/hosts\r"}
  expect #    {send exit\r}
  expect eof
  EOF`


  expect -c  "$expect_transfer" 

results:

  spawn ssh 12.219.102.43
  root@12.219.102.43's password: 
  Last login: Sun Aug  4 12:29:25 2013 from 12.219.102.43
  [root@localhost ~]# can't read "HOME": no such variable
  while executing
   "send "export OLD=10.10.10.10 ; export NEW=1.1.1.1 ; perl -i -pe 's/\Q$HOME/.kshrc{OLD}\E/$HOME/.kshrc{NEW}/' /etc/hosts\r""
  invoked from within
  "expect #  {send "export OLD=10.10.10.10 ; export NEW=1.1.1.1 ; perl -i -pe 's/\Q$HOME/.kshrc{OLD}\E/$HOME/.kshrc{NEW}/' /etc/hosts"
  • I will happy to get any other solution under ksh scripts
2

Apparently $ENV is expanded to $HOME/.kshrc in

send "export OLD=10.10.10.10 ; export NEW=1.1.1.1 ; perl -i -pe 's/\Q$HOME/.kshrc{OLD}\E/$HOME/.kshrc{NEW}/' /etc/hosts\r

You could

  1. try $OLD and $NEW in the Perl line,

  2. ditch expect in favor of plain ssh: ssh root@12.219.102.43 -- sed -i s/$OLD/$NEW/ /etc/hosts

    With a proper keys setup, you don't need a password either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.