简体   繁体   English

如何使用Expect替换远程主机上的IP地址

[英]how to replace IP address on remote host using expect

I write the following expect script in order to replace the IP address on remote linux machine 我编写以下期望脚本以替换远程Linux机器上的IP地址

I use the perl one linear for this task 我将perl用于此任务

I get errors about can't read "HOME": no such variable , 我收到有关无法读取“ HOME”的错误:没有这样的变量,

please advice what I need to change in my expect script so I will change the requested IP ? 请在我的期望脚本中建议我需要更改的内容,以便更改请求的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 我很乐意在ksh脚本下获得任何其他解决方案

Quote the “EOF” (here document marker) in the line where you use it, that is, change 在您使用它的行中引用“ EOF”(此处为文档标记)

expect_transfer=`cat << EOF

into 进入

expect_transfer=`cat << \EOF

or 要么

expect_transfer=`cat << 'EOF'

When you do not quote the here document marker, the shell interpolates variable references like $ENV inside the here document. 当您不引用here文档标记时,shell会在here文档内插变量引用,例如$ ENV。 In your case, ENV='$HOME/.kshrc' so that Perl does not see $ENV{OLD} but $HOME/.kshrc{OLD} which is clearly wrong. 在您的情况下,ENV ='$ HOME / .kshrc',这样Perl不会看到$ ENV {OLD}而是$ HOME / .kshrc {OLD},这显然是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM