2

I got a task to do.

I have to search whether, are there running services on below ports.
ports 53 & 55
If not I have to send alerts.
Please correct my logic and syntax in below code for one port

ntstat=`netstat -tulpn | grep :53 | grep LISTEN | awk {'print $4'}`
port="*:53"

#Just to echo for testing
echo $ntstat  
echo $port

if [[ "$ntstat" == "$port" ]];
then
   echo " X Service at port number $port"
else 
   echo " Port is not listing  "
fi

Output :

127.0.1.1:53
*:53
Port is not listing  

I am struggling to match strings (127.0.1.1:53 to *:53)

  • @Kusalananda please look into this – Prabash Oct 10 '17 at 17:28
  • You've got some answers from people on Linux systems. I'm not on Linux and my netstat is different. Could you not also check the daemons that are supposed to listen on those ports? Some systemctl command or other to make sure they are running? (again, I'm not on Linux) – Kusalananda Oct 10 '17 at 21:26
0

You can also do a text-processing to your current output to match the port. Just add sed -e 's/.*:/:/g'

#!/bin/bash
ntstat=`netstat -tulpn | grep ":53 " | grep LISTEN | awk {'print $4'} | sed -e 's/.*:/:/g'`
port=":53 "

#Just to echo for testing
echo $ntstat  
echo $port

if [[ "$ntstat" == "$port" ]]
then
    echo " X Service at port number $port"
else 
    echo " Port is not listing  "
fi 

Please note that I added a space right after the port number in order to avoid other ports containing 53

Since this are bash operators, you must want to put the shebang #!/bin/bash at the very top of your script so when you execute your script as ./portlisten.sh, it will take the interpreter as /bin/bash, would be the same if you execute your script as $ bash portlisten.sh

  • Thanks lot for your solution . Can please explan how does sed -e 's/.*:/:/g' command works with my output 127.0.1.1:53.since i am very new to sed commands . – Prabash Oct 11 '17 at 0:43
  • I have implemented your code. but still i am getting this out put - :53 :53 portlisten.sh: 10: portlisten.sh: [[: not found Port is not listing – Prabash Oct 11 '17 at 3:04
  • @Prabash I edited the answer, give a try – tachomi Oct 11 '17 at 13:43
  • so i added your new code and execute as ./portlisten.sh please find output as below :53 :53 Port is not listing still it says "port is not listing" , I think we have problem with string matching – Prabash Oct 12 '17 at 5:59
  • I am able to below get out put :53 :53 X Service at port number :53 #I have removed " " in variables comparison ..... if [[ $ntstat == $port ]] . Once again thank for your support. – Prabash Oct 12 '17 at 7:34
1

To check if there are running services on ports 53(or 55) I would suggest the following optimized solution:

port=":53"
if netstat -tulpn | grep --line-buffered -q "$port .*LISTEN"; then
    echo " X Service at port number $port"
else 
    echo " Port is not listing  "
fi
0

Don't use netstat since this is a deprecated command, use ss instead.

In your case you could use something like:

ss -H -o state listening '( sport = :53 or sport = :55 )' 
  • 1
    Who deprecated netstat? How can a non-standard utility be deprecated? – Kusalananda Oct 11 '17 at 6:07
  • @Kusalananda the netstat command is part of the net-tools bundle which hasn't been maintained since 2011. To me this is pretty much outdated and shouldn't be used in modern operating systems. Have a loop at the man netstat manpage. Once again, netstat is replaced by ss (Socket Statistics) , has a more sophisticated option set and is the future. – Valentin Bajrami Oct 11 '17 at 9:17
  • @val0x00ff Ah, the Linux netstat may well be old, but netstat, albeit another implementation of it, is also installed by default on BSDs. This is obviously not "deprecated" but actively maintained. – Kusalananda Oct 11 '17 at 9:19
  • @Kusalananda BSD implementations use sockstat in that regard. Also as far as my research concerns, The man page of netstat tells us the following: NOTES This program is mostly obsolete. Replacement for netstat is ss. Replacement for netstat -r is ip route. Replacement for net‐ stat -i is ip -s link. Replacement for netstat -g is ip maddr. – Valentin Bajrami Oct 11 '17 at 9:22
  • @val0x00ff I'm ok with saying that the net-tools netstat may be deprecated, but the BSD version certainly is not: man.openbsd.org/netstat – Kusalananda Oct 11 '17 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.