Script to login to another server

Folks;
I'm trying to write a shell script that should runs on one machine and to perform the following:

  • The script should SSH to another machine using user name and password then runs a command on that machine then exit and bring the command output to the screen.

Thanks in advance for the help

  1. For logging in use, passwordless login or using expect give the password.

  2. use -c to execute a command.

  3. Normally it will show the output in the screen, and exits.

ssh username@hostname ls

This will execute ls in that host, shows the output, and exits.

use rsh

Thanks for replying folks;

  • I must use SSH.

  • I don't see from the answer where i can add the password for the SSH inside the script. the script should be able to login to the new machine using user and password.

  • i'd like the user to pass in the IP address for the machine we need to SSH to it as it will change every time.

  • Also, i need the user to get back the output from running the command inside the script

Thanks again for your help

Create public SSH keys on all remote machines then add those keys to the authorized_keys2 file on the host you intend to run your script. This will allow for passwordless SSH connection.

You could assign a variable the output by running your SSH command in backticks.

ssh_output=`ssh user@host command`