How To Use SSH in Unix or Linux shell script

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.

ADVERTISEMENTS

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.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
9 comments… add one
  • Sriram Sep 7, 2006 @ 5:35

    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

    • Anonymous Jun 16, 2011 @ 10:32

      You can try replacing ” ssh $USR@$host w > $OUT” with ” ssh -t -t -l $USR $host w > $OUT”

  • krishna kumar Mar 28, 2007 @ 6:20

    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.

  • Nilesh Aug 21, 2007 @ 12:02

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

  • ccb Mar 28, 2009 @ 1:40

    @ krishna kumar
    @ Nilesh

    PHP isn’t necessary — you can generate public/private keys per user/host you wish to connect to and avoid passwords altogether, allowing you to fully automate your ssh connections.

    Google ‘ssh without password’ and you’ll find plenty of tutorials on this process.

  • mike May 4, 2012 @ 4:53

    I found it necessary to put my commands in double quotes.

    ssh $USR@$host “w > “$OUT

    Not sure why that wasn’t needed for the above.

    Also, do I need to include an “exit” command? Or is the channel closed automatically somehow?

    Mike

  • Prateek May 9, 2012 @ 18:39

    I want to know by Ssh command by logging to another server I am trying to run a program that contains a special character “$” and it’s giving error ….. Any advice for this

  • prathamesh Oct 12, 2012 @ 17:35

    Hey I want to setup my centos machine like whenever any local user connect my pc through ssh the entry is added in any file so can see who used my machine.

    So how can i seup this???

  • vinisha Nov 13, 2013 @ 11:55

    Hi,

    i want to use ssh for file transfer. along with ssh command i want to use set of aommands in same session.
    e.g. ssh HOSTNAME command

    following command to be executed in one go
    cd dir
    ssh_get *
    exit

    is there any way I can do this?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.