K
K
ksvdon2014-12-12 00:17:59
linux
ksvdon, 2014-12-12 00:17:59

How to get data from a remote host using expect?

I need to do certain actions on a remote host automatically. For example, I want to see the PID of some process and save it to a variable on the host where I will execute the script. Those. I have car "A". I want to see the PID of some process on machine "B" using ps -e | grep some_process | awk {'print $1'} and store the resulting value in a variable for further processing. You can perform actions on a remote host like this:

expect -c 'spawn ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null [email protected] "ps -e | grep some_process | awk {'print $1'}"; expect password; send "12345\n"; interact'

but how to save the received data for further processing on my host? So that in the same script I could operate on the data received after executing the command on a remote host. Here's a dumb idea - let's say I save the output of a command to a file on a remote host, then copy that file to my host where the script is being executed using scp, and then extract the data from the resulting file. But I think it's too much of an asshole. How to do it like a human?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Official account of the Timeweb team, 2020-09-15
@timeweb_team

in this case, you can generally refuse to use expect and use sshpass (you may need to install it separately) and in the script you can add the output of the command you need to a variable, for example:
a=$(sshpass -p '' ssh -oStrictHostKeyChecking=no [email protected] \ ps -e | grep some_process | awk {'print $1'})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question