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 ?


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

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

  expect -c  "$expect_transfer" 


  spawn ssh
  root@'s password: 
  Last login: Sun Aug  4 12:29:25 2013 from
  [root@localhost ~]# can't read "HOME": no such variable
  while executing
   "send "export OLD= ; export NEW= ; perl -i -pe 's/\Q$HOME/.kshrc{OLD}\E/$HOME/.kshrc{NEW}/' /etc/hosts\r""
  invoked from within
  "expect #  {send "export OLD= ; export NEW= ; 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

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

send "export OLD= ; export NEW= ; 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@ -- 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.