Execute ssh commands through bash script

Hi all!

I am trying to write a script that will check if a certain directory is available at several different nodes and then do stuff in it .....

On the beginning of the script I give as a variable the directory and the number of the nodes and then I loop like this:

 
for ((i=1;i<=$NODES_NUM;i+=1))
        do
                       ssh x$i
                        if [ -d "$DATA_DIR" ]
                        then
                        cd $DATA_DIR
                        {execute a banch of commands}
                        else
                        echo "The dir is not available"
                        fi
                exit
        done

My problem is that because I ssh then the rest of the scipt is not executing at all. Any ideas?

Thanks a lot

put the code in a script make it executable, then in your loop

ssh x$i <script>

The script should be placed on all the nodes that requires...

  • OR -
ssh x$1 'bash -s' < script

If this alternative works for you, you have then no need of copying the script on the remote servers...

..
All the best

---------- Post updated at 15:12 ---------- Previous update was at 14:37 ----------

I just tested for you using ksh: (on an hpux...)

date
ll -lt
"ssh_test" 2 lines, 12 characters 
ant:/home/vbe $ ssh ran 'ksh -s' <ssh_test
Wed Sep 22 14:59:13 METDST 2010
total 990
-rw-r--r--   1 root       sys           5174 Sep 22 11:19 ioscan_log.100917
-rwxrwxr-x   1 vbe        bin           1764 Sep  8 15:41 mt_pt_contents_get
-rw-rw-rw-   1 vbe        bin          22393 Sep  8 14:37 mt_pt_contents.log
-rw-r--r--   1 vbe        bin           1839 Sep  8 14:25 shell_template
-rwxrwxr-x   1 vbe        bin            214 Sep  8 14:19 mt_pt_contents_get.save
-rw-rw-rw-   1 vbe        bin           3472 Sep  3 18:29 make_rec_out.100903
-rw-r--r--   1 root       sys          28752 Aug 20 15:29 hldutil_ran.100820
-rwxrwxr-x   1 vbe        bin            158 Jul  8 13:42 function.ksh
-rw-rw-rw-   1 vbe        bin             32 Jul  8 13:20 SERVERNAMES
-rw-rw-rw-   1 vbe        bin           3887 Jun 23 20:04 ran_mf-licences.now
-rw-rw-rw-   1 vbe        bin            561 Jun 22 11:17 ran_disk.used
-rw-rw-rw-   1 vbe        bin           1181 Jun 22 11:15 ran_dsk
-rw-rw-rw-   1 vbe        bin            742 Jun 16 15:52 ezra_reboot.100616
-rw-rw-rw-   1 vbe        bin            162 Jun  1 14:30 add_driver.howto
-rw-rw-rw-   1 vbe        bin            297 Jun  1 14:07 pdc_info_get.howto
-rw-rw-rw-   1 vbe        bin            192 May  4 11:36 raco_caph.txt
-rw-rw-rw-   1 vbe        bin            888 Apr 27 10:38 adviser.out
-rwxr-xr-x   1 root       sys            305 Dec  1  2009 nddget_check
-rw-r--r--   1 root       sys            340 Dec  1  2009 nddset_bastil
-rw-rw-rw-   1 vbe        bin              0 Oct 27  2009 output.txt
-rw-rw-rw-   1 vbe        bin           1716 Jul 15  2009 bdf.090715
-rw-r--r--   1 root       sys            444 Jul 11  2009 rc.tipix_ezra
-rw-rw-rw-   1 vbe        bin            367 Jun 12  2009 dsk_todo.090526
-rw-r--r--   1 root       sys           4516 Jun  3  2009 typed.090528
-rw-rw-rw-   1 vbe        bin          10014 May 26  2009 ioscan_disk_log.090526
-rw-rw-rw-   1 vbe        bin           3263 Jan  9  2009 typescript
-rw-r--r--   1 root       sys          27352 Jan  9  2009 ipcs-a.log
-rwxrwxr-x   1 root       sys            107 Jan  8  2009 kill_user
-rw-r--r--   1 root       sys             11 Jan  7  2009 old_uname-i
-rw-r--r--   1 root       sys              0 Jan  7  2009 dsmj.log
-rw-r--r--   1 root       sys           1448 Dec 11  2008 var_not_on_1line.howto
-rw-r--r--   1 root       sys            407 Dec  9  2008 users
-rw-r--r--   1 root       sys             95 May 29  2008 dsmerror.log
-rw-rw-rw-   1 vbe        bin            265 Nov  9  2007 ioscan_proc_ran.txt
-rw-rw-rw-   1 vbe        bin           2729 Nov  9  2007 swlist_ran.txt
-rw-rw-rw-   1 vbe        bin          14241 Dec 22  2006 syslog_for_fred.txt
-rw-r--r--   1 root       sys          36100 Dec 14  2006 syslog_out.txt
-rw-rw-rw-   1 vbe        bin           1127 Sep 21  2006 isa_wants
-rw-rw-rw-   1 vbe        bin           1971 Sep 20  2006 itr_req
-rw-rw-rw-   1 vbe        bin           1535 Sep 15  2006 for_ITR
-rw-rw-rw-   1 vbe        bin           1715 Mar 15  2006 use_utmpfix_on
-rwxr-xr-x   1 vbe        bin            258 Mar  9  2006 dtree
-rw-rw-rw-   1 vbe        bin           1648 Jan 20  2006 bdf.060120
-rw-rw-rw-   1 vbe        bin           3091 Jan 12  2006 dsk_manip.060111
-rw-rw-rw-   1 vbe        bin           5593 Jan  8  2006 bdf.060108_b
-rw-rw-rw-   1 vbe        bin           1973 Jan  8  2006 bdf.060108_a
-rw-r--r--   1 root       sys           1047 Jan  5  2006 typ_licence.06
-rw-r--r--   1 root       sys          28168 Dec 20  2005 hldutil_out.052012.postioscan
-rw-r--r--   1 root       sys          28168 Dec 20  2005 hldutil_out.052012.beforioscan
-rw-rw-rw-   1 vbe        bin            751 Dec 20  2005 dsk_layout.txt
-rw-rw-rw-   1 vbe        bin            620 Dec 19  2005 to_remove_dsk
-rw-rw-rw-   1 vbe        bin           2169 Nov 18  2005 bdf.051118
-rw-r--r--   1 root       sys         137973 Sep 29  2005 saslog.log
-rw-r--r--   1 root       sys          15106 Sep 29  2005 sam_extract.log
-rw-rw-rw-   1 vbe        bin           2169 Apr 25  2005 bdf.050425
-rwxrwxrwx   1 vbe        bin            822 Jan  6  2005 sftp_usage01.txt
-rwxr-xr-x   1 root       sys          16384 Jan  4  2005 clrwtmp
-rwxrwxr-x   1 vbe        bin            265 Sep  1  2004 cpu_count
drwx------   2 vbe        bin             96 Feb 27  2004 nsmail
ant:/home/vbe $ 

Thanks a lot for your suggestion!

I was looking for a way to do it directly but apparenty there isn't one!

Thanks again!

for ((i=1;i<=$NODES_NUM;i+=1))
do
    ssh x$i < script                
done

should work, the only difference with the original script is putting the if condition and treatment in a shell script used in input