Execute multible commands on a remote machine using SSH

To execute a number of commands on a remote machine you can create a file that contains the commands that you want to execute. This file can then be ‘cat’ed to your ssh command. I wrote one command file to test the functionality and called it ‘remoteCommands’. As you can see it is even possible to use constructs like if clauses.

if [ -e /sbin/vxdisk ]
        vxdisk list | grep -v disk_0 | awk '{ print $1"    "$4"    "$3 }'

The script is getting a list of known LUNs from servers on the network. The output will look like the one below:

[root@testclient ~]# cat bin/remoteCommands | ssh testdb1
Pseudo-terminal will not be allocated because stdin is not a terminal.
emc_clariion0_101    ztkuatjrn_dg    ztkuatjrn_dsk
emc_clariion0_102    ensuatjrn_dg    ensuatjrn_dsk
emc_clariion0_201    ztkuatdb_dg    ztkuatdb_dsk
emc_clariion0_202    ensuated_dg    ensuated_dsk
emc_clariion0_403    ztkuatewd_dg    ztkuatewd_dsk
emc_clariion0_501    ztkbase_dg    ztkbase_dsk-a
emc_clariion0_541    ensbaseed_dg    ensbaseed_dsk

The warning about the ‘Pseudo-terminal’ can be suppressed by using the ssh option -T.

About Juergen Caris

I am 54yo, MSc(Dist) and BSc in Computer Science, German and working as a Senior Server Engineer for the NHS Lothian. I am responsible for the patient management system, called TrakCare. I am a UNIX/Linux guy, working in this sector for more than 20 years now. I am also interested in robotics, microprocessors, system monitoring, Home automation and programming.
This entry was posted in Bash, Beginner. Bookmark the permalink.

Leave a Reply

Your email address will not be published.