Using expect to remote SSH and write to a local file

Hi Guys,

So what I am trying to do is :

Host A should do a SSH to Host B to F. Login to the remote host and gather the output of uptime and write to to a file in HostA.

So by the end of the script, HostA should contain a file that contains the uptime output of Host B,C,D,E,F.

Right now Im using expect to login to remote nodes.

The script I have till now is just for 1 node but I can do it for multiple nodes later.

#!/usr/bin/expect -f
set timeout 60
spawn ssh cf315307@HostB

expect "cf315307@HostB's password:"
send "Alpha@123\r"

interact

Till now it works. So how do I send the the uptime command to the server and make sure the output is written to a file on HostA ?

You are having this problem because you are using the third-party expect brute-forcing tool to cram a stored plaintext password into ssh. ssh is designed to prevent you from doing this because this is extremely insecure. If you use passwordless ssh keys as intended, your script will become as easy and simple as ssh username@host uptime > filename

Literally as simple as that.

See passwordless ssh