Execute ssh commands through bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execute ssh commands through bash script
# 1  
Old 09-22-2010
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:

Code:
 
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
# 2  
Old 09-22-2010
put the code in a script make it executable, then in your loop
Code:
ssh x$i <script>

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

- OR -
Code:
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...)
Code:
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 $


Last edited by vbe; 09-22-2010 at 09:56 AM..
# 3  
Old 09-22-2010
Thanks a lot for your suggestion!

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

Thanks again!
# 4  
Old 09-22-2010
Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ssh multiple hops to execute commands with arguments

Hi I need to write a script to ssh through several hops (e.g. HostA-HostB-HostC-HostD), where Host A does not have direct assess to HostC ; HostB cannot access HostD directly. when I ssh 3 hops and run command with arg1, arg2 and redirect the output to a file, e.g. HostA> ssh -t HostB ssh -t... (3 Replies)
Discussion started by: chiensh
3 Replies

2. Shell Programming and Scripting

Shell script help to execute ssh remote commands

Hi, The below command is not giving me the count , Can somebody help me in re-writing this pls . Proc_Exist=`ssh -q -o "BatchMode=yes" -o "PasswordAuthentication=no" $OAUSER@${Primary_Node} ps -ef | grep -v grep | grep "${ICM_Proc}" |wc -l ` Also the same problem with below... (13 Replies)
Discussion started by: Y.balakrishna
13 Replies

3. Shell Programming and Scripting

bash script to execute a command remote servers using ssh

Hello, I am running into few issues, please suggest me what I am missing. I am running this script on a linux host. Main idea of this script is to, login to each host via ssh and get uid of user, service user that I trying to run this script, has already deployed ssh keys and provide sudo... (8 Replies)
Discussion started by: bobby320
8 Replies

4. Shell Programming and Scripting

connect to multiple servers using SSH and execute commands

Requirement: Run a shell script with below inputs file name checksum path the script should go to multiple servers (around 35) and verify the input cksum and if there is a mismatch display a simple message to the user that cksum verification failed. host details, user id /... (1 Reply)
Discussion started by: amicableperson
1 Replies

5. UNIX for Dummies Questions & Answers

commands to execute unix script

commands to execute unix script (1 Reply)
Discussion started by: sunilamarnadh
1 Replies

6. Shell Programming and Scripting

Execute commands from script in current bash session

I have a file as follows: cat /etc/mxg/ssh-hostsmx.example1.com.au:2225 mx2.example2.com.au:2225 mx.example3.com.au:2225 mail.example4.com.au:2225 mail.example5.org.au:2225 mail.example6.com.au:2225I want to dynamically create aliases for quick access to these servers from bash. I wrote... (4 Replies)
Discussion started by: jelloir
4 Replies

7. Shell Programming and Scripting

ssh - to execute set of commands

Hi Can someone help me to figure out Want to execute few cmds in remote host thru ssh Tried below cmd -------------------------------excerpt------------------- RDIR=/data1/logs ---> variable stores rem. server directory TODAY="`date '+%b %d'`" ssh -i $userid@$host "cd... (2 Replies)
Discussion started by: id100
2 Replies

8. Shell Programming and Scripting

Bash commands to an 'ssh' within an ssh'

I've struggled to find a solution to this problem from searching so I thought I'd write a post to see what can be done. I'm attempting to connect and run commands on 'server2' but because of security limitations I cannot access it directly. I can however ssh into 'server1' and then into... (7 Replies)
Discussion started by: mcintosh.jamie
7 Replies

9. Shell Programming and Scripting

Can BASH execute commands on a remote server when the commands are embedded in shell

I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config. I am not sure if I can do this with BASH scripting. I have set up password less login by adding my public key to authorized_keys file, it works. I am a little... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

10. Shell Programming and Scripting

how do i get my script to execute multiple commands?

New to shell scripting. I can't get my script to execute multiple commands. Here's the code. It's a menu script. #!/bin/ksh clear print "SDE MENU" PS3="SDE MENU, enter choice:" select clean_menu in "tasdedev instance 5151" "orkindev instance 5155" "tasdetst instance 5157" "orkinsys... (1 Reply)
Discussion started by: hvincent
1 Replies
Login or Register to Ask a Question