linux - Executing 'sed' through 'spawn' into a expect script -


i writing simple expect script accessing remote server without introducing password command window.

i able access remote server, , there want modify text file. modification performed 'sed' command finding 'keyword', line 'keyword' located deleted.

the part of code giving me problems combination of 'spawn' 'sed' following one

spawn sed -i -e '/keyword_to_delete/d' /home/my_file 

since doing in expec file have through 'spawn' command. switch on debug info mode , main error info in screen :

expect: set expect_out(buffer) "exit\r\nsed: -e expression #1, char 1: unknown command: `''\r\n" cannot interact self - set spawn_id spawned process 

i hope have show problem here clear , in nice way, if not tell me , try better.

thanks much

code:

 set ip [lindex $argv 0]   set port [lindex $argv 1]   set password <pswd>   proc sendpass {} {  global ip global port global password  spawn "/usr/bin/ssh" -x -p $port root\@$ip "-o stricthostkeychecking no" "-o userknownhostsfile /dev/null"  while {1} {   expect {     "in"                     {break}     "are sure want"     {send "yes\r"}     "assword"                   {send "$password\r"}   } }  spawn chmod 775 /etc/hosts spawn sed -i -e {/operator/d} /etc/hosts spawn sed -i -e {/default/d} /etc/hosts  send "exit\r" expect eof  }; # end proc sendpass  #execute proc sendpass   # script terminates interact 

and current error : "sed: couldn't open temporary file /etc//seddeb2fx: permission denied"

  1. again i'm not expert of expect, script looks swawn chmod , sed on local machine, not on remote machine. shouldn't use send chmod 775 ... instead, you're using send exit execute exit on remote machine?

  2. this how sed -i ... works. notice need write permission on /etc temp file created. avoid need explicitly create temp file not using -i, eg. sed -e /foo/d /etc/hosts > /tmp/hosts; mv /tmp/hosts /etc. maybe need cp instead of mv, i'm not sure... i'm lazy try strace mv ... myself.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -