Issue with looping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with looping
# 1  
Old 05-21-2015
Issue with looping

Hi guys,

I am creating a script that checks if a service is UP and running on a bunch of remote servers.

My list of servers is:

Code:
p2 runtime1 8080
p3 runtime2 8080
p4 runtime3 8080
p5 runtime4 8080
p6 runtime5 8080
p7 runtime6 8080
p8 runtime7 8080
p9 runtime8 8080
p10 runtime9 8080
p11 runtime10 8080
p12 runtime11 8080
p13 runtime12 8080

My column is Host Name ( resolvable by DNS), Node Type and Port.


My script is:

Code:
#!/bin/bash

NODE=`cat ports.hosts | grep $1 | awk '{print $2}' `


for i in $NODE
do
    node_port=`cat ports.hosts | grep $i | awk '{print $3}'`
    node_name=`cat ports.hosts | grep $i | awk '{print $1}'`

l_TELNET=`echo "quit" | telnet $node_name $node_port | grep "Escape character is"`
if [ "$?" -ne 0 ]; then
  echo "Connection to $node_name on port $node_port failed"
else
  echo "Connection to $node_name on port $node_port succeeded"
fi

done

But it seems to be creating an issue while looking.


Running the script in debugging mode says:
sh -x check_nodes.sh runtime

Code:
++ cat ports.hosts
++ grep runtime
++ awk '{print $2}'
+ NODE='runtime2
runtime3
runtime4
runtime5
runtime6
runtime7
runtime8
runtime9'

+ for i in '$NODE'
++ cat ports.hosts
++ grep runtime1
++ awk '{print $3}'
+ node_port='8080
8080
8080
8080
8080
8080
8080
8080'
++ cat ports.hosts
++ grep runtime1
++ awk '{print $1}'
+ node_name='p3
p4
p5
p6
p7
pvmkf108
pvmkf109
pvmkf110'
++ echo quit
++ telnet p3 p4 p5 p6 p7 p8 p9 p10 8080 8080 8080 8080 8080 8080 8080 8080 
++ grep 'Escape character is'
Usage: telnet [-8] [-E] [-L] [-S tos] [-a] [-c] [-d] [-e char] [-l user]
	[-n tracefile] [-b hostalias ] [-r] 
 [host-name [port]]
+ l_TELNET=
+ '[' 1 -ne 0 ']'
+ echo 'Connection to p3
p4
p5
p6
p7
p8
p9
p10 on port 8080
8080
8080
8080
8080
8080
8080
 failed'
+ echo 'Connection to p3
p4
p5
p6
p7
p8
p9
p10 on port 8080
8080
8080
8080
8080
8080
8080

8080 failed

Whats wrong in the loop ?
# 2  
Old 05-21-2015
That's a terrible way to handle data. You can use a while read loop and handle this much better. I assume you're passing an argument to the script to match on column 2.

Code:
#!/bin/bash

while read -r host type port _ <&3; do
  [[ $type = *"$1"* ]] || continue

  if nc -z "$host" "$port"; then
    echo "Connection to $host on port $port succeeded"
  else
    echo "Connection to $host on port $port failed"
  fi
done 3<ports.hosts

This User Gave Thanks to neutronscott For This Post:
# 3  
Old 05-21-2015
I do not have nc on the node installed. Don't have authorization to install it too.
# 4  
Old 05-21-2015
you can change that line.. i guess. i don't have telnet Smilie

Code:
if telnet "$host" "$port" 2>/dev/null <<< quit | grep -q ^Escape; then

# 5  
Old 05-21-2015
Thanks ! Worked like a charm ! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting looping issue

#!bin/ksh --------------------------------------------------------------------------------------------- -- Get sequence number from database --------------------------------------------------------------------------------------------- .os rm... (3 Replies)
Discussion started by: swathi reddy1
3 Replies

2. Shell Programming and Scripting

Looping

Hey guys, so I am trying to do a loop script that will go through each folder (no gui so just each domain has a folder) and grab out the databases being used on that domain. I know I would use mysql -e "show databases where not 'information_schema';" once in each directory to pull the actual... (3 Replies)
Discussion started by: dough
3 Replies

3. Shell Programming and Scripting

Looping in background issue

Background info : Linux machines, /bin/bash shell . I have several remote machines. I want to create a variable for each of these remote machines, by retrieving some data remotely for each of them. As this may take some time for each remote machine, I want to launch these commands woth a for... (2 Replies)
Discussion started by: black_fender
2 Replies

4. Shell Programming and Scripting

Odd looping issue with NET::FTP and Proftpd

Hello, I'm a UNIX SysAdmin at a large webhosting company and we have a vendor that provides a service which involves the ftp'ing of files from their servers to ours. They initiate FTP using a perl script with NET::FTP. When they try to transfer files (and delete files over ftp), there is... (3 Replies)
Discussion started by: tmmgeekette
3 Replies

5. Shell Programming and Scripting

[Solved] looping script issue

Hi all.. Need your help I want to combine 2 file into 1file File1 : A B C D File2 : 1 2 3 4 Result : A1 A2 A3 A4 B1 (3 Replies)
Discussion started by: buncit8
3 Replies

6. Shell Programming and Scripting

Looping

Hi, Now I have written a script which sorts the records in the file and splits them according to some condition. Now, I need to modify the script so that it reads all the files one after the other and does the sorting & splitting. Pls help me in reading all the files in a directory and... (8 Replies)
Discussion started by: Sunitha_edi82
8 Replies

7. Shell Programming and Scripting

help with looping

vesselNames values: xxx yyy zzz vesselPlanned values: xxx zzz zzz zzz OIFS="" OIFS=$IFS IFS="\n" (2 Replies)
Discussion started by: finalight
2 Replies

8. Shell Programming and Scripting

help on looping using if/for or while

Hello, where can I get usefull information on the use of looping with for , if and while with extensive examples. Also use of variables in scripts (1 Reply)
Discussion started by: sam4now
1 Replies

9. Shell Programming and Scripting

looping

Hi I have around 100 users in sun server and have default home directory in /usr/home/<username> I want to clean their home directory time to time to make free space on root, as users generate many output files during usage of application. My idea is, generate a file with following command... (4 Replies)
Discussion started by: ishir
4 Replies

10. UNIX for Dummies Questions & Answers

Help with looping

Hi, Actually I have a file which consists data . for eg names. Then I want my sql query to read this file and produce the output. Currently I am using this FOR EG : FILENAME is NAMES for i in `cat NAMES` { sqlplus -s $CONNECTID << EOF spool rooh set heading off select... (1 Reply)
Discussion started by: rooh
1 Replies
Login or Register to Ask a Question