buffer - Using an expect Script to send the output of a command and store in a file -


hi trying store output of command run through spawn ssh remote window local host, new expect , not able figure out wrong. code:

#!/bin/bash  while read line         /usr/bin/expect <<eod         spawn ssh mininet@$line         expect "assword:"         send -- "mininet\r"         set output [open "outputfile.txt" "a+"]         expect  "mininet@mininet-vm:*"         send -- "ls\r"         set outcome $expect_out(buffer)         send "\r"         puts $output "$outcome"         close $output         expect  "mininet@mininet-vm:*"         send -- "exit\r"         interact         expect eof eod done <read_ip.txt 

i getting error

 expect: spawn id exp6 not open     while executing "expect  "mininet@mininet-vm:*"" 

please can body me on code.

you have expect program in shell heredoc. shell expand variables in heredoc before launching expect. have protect expect's variables shell.

one way use 'quoted' heredoc, , pass shell variable expect through environment:

#!/bin/bash export host                            ## environment variable while read host     /usr/bin/expect <<'eod'            ## note quotes here         spawn ssh mininet@$env(host)   ## value environment         expect "assword:"         send -- "mininet\r"         set output [open "outputfile.txt" "a+"]         expect  "mininet@mininet-vm:*"         send -- "ls\r"         set outcome $expect_out(buffer)         send "\r"         puts $output "$outcome"         close $output         expect  "mininet@mininet-vm:*"         send -- "exit\r"         expect eof                 ## don't want both "interact" , "expect eof" eod done <read_ip.txt 

putting single quotes around heredoc terminator means whole heredoc acts single quoted string, , expect's variables left expect handle.

you might investigate expect log_file command: can enable , disable logging @ will, doing manually here.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -