Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scripting

Status
Not open for further replies.

THOR01

MIS
Jul 26, 2007
35
US
Good morning. I am trying to write a script that will tell me when a connection to a particuliar port number changes.

I have a server that connects to me on port 9010, if the connection dies it will try to reconnect to me again on port 9010 but it's port number will have changed. I want to know if the port number changes. I had written a script (tried to write a script) as follows, I don't know if the direction I am going would be the best, some advice would be appreciated.:

Pid_File="/home/user/stat_pid.txt"
PID=`netstat -an |grep 9010 | grep -v grep | awk ' { print $5 }'`
STAT_PID=`cat /home/user/stat_pid.txt`
###############################################################################
# Start Port checker
###############################################################################
if ( $STAT_PID ne $PID )
then
AdminMail=me@comp.com
echo "CONNECTION HAS DIED" > /tmp/txt.out
mail -s "Error Messages" $AdminMail < /tmp/txt.out
fi
 
Couple of things:

You should include some sanity checking on your variables: What if the pid_file is empty or nonexistent? Second, what if there is no connection at the time of the netstat?

Finally, I suspect you should also include a line in the mail notification segment that updates the recorded old pid with the new pid after a change, otherwise you will get an email every time the script checks, after the first reconnect.
 
You should also make the following change:

[tt]PID=`netstat -an |grep '.9010 ' | grep -v grep | awk ' { print $5 }'`[/tt]

The script as written will alarm incorrectly if the string "9010" happens to show up any place other than the port designation on the connection you're looking for, such as a substring of a memory address or other port like 49010.

- Rod


IBM Certified Advanced Technical Expert pSeries and AIX 5L
CompTIA Linux+
CompTIA Security+

A Simple Code for Posting on the Web
 
Another point. If you're using awk then there's no need to use grep so
Code:
PID=`netstat -an |grep '.9010 ' | grep -v grep | awk ' { print $5 }'`
can become
Code:
PID=$(netstat -an|awk '/.9010/ {print $5}')
and, I don't know netstat that well, but shouldnt't it be
Code:
PID=$(netstat -an|awk '/:9010 / {print $5}')

Ceci n'est pas une signature
Columb Healy
 
Good point. You'll want to literalize the period in the regexp as the period will match any character..

grep "\.9010
 
The escaping of the period is needed in awk, as well.

This thread is starting to remind me of Monty Python's Spanish Inquisition sketch.
Ximinez said:
Nobody expects the Spanish Inquisition! Our chief weapon is surprise...surprise and fear...fear and surprise.... our two weapons are fear and surprise...and ruthless efficiency.... Our three weapons are fear, surprise, and ruthless efficiency...and an almost fanatical devotion to the Pope.... Our four...no... amongst our weapons.... amongst our weaponry...are such elements as fear, surprise.... I'll come in again. (exit and exeunt)

- Rod

 
I'm bored so let's assume that this process is run from cron then, putting it all together, we have (NOT TESTED)
Code:
#!/bin/ksh

mail_and_quit ()
  {
  echo $@ | mail -s "Port Checker" me@comp.com
  exit
  }

PID=$(netstat -an|awk '/:9010 / {print $5}')
PID_FILE=/home/user/stat_pid.txt

#Check we have a current connection
[[ -n "$PID" ]] || mail_and_quit "No PID for port 9010"
#Check we have a PID file
[[ -f $PID_FILE ]] || mail_and_quit "$PID_FILE not found"
#Check that PID File contains a number
expr $(cat $PID_FILE) : "^[0-9]+$" > /dev/null || mail_and_quit "Contents of $PID_FILE non numeric"
#Check that contents of PID file match PID
[[ $(cat $PID_FILE) -eq $PID ]] || mail_and_quit "Connection has died"

Ceci n'est pas une signature
Columb Healy
 
or

Code:
#!/usr/bin/ksh

echo -n "which port?> "
read port

for pid in `ps -ef -o pid | tail +2`
do
  foundport=`pfiles $pid 2>&1 | grep "sockname:" | grep "port: $port$" |
awk
'{print $NF}'`
  if [ "$foundport" != "" ]; then
    foundproc=`pfiles $pid 2>&1 | grep "^$pid:"`
    echo "$foundproc, $foundport"
 else
mail 
  fi
done

exit

# End of script

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
He folks THANKS for all the info and explainations. I'll try all your suggestions and update .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top