About Linux FAQ

Browse More FAQs:

How To Use SSH in Unix or Linux shell script

Posted by Vivek Gite [Last updated: April 18, 2008]

Q. How do I use ssh client program in a shell script under UNIX or Linux operating system?

A. SSH client is a program for logging into a remote machine and for executing commands on a remote machine. ssh connects and logs into the specified hostname.The user must prove his/her identity to the remote machine using one of several methods depending on the protocol version used. If command is specified, command is executed on the remote host instead of a login shell.

SSH general syntax

ssh user@hostname command

For example login into remote system called portal.nixcraft.com and find out who logged in, enter:
$ ssh admin@portal.nixcraft.com who
You can use same command in shell script. However, it will prompt for a password. To avoid password prompt you need to ssh keys based login as specified in our article for password less login. Next, you can create a sample shell script as follows:

$ vi sshscript.sh

Type the following shell script lines:

#!/bin/bash
# Linux/UNIX box with ssh key based login enabled
SERVER="192.168.1.1"
# SSH User name
USR="admin"
OUT="out.txt"
ssh $USR@$host w > $OUT

Save and close the file. Type the following command to execute the script:
$ chmod +x sshscript.sh
$ ./sshscript.sh

You can see output of script with cat command:
$ cat out.txt
See more complex example in our shell script directory.

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Other Helpful FAQs:

Discussion on This FAQ

  1. Sriram Says:

    When I try to Authenticate via SSH using a Script , I get

    The script is the same :

    Pseudo-terminal will not be allocated because stdin
    is not a terminal.

    I tried using -t and -T switch both on and off.

    Suggest if you have any better Ideas

    Many Thanks
    Sriram

  2. krishna kumar Says:

    i want to know whether i can login to a sever using SSH by hardcoding the user name and password in my unix script.

    expecting your replay soon.

  3. Nilesh Says:

    Yes, you can login to server using SSH by hardcoding
    . For this you have to use php.
    Php provides expect module to ssh login.

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.