johnny bravo
johnny bravo

Reputation: 145

expect write to remote file over ssh

I have a script that is parsing a local file and execute remotely a new file created with the content of previous one.

 Just an example: machine1 with the following command file content:
#cmd1
<blank line here>
#cmd5
hostname -f
reboot`

Now the script will parse that file, will remove blanks and commented lines and create REMOTELY a new file with the new content:

proc _command {fh} {
    set fd [open "$fh" r]
    #set fp [open "/tmp/script_${fh}" w+]
    while { [gets $fd data] >= 0 } {
            if { [string length $data] > 0 } {
                    #skip commented & blank lines
                    if {[string match "#*" $data] || [string match "" $data]} {
                            continue
                    }
                    #puts $fp "$data"
                    send  "$data\r"
                    #send [exec echo $data >>/tmp/1.txt]
            }
    }
                    #close $fp
}
...
   spawn -noecho ssh -i $home/.ssh/.id_rsa -q -o StrictHostKeyChecking=no $user@$server
                    expect {
                            -re "($prompt)" {
                                            send "sudo su -\r"
                                            expect {
                                                    -re "# " {
                                                            _command $cfile
                                                            send "exit\r"

Well, for now the part of the procedure that is writing to the file is commented as every time when I execute the script the file is created locally and not on remote machine.

It's something that I'm missing but can't figure out what...

Upvotes: 0

Views: 308

Answers (1)

glenn jackman
glenn jackman

Reputation: 246764

Do you really need expect for this? You're already using private key authentication, so I think all you really need is:

sed -e '/^$/d' -e '/^#/d' local_file | ssh user@host sudo sh -c 'cat > remote_file'

Upvotes: 1

Related Questions