Link to home
Start Free TrialLog in
Avatar of cfetzer
cfetzer

asked on

How to use SSH within a shell script

Hello all!

I need to use SSH within a shell script and I am running into a bit of an impasse.  Say I have this scenario:

Two Solaris boxes (ServerA and ServerB).  ServerA runs a cron job, which will execute a shell script at a particular time every day.  This shell script needs to contain the code to run SSH, which will connect to ServerB and execute a shell script remotely.

So far, I can do it manually:

$ ssh [host] [command]
(I don't have root access to either box)

I am then prompted for a password, which I supply, and the command executes perfectly.  The accounts that I am using on both boxes share the same name, but have different passwords.  Maybe this is why I am asked to supply a password...

I need to create a shell script that will run a SSH to ServerB, and automatically supply the password that is required..  Could someone assist me?

Thanks!
Chad
Avatar of yuzh
yuzh

You have atleast 2 options to do this job.

The syntax for using ssh to run command in a remote server:

ssh user@remote-box /full-path/program-or-script

Option 1, setup the ssh without password, have a look at the following pages for how to do it:

http://www.cs.ucsd.edu/classes/sp99/cse160/programming/ssh.html

http://www.cs.umd.edu/~arun/misc/ssh.html

Option 2, use "expect" script to handle the password, if you don't know kow to write an expect script, use autoexpect to create one for you.

man autoexpect

to learn more.

In case you don't have expect install on your system, you can download it from:
http://sunfreeware.com/
You should also get tcl-8.4.1 , tk-8.4.1 install from the same site.
information about expect (including script example can be found):

http://expect.nist.gov/

Hope the infor can help.





ASKER CERTIFIED SOLUTION
Avatar of madan1278
madan1278
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Note to above comment: If SSH version 2 is used the name of the authorization file should be authorized_keys2 if OpenSSH is used.
Avatar of cfetzer

ASKER

Thank you madan1278!  This solved my problem exactly and it works perfectly!

Thanks to you too, yuzh, the links are useful and I appreciate them, but madan1278 provided the steps that I needed to get this done asap.

Cheers,
Chad
Hi
What if I want to ssh from linux to a solaris machine??

does the above method work fine?


pravesh