script to display occupied and non occupied ports in unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script to display occupied and non occupied ports in unix
# 1  
Old 07-31-2009
script to display occupied and non occupied ports in unix

Quote:
#!/bin/bash
if [ $# -ne 3 ]
then
echo "Usage: $(basename $0) <host/ip> <start port> <end port>"
exit 1
fi

for port in $(seq $2 $3)
do
nc -z $1 $port && echo "$port on $1 is open"
done
I want to extend this script.

This must also be able to show me that a port is already assigned to a service but not running now

there must be three different messages

port 8949 is open but not listening
port 8959 is open
port 8999 hasn't been assigned to any service.


I know that using telnet we can do but how can I use the telnet command( this must be done using set timeout)


Thanks,
Charan
# 2  
Old 08-05-2009
Your script works just about fine. You might need an if-else construct:
Code:
if nc -z $1 $port &>/dev/null; then 
   echo "Port $port is open"
else
  echo "Port $port is not open";
fi

But what does this mean: "hasn't been assigned to any service"? That's meaningless. Either a port is being listened to or it's not.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX script to display the filename

Hi All How to answer the below interview question.. With a path and filename of "/mydir1/mydir2/mydir3/myfilenane.dat" write a UNIX script to display the filename (2 Replies)
Discussion started by: shumail
2 Replies

2. Shell Programming and Scripting

Help with script to fully occupied all available cpu

I have long list of file wanna processed by a program: data_1.txt data_2.txt data_3.txt data_4.txt data_5.txt data_6.txt data_7.txt data_8.txt . . data_1_2.txt data_2_2.txt data_3_2.txt data_4_2.txt data_5_2.txt data_6_2.txt . (12 Replies)
Discussion started by: perl_beginner
12 Replies

3. UNIX for Dummies Questions & Answers

Display all directory/sub directory with occupied space?

Hello, I am using Red Hat linux system. I see my /work directory has used space 300GB. But there are so many sub directory under /work. I want to list each direcotry and under all subdirectory. But i want to know how much space occupied by each directory. What kind of command i can use to... (3 Replies)
Discussion started by: govindts
3 Replies

4. UNIX for Dummies Questions & Answers

Space occupied by core

Guys, Can some one help me? I need to find the total percent of space occupied by core files on my unix system. I know df -k will give me the percent space utilization for a directory but how do we replicate the space for a file? (3 Replies)
Discussion started by: yabhi_22
3 Replies

5. Solaris

Command used for checking space occupied by files & sub-direc's inside a mount in %?

Hi, I want to know the command which can be used for finding the % of disk space occupied by files & sub-folders inside a given mount in Sun Solaris For eg: I have /tmp/ folder when I sat df -k it will give the percentage of space used by /tmp/. Say if I want to see how much % the files &... (2 Replies)
Discussion started by: weblogicsupport
2 Replies

6. UNIX for Advanced & Expert Users

so there is no way to know the size occupied by data ?

I want to find out how many disk blocks are used by only data (and not metadata) by a file. But as far as I can tell, if the file has holes, then there is no way to know this. You can find out the logical size of the file (physical size + hole blocks). You can get the physical size of the... (3 Replies)
Discussion started by: the_learner
3 Replies

7. UNIX for Dummies Questions & Answers

Want display a line with space in file by unix script

HI , I am having a file as -----------------a.out------------------- Hi I am unix developer using hp unix this is a test --------------------------------------- i need to read each line by a unix script of the file and to print in console with the space in the line as ... (9 Replies)
Discussion started by: arunkumar_mca
9 Replies

8. Programming

How to find out the stack size occupied for a process?

Hi, In Linux how to find out what will be the stack size allocated for a process? Actually i have to fork n number of processess, and will call exec. I will be execing an executable which is already multithreaded and each thread size is defined. My doubt is how to know if the size of the... (2 Replies)
Discussion started by: rvan
2 Replies

9. UNIX for Dummies Questions & Answers

Ports In Unix

Please could some one tell me how to open a port in unix or to check if a port is open. (2 Replies)
Discussion started by: bountyhunter
2 Replies
Login or Register to Ask a Question