Need help on ssh usage in a loop of shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help on ssh usage in a loop of shell script
# 1  
Old 04-10-2012
Need help on ssh usage in a loop of shell script

I need help on how to connect remote systems through ssh command in while loop of shell script. I was able to connect one remote system using ssh from shell script. Please find sample code snippet as given below…..


Code:
  ssh "root@148.147.179.100" ARG1=$rpmFileName 'bash -s' <<'ENDSSH'
                echo ">>Checksum ..."
                md5sum /root/$ARG1
ENDSSH

When tried to run same piece of code within a loop getting the error "syntax error: unexpected end of file", which I couldn’t resolve.

But when placing the same piece of code in another script file and using that file in while loop of another script, is working.

Can anyone help me with some solution.

Regards,
Rajesh Tulluri

Last edited by Scrutinizer; 04-10-2012 at 03:29 PM.. Reason: code tags
# 2  
Old 04-10-2012
The 'endssh' must be at the beginning of the line. It can't even be indented.
# 3  
Old 04-10-2012
I'm able to connect remote system when used the piece of code in shell script (out side while loop). Problem occurs when tried to connect a series of systems. i.e. getting error when attached piece of code used within while loop ... please find sample code as given below..

Code:
while read sysType serverIp uid pwd
do
 
  if [ -n "$serverIp" ]
  then
     valid_ip $serverIp
     #Assign the return value to a variable
     isValidIp=$?
   else
      isValidIp=1
  fi

  if [ $isValidIp -eq "0" ]
    then
       if [ "$sysType" = "ebox" ]
       then
           ssh "$address" ARG1=$rpmFileName 'bash -s' <<'ENDSSH'
              echo ">>Checksum ..."
              md5sum /root/$ARG1
           ENDSSH
        fi
    fi
done

Moderator's Comments:
Mod Comment Welcome to the UNIX and Linux Forums. Please use code tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-10-2012 at 03:29 PM.. Reason: Code tags
# 4  
Old 04-10-2012
Please have a look at Corona688's comment and then look at the position of the ENDSSH label in your script..
# 5  
Old 04-11-2012
Hi Corona688/Scrutinizer,
Thanks for your reply. But i couldn't understand the reply fully.. apologies for that. I understood that I need to use ENDSSH at the beginning of the line, do you mean I need to use the label as given below..
Code:

ENDSSH ssh -n "$address" ARG1=$rpmFileName 'bash -s'
echo ">>Checksum ..."
md5sum /root/$ARG1
ENDSSH

It's still giving error as "not a valid identifier" correct if i'm wrongly using the label...

Last edited by Franklin52; 04-11-2012 at 04:04 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 04-11-2012
There are two endssh. One of them is indented. Which one would make the most sense to not indent, if I told you they couldn't be indented? The one that was indented? Or the one which wasn't?

Code:
while read sysType serverIp uid pwd
do
 
  if [ -n "$serverIp" ]
  then
     valid_ip $serverIp
     #Assign the return value to a variable
     isValidIp=$?
   else
      isValidIp=1
  fi

  if [ $isValidIp -eq "0" ]
    then
       if [ "$sysType" = "ebox" ]
       then
           ssh "$address" ARG1=$rpmFileName 'bash -s' <<'ENDSSH'
              echo ">>Checksum ..."
              md5sum /root/$ARG1
ENDSSH
        fi
    fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While loop is causing ssh command to exit from script after first iteration.

I am trying to check multiple server's "uptime" in a loop over "ssh". When I execute multiple ssh commands with hard coded servernames script is executing fine. But when I pass server names using while loop, script is exiting after checking first server's status, why? # serverList... (8 Replies)
Discussion started by: kchinnam
8 Replies

2. UNIX for Dummies Questions & Answers

Basic sed usage in shell script

Hi, A basic sed question. I have a set of files. In each file there is a number that I want replaced. For example, if I run sed I should get the following: % cat test2.txt #goofy//171.00 goofy 171.00 % sed -i 's/171/xxx/g' test2.txt % cat test2.txt #goofy//xxx.00 goofy xxx.00 ... (2 Replies)
Discussion started by: pc2001
2 Replies

3. AIX

Loop breaks through ssh in script

hello all, i have an AIX6.1 machine and INFORMIX 11.7 database server. i have a script to create users on 3 machines and also i need to grant this user access to a specific database. the script works and it does what i want it to do but the loop doesnt work. for example if i insert 10 lines in... (5 Replies)
Discussion started by: omonoiatis9
5 Replies

4. Shell Programming and Scripting

Shell script to calculate the max cpu usage from the main script

Hi All, I have a script which does report the cpu usuage, there are few output parameter/fields displayed from the script. My problem is I have monitor the output and decide which cpu number (column 2) has maximum value (column 6). Since the output is displayed/updated every seconds, it's very... (1 Reply)
Discussion started by: Optimus81
1 Replies

5. Shell Programming and Scripting

Need help to make a shell script with usage

Hello, I begin to write my first shell script, but I am totally lost, hope you can help me. I'd like to write a script with some conditions and multiples usages: ./myscript.sh -i <host> or ./myscript.sh -e <host> Have you some suggestion, because I am really lost, I try to do something... (12 Replies)
Discussion started by: Francesco
12 Replies

6. Shell Programming and Scripting

How to use ssh execute other shell script on other host (shell script include nohup)?

i want use ssh on the host01 to execute autoexec.sh on the host02 like following : host01> ssh host02 autoexec.sh autoexec.sh include nohup command like follwing : nohup /home/jack/deletedata.sh & after i execute ssh host02 autoexec.sh one the host01. i can't found deletedata.sh... (1 Reply)
Discussion started by: orablue
1 Replies

7. Shell Programming and Scripting

usage of telnet in shell script

Hi All, How could I use telnet to connect to Primary Host in the below shell script: Could I use: ( echo user sleep 2 echo password sleep 2 echo "ls ~" sleep 2 echo "exit" ) | telnet hostname ... (1 Reply)
Discussion started by: a1_win
1 Replies

8. Shell Programming and Scripting

how do i get current bandwidth usage via shell script?

hello unix-people. can u please tell me how i can get the current bandwidth usage of my machine on shell into variables? thanks a lot (2 Replies)
Discussion started by: scarfake
2 Replies

9. UNIX for Dummies Questions & Answers

shell script extended usage...

Hi all, In a shell script, how can I do the following: Find out the size of a mounted usb stick (df -h /media/usbdisk) and use the result (physical size of USB stick) to set a loop-counter according to the disk size?? As a result, I do copy files of 4MB each as many times as they will fit... (1 Reply)
Discussion started by: joerg535
1 Replies

10. Shell Programming and Scripting

SSH usage in a script

I have configured ssh to a remote server without prompting for a password with the help of public keys...but now i am not able to execute any commands through a script what i want is a one line script which takes me directly into /var directory after executing it...searched the entire forum but... (2 Replies)
Discussion started by: roshanjain2
2 Replies
Login or Register to Ask a Question