1

on the remote machine there is one script ./bin/show_info.py which I want to execute and get the output on the local machine, for which i have created a script with expect.

If I run the below command it works properly, but when the same in mentioned in the script it gives error.

ssh IPaddress "./bin/show_info.py |  grep  total -A2 | grep -v ^+ | grep -v ^-  | sed 's/total.*//g'" > Report_2017.txt

script error

[root@centseven ~]# ./directconnect
spawn echo -e > Report_2017.txt
extra characters after close-quote
    while executing
"spawn sh -c "ssh IPaddress "./bin/show_info.py | grep total -A2 | grep -v ^+ | grep -v ^- | sed 's/total.*//g'" > Report_2017.tx..."
    (file "./directconnect" line 6)

file content

#!/usr/bin/env expect
set timeout 5

spawn echo -e > Report_2017.txt

spawn sh -c "ssh IPaddress "./bin/show_info.py | grep total -A2 | grep -v ^+ | grep -v ^- | sed 's/total.*//g'" > Report_2017.txt"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "12345\r"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "12345\r"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "12345\r"
expect "Password:"
send "54321\r"
interact
1

The double quotes are a problem in your case in the expect script.

spawn sh -c "ssh IPaddress "./bin/show_info.py |.. sed 's/total.*//g'" > Report_2017.txt"

Just observe that due to the double quotes ending after IPaddress, the pipe is left exposed.

You may want to try this:

spawn sh -c "ssh IPaddress \"./bin/show_info.py |.. sed 's/total.*//g'\" > Report_2017.txt"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.