LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 08-02-2012, 04:52 AM   #1
Iyyappan
Member
 
Registered: Dec 2008
Location: Chennai, India
Distribution: CentOS 5, SLES 11
Posts: 245

Rep: Reputation: 4
script to scan ports using nc command


Hi All,
I am using nc to scan ports. But I need to scan only two different ports. I have written script for one port.


#!/bin/sh

DATE=`date +%h-%d-%Y_%H:%M`
for port in 3220
do
# echo $port
/usr/bin/nc -z *.*.*.* 3220
if [ $? -eq 0 ]
then
echo $port is Up
else
echo $port is Down on $DATE
fi
done


But I need to scan port 52000 port too in the script, kindly guide me how to do this
 
Old 08-02-2012, 05:00 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

seems you need to learn how to use for loops.
Here is and example:
Code:
for p in 3220 52000
do
  echo $p
done
Evo2.
 
Old 08-02-2012, 11:45 AM   #3
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Or use a proper port-scanning utility like nmap(1).

(Don't get me wrong; nc(1) is a beautiful utility with many purposes. But only being able to scan single ports or ranges - without the help of a wrapper script - is annoying.)
 
Old 08-04-2012, 09:57 AM   #4
Iyyappan
Member
 
Registered: Dec 2008
Location: Chennai, India
Distribution: CentOS 5, SLES 11
Posts: 245

Original Poster
Rep: Reputation: 4
I used two scripts separately for two ports.

#!/bin/sh

/usr/bin/nc -z -w 5 localhost 52000 > /dev/null
if [[ $? -eq 0 ]]
then
echo 0
else
echo 1
fi




#!/bin/sh

/usr/bin/nc -z -w 5 localhost 6280 > /dev/null
if [[ $? -eq 0 ]]
then
echo 0
else
echo 1
fi
 
Old 08-04-2012, 12:34 PM   #5
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
If you're going to completely ignore our advice, then at least consider eliminating the redundant if-else constructs (in each of your redundant scripts).
 
Old 08-04-2012, 11:53 PM   #6
Iyyappan
Member
 
Registered: Dec 2008
Location: Chennai, India
Distribution: CentOS 5, SLES 11
Posts: 245

Original Poster
Rep: Reputation: 4
Anomie,
I did use your suggestion for another server. I had placed two scripts because, I was in a hurry to complete that day itself.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How can i add ports in firewall using command or script anuja shridhar Linux - Newbie 2 02-18-2011 06:22 PM
Scan open ports Iyyaaz Linux - Kernel 1 12-23-2010 03:54 PM
Scan for Local Open Ports ToNNiX Linux - Newbie 1 11-09-2006 10:19 AM
Explaination of network scan ports. NuxIT Linux - Networking 1 06-17-2006 10:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 12:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration