0

I'm trying to write my first expect script which will push new language to a remote file.

This is what I have so far that isn't really working:

#!/usr/bin/expect

set fid1 [open ./hosts.list r]
set hosts [read -nonewline $fid1]
close $fid1

set banner_cmd "cat > /tmp/new_sshd_banner << EOF


<New Language Goes Here>


EOF"
send "\n"

stty -echo
send_user "Enter password for remote sudo: "
expect_user -re "(.*)\n"
stty echo
set pass $expect_out(1,string)
send "\n"

foreach host [split $hosts "\n"] {
    eval spawn "/usr/bin/ssh $host"
    expect {
        -re "RSA key fingerprint" {send "yes\r"}
        timeout {puts "Host is known"}
    }

    expect "$host"
    send "sudo mv /etc/file /etc/file.orig"
    expect "assword"
    send $pass

    expect "$host"
    send "sudo $file_cmd"

    expect "$host"
    send "sudo mv /tmp/file /etc/file

}

When it runs, this is what I see:

opensuse @ 15:10 ~/bin> ./fix_file.exp 

Enter password for remote sudo: 
spawn /usr/bin/ssh server

<New Language Goes Here>


Last login: Fri Aug 31 19:52:45 2012 from 10.152.81.105
[user@server ~]$ Host is known
sudo cat > /tmp/new_file << EOF 
> 
> 
> <New Language Goes Here> 
> 
> EOFsudo mv /etc/file /etc/filePASSWORD*missing "
    while executing
"send "sudo mv /tmp/file /etc/file

"
    ("foreach" body line 17)
    invoked from within
"foreach host [split $hosts "\n"] {
    eval spawn "/usr/bin/ssh $host"
    expect {
        -re "RSA key fingerprint" {send "yes\r"}
        timeout {puts "Host i..."
    (file "./fix_file.exp" line 45)

One thing that isn't readily apparent is that it tacks the password obtained from the user into the output of the mv command.

Where have I gone wrong with this?

Glenn
  • 822
  • 6
  • 9
theillien
  • 1,189
  • 3
  • 19
  • 33

1 Answers1

1

Exactly what the error says:

missing "
    while executing
"send "sudo mv /tmp/file /etc/file

You're missing the closing " after the line:

    send "sudo mv /tmp/file /etc/file
Glenn
  • 822
  • 6
  • 9
  • Cripes. How did I miss that? /facepalm – theillien Sep 04 '12 at 21:34
  • And now that that is fixed, I can see what the rest of it is doing such as appending the password to one of the filenames. It's actually just echoing the send command's arguments to the screen. Unfortunately, it isn't actually performing the action I'm trying to do. The language isn't being entered into the file. – theillien Sep 04 '12 at 21:39
  • 1
    @theillien You might be better off creating the file locally and doing a `spawn scp $localfile ${host}:/tmp/new_sshd_banner` to transfer it (with stuff to handle passwords as necessary). More generally though, I'd write a procedure to do the `send sudo ...` so that you can handle the _optional_ requests for a password without having to write it out in full each time. It's good to only debug things once. :-) – Donal Fellows Sep 05 '12 at 08:01
  • I did end up scp'ing the file over to each server and will now attempt to run a sudo to put it in place. We'll see how that goes. – theillien Sep 05 '12 at 22:19
  • Still not working when simply trying to move a file. It actually logs in and puts me at the prompt rather than sending the sudo command. – theillien Sep 05 '12 at 22:41
  • I'm getting closer. I found I needed to escape a semi-colon. Now I need to figure out how to add "< /dev/tty" to my ssh command. – theillien Sep 05 '12 at 22:47