Slow Script Execution.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Slow Script Execution.
# 1  
Old 06-22-2011
Question Slow Script Execution.

Basically my requirement is to know the total number of free anonymous ports.

anonymous port range is 32768- 65535.

i wrote a script for that





**********************************************
Code:
for i in {32768..65535}
do
netstat -an | grep $i > /dev/null 
        if [ $? -ne 0 ]
        then
portcount=$((portcount+1))
        fi
done
echo "Totat free anonymous ports are portcount"

************************************************






but the above script take 20 mins to execute

Also tried this way

*******************************************************
Code:
netstat -an > $HOME/netstat.out
for i in {32768..65535}
do
grep $i $HOME/netstat.out > /dev/null
        if [ $? -ne 0 ]
        then
portcount=$((portcount+1))
        fi
done
echo "Totat free anonymous ports are:$portcount"

*******************************************************

but still no luck.

Is there a quick way to acheive this ?

Last edited by pludi; 06-22-2011 at 12:25 PM..
# 2  
Old 06-22-2011
sorry wrong context...

Last edited by rn_; 06-22-2011 at 12:27 PM..
# 3  
Old 06-22-2011
Surely you want to start at free ports = 32767 and subtract from there if they are used in netstat?

try the following
Code:
ports=$((65535-32768))
for i in $(netstat -an  | grep -Eo '[0-9]+' ) 
   do 
   if [ $i -gt 32768 -a $i -lt 65535 ] 
   then 
      ports=$((ports-1))
   fi
done
echo "There are $ports free "

# 4  
Old 06-22-2011
Try this:
Code:
netstat -atu | perl -nae '$F[3]=~/:(\d+)$/;$i++ if $1>32767;END{print "Free anon ports: ",32767-$i,"\n"}'

Note that I used netstat -atu, which will show only TCP and UDP ports, without UNIX sockets present in netstat -an, which might confuse your calculations (6th column in UNIX sockets output shows inode number which might fall into the port range while grepping).
# 5  
Old 06-22-2011
Code:
for i in {32768..65535}
do
netstat -an | grep $i > /dev/null 
        if [ $? -ne 0 ]
        then
portcount=$((portcount+1))
        fi
done
echo "Totat free anonymous ports are portcount"

Well no wonder it's slow. You're running netstat, a program which can take seconds to run if you've got a lot of traffic, thirty-two thousand separate times. Just run it once, and process its output once.

I was going to write an awk solution but the perl one looks more elegant.

Quote:
Originally Posted by bartus
Note that I used netstat -atu, which will show only TCP and UDP ports
I'd suggest netstat -atun, to avoid domain unnecessary name lookups. They don't care who's connected to where for this, just what ports are being used.
# 6  
Old 06-22-2011
Quote:
Originally Posted by Corona688
I was going to write an awk solution but the perl one looks more elegant.

I'd suggest netstat -atun, to avoid domain unnecessary name lookups. They don't care who's connected to where for this, just what ports are being used.
I wrote it in awk but before submitting saw the sh solution. Smilie

"netstat -atun" is definitely the way to go.
# 7  
Old 06-22-2011
Error

It's not working for me and I never used perl.

bash: netstat -atu | perl -nae '$F[3]=~/Smilie\d+)$/;$i++ if $1>32767;END{print "Free anon ports: ",32767-$i,"\n"}'
Code:
netstat: illegal option -- t
usage: netstat [-anv] [-f address_family]
       netstat [-n] [-f address_family] [-P protocol] [-g | -p | -s [interval [count]]]
       netstat -m [-v] [interval [count]]
       netstat -i [-I interface] [-an] [-f address_family] [interval [count]]
       netstat -r [-anv] [-f address_family|filter]
       netstat -M [-ns] [-f address_family]
       netstat -D [-I interface] [-f address_family]
Free anon ports: 32767

Wanted to share the Operation System version.
Code:
bash> uname -a
SunOS mypc 5.10 Generic_144488-07 sun4v sparc SUNW,SPARC-Enterprise-T5220

Also, wanted to understand if you script runs from 32767 to 65535

Thanks for your quick reply.

---------- Post updated at 11:22 AM ---------- Previous update was at 11:11 AM ----------

My grep does not have the E,o option.
Code:
grep: illegal option -- E
grep: illegal option -- o

Usage: grep -hblcnsviw pattern file . . .
 
SunOS 5.10          Last change: 26 Feb 2008                    1
User Commands                                             grep(1)
OPTIONS
     The following options are supported for  both  /usr/bin/grep
     and /usr/xpg4/bin/grep:
     -b    Precedes each line by the block number on which it was
           found. This can be useful in locating block numbers by
           context (first block is 0).
     -c    Prints only a count of the lines that contain the pat-
           tern.
     -h    Prevents the name of the file containing the  matching
           line  from  being  prepended  to that line.  Used when
           searching multiple files.
     -i    Ignores upper/lower case distinction during  comparis-
           ons.
     -l    Prints only the names of files  with  matching  lines,
           separated  by NEWLINE characters.  Does not repeat the
           names of files when the pattern  is  found  more  than
           once.
     -n    Precedes each line by its  line  number  in  the  file
           (first line is 1).
     -s    Suppresses error messages about nonexistent or unread-
           able files.
     -v    Prints all lines except those that  contain  the  pat-
           tern.
     -w    Searches for the expression as a word as if surrounded
           by \< and \>.
 
uname -a
SunOS mypc 5.10 Generic_144488-07 sun4v sparc SUNW,SPARC-Enterprise-T5220

Quote:
Originally Posted by Skrynesaver
Surely you want to start at free ports = 32767 and subtract from there if they are used in netstat?

try the following
Code:
ports=$((65535-32768))
for i in $(netstat -an  | grep -Eo '[0-9]+' ) 
   do 
   if [ $i -gt 32768 -a $i -lt 65535 ] 
   then 
      ports=$((ports-1))
   fi
done
echo "There are $ports free "


Last edited by Scott; 06-22-2011 at 03:40 PM.. Reason: Code tags, please...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Script execution is very slow when trying to find all files and their owners on HP-UX box

Hi, I have a HP-UX server were I need to list all the files in the entire file system, their directory path, last modified date, owner and group. I do not need to search the file contents. I created the script given below and I am excluding directories and files of type tmp, temp and log. The... (4 Replies)
Discussion started by: Adyan Faruqi
4 Replies

2. Shell Programming and Scripting

Shell script reading file slow

I have shell program as below #!/bin/sh echo ======= LogManageri start ========== #This directory is getting the raw data from remote server Raw_data=/opt/ftplogs # This directory is ready for process the data Processing_dir=/opt/processing_dir # This directory is prcoessed files and... (4 Replies)
Discussion started by: Chenchireddy
4 Replies

3. Shell Programming and Scripting

Script to alert about a slow link on the website

Hello all, Currently I am using a script with "curl" to get the an alert if 200 ok would not be grepped.and the link is down. is it possible to get an alert mail if a particular link on a website is not completely down but SLOW?? (0 Replies)
Discussion started by: chirag991
0 Replies

4. Shell Programming and Scripting

Slow down output from dhclient-script to screen

Hi I know the basic about script and sleep processes. However this is more tricky: I would like to run sh -x /sbin/dhclient-script and slow down the output of the script as a whole. How would you do it? I would like to delay output on the screen with 1 second for every line for the output... (3 Replies)
Discussion started by: medium_linux
3 Replies

5. UNIX for Dummies Questions & Answers

Help with slow KSH script

My script builds a lot of these array lists, then compares their sizes which solves my problem, but runs very slow. :( set -A comboSorted -- $( for x in ${IDs} do nawk -v s=$x ' BEGIN { testPattern="^" s "$" } { if ( $2 ~ testPattern ) { getline;getline; if ($1 == "IMAGE_SIZE") print... (1 Reply)
Discussion started by: nerdcurious
1 Replies

6. Shell Programming and Scripting

Slow Perl script: how to speed up?

I had written a perl script to compare two files: new and master and get the output of the first file i.e. the first file: words that are not in the master file STRUCTURE OF THE TWO FILES The first file is a series of names ramesh sushil jonga sudesh lugdi whereas the second file (could be... (4 Replies)
Discussion started by: gimley
4 Replies

7. Shell Programming and Scripting

Bash script too slow

I have a bash script that will take approx. 130 days to complete. I am trying to grep a list of 1,144 user ID's out of 41 (1 GB each) files. The 41 files were originally one 41 G file, but that was horrendously too slow.:eek: This is my current file: #!/bin/bash for i in `cat... (11 Replies)
Discussion started by: tigta09
11 Replies

8. Shell Programming and Scripting

script to add numbers is slow

Hi, I am running a BASH shell with the following script. The script works and gives me correct output but is very slow with large files. The more rows and columns (width and height) the slower as you can probably see. How can I do what I want more efficiently? Any ideas welcome. It has been... (10 Replies)
Discussion started by: macsurveyr
10 Replies

9. Shell Programming and Scripting

slow command execution?

Dear World, I just wrote a script, which puzzled me somewhat. The siginficant code was: for file in `ls splits*`; # splits* came from a split command executed earlier do tail -$SomeNumber $file | cut -d" " -f6 > $file; done; The interesting thing is this: A few of the $files were... (2 Replies)
Discussion started by: BandGap
2 Replies

10. UNIX for Advanced & Expert Users

My script runs too slow :-(...

Hello experts, I have a series issue in script that result with bad peformence and I wonder if you can assist me. For example I have two files: File-New, size 15Mb. File-Old, size 1Mb. File-New content: a b c k File-Old content: d f a b (0 Replies)
Discussion started by: roybe
0 Replies
Login or Register to Ask a Question