I am writing a simple expect script for accessing to a remote server without introducing the password with the command window.
I am able to access to the remote server, and there I want to modify a text file. This modification is performed with the ‘sed‘ command for finding a ‘keyword’, the line where this ‘keyword’ is located is deleted.
The part of the code that is giving me problems is the combination of ‘spawn‘ with ‘sed‘ is the following one
spawn sed -i -e '/keyword_to_delete/d' /home/my_file
Since this is doing in a expec file I have to do it through ‘spawn’ command. I switch on the debug info mode and the main error info in the screen is :
expect: set expect_out(buffer) "exit\r\nsed: -e expression #1, char 1: unknown command: `''\r\n"
cannot interact with self - set spawn_id to a spawned process
I hope I have show the problem here clear and in a nice way, if not tell me and I will try to do it better.
Thanks so 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 you sure you 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 itself terminates
interact
And the current error is : “sed: couldn’t open temporary file /etc//sedDEb2Fx: Permission denied”
Again I’m not an expert of
expect, but your script looks to swawnchmodandsedon your local machine, not on your remote machine. Shouldn’t you usesend chmod 775 ...instead, just like you’re usingsend exitto executeexiton your remote machine?This is how
sed -i ...works. You will notice that you need a write permission on/etcwhere a temp file will be created. To avoid it you need to explicitly create a temp file by not using-i, eg.sed -e /foo/d /etc/hosts > /tmp/hosts; mv /tmp/hosts /etc. Maybe you needcpinstead ofmv, but I’m not sure… I’m too lazy to trystrace mv ...by myself.