1

I want to create a bash script that will login to all the linux servers in my network using ssh and collect the output of 'uptime' command to a local file. There is no keypair installed between these local server and the remote servers. So I need to give the password (username and password is same for all the remote servers) in the script itself. I know this is not a secure way to do it, but it is just for learning purpose. I see 'expect' command can be used for the ssh login with password but confused how to use it together with the 'uptime' command that provide the server status. So my requirement is

1. I have local server test1 which contains a text file 'server_status.txt' 
2. I need a script in test1 that will try to login to all the remote servers (say 192.168.0.1 to 192.168.0.50) using the same username and password. It will execute the command 'uptime' once logged in to the remote servers and store the output to the local file 'server_status.txt'
Cyrus
  • 84,225
  • 14
  • 89
  • 153
theG
  • 150
  • 1
  • 4
  • 10
  • possible duplicate of [Use expect in bash script to provide password to SSH command](http://stackoverflow.com/questions/4780893/use-expect-in-bash-script-to-provide-password-to-ssh-command) – Cyrus Jan 14 '15 at 11:04
  • If you are just testing then just do key based auth. Expect really should only be used as a last ditch effort. I've used it with vendors who don't support key based auth but also only support SFTP. Its bad for business all around to get into this habit. – mmmmmpie Jan 21 '15 at 13:34

2 Answers2

0

REVOKE: paste your public key into the server's /path2userthatshouldlogon/.ssh/authorized_keys and run the your commands remotely using ssh user@host commandtoexecute

due to connection wanted to be established without key. UPDATE: have a look at sshpass if you really want to need passwords, which is NOT RECOMMENDED

Community
  • 1
  • 1
Marc Bredt
  • 905
  • 5
  • 13
0

Note: Doing this is poor practice. If you are testing around with this then you are learning a bad habit. Don't do this in production on servers you care about.

You'll want to execute the expect call as a $? and be sure to store the $USER and $SERVER variables or just replace them:

uptime=$(expect -c 'spawn ssh $USER@$SERVER send "uptime"; exit;')
echo $uptime
mmmmmpie
  • 2,908
  • 1
  • 18
  • 26