Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-05-2006
Registered User
 

Join Date: Oct 2006
Posts: 38
first script. need help and advice.

Hello everyone,

This is my first post here and this is the first time I am using UNIX OS (Slackware). I find it really useful and powerful and would like to master it but as you may guess I am expreicing quite a few problems.
I've been reading a few documentations about it and bash this week and now I am trying to make my first script (related to a task I got at the office) but I am really stuck. What I want it to do is make a list of all the open ports on the computer + the processes that have opened them + the number of files opened by each process. I tried using netstat with some of its options for the first two but it wouldn't work and for the last part I'm clueless.
I would be really grateful if anyone could help me.

Kindest regards
Sponsored Links
  #2 (permalink)  
Old 10-05-2006
Registered User
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 92
Try lsof -i, if its available.

which lsof would give a clue, on my system its located at /usr/sbin/lsof but i am using linux
  #3 (permalink)  
Old 10-05-2006
Registered User
 

Join Date: Oct 2006
Posts: 38
Thanks for your suggestion but I think that it lists all processes on all ports without listing the port numbers and I would like to have them as well.

P.S.I'm also using Linux- Slackware.
  #4 (permalink)  
Old 10-05-2006
Registered User
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 92
Quote:
Originally Posted by sanchopansa
Thanks for your suggestion but I think that it lists all processes on all ports without listing the port numbers and I would like to have them as well.

P.S.I'm also using Linux- Slackware.

It does lists the port numbers, but they get converted to service names from /etc/services. Try this:
lsof -i -P

Please check the man page for options that would be suitable for you.
  #5 (permalink)  
Old 10-05-2006
Registered User
 

Join Date: Oct 2006
Posts: 38
Thanks very much, vish_indian.
I looked through the man page after you posted your first reply but I couldn't find what I needed. You were right that it lists the ports and now everything is fine with this.
What I am confused now with is that I want for everyone of the processes that are listed with lsof -i -P to make a list of all the files that each one is using. I looked at the reference and it's done with lsof -p PID, but is there a way to incorporate these two commands into one so that it lists the processes and the files without having to manually run the lsof -p command for each process separately.
  #6 (permalink)  
Old 10-06-2006
Registered User
 

Join Date: Oct 2006
Posts: 38
I realized that the lsof -i -P does not list all the open ports and therefore I now use
Code:
netstat -tuv

instead. In order to find the process at each port I use

Code:
lsof -i:port number

which displays the process and the PID and now with


Code:
lsof -p PID > filelist.list;
wc -l filelist.list;

I display the number of files for each process. My problem is that in order to put this into a script I need to have access to the port numbers and PIDs. Is there a way of putting them into an array?

Thanks for your help.
  #7 (permalink)  
Old 10-06-2006
Registered User
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 92
A very crude script

Quote:
for i in `netstat -ltp -n | awk 'NR>2{ split($4,arr,":");split($7,parr,"/"); print arr[2]":"parr[1]}'`; do
port=`echo $i | cut -d":" -f1`;
pid=`echo $i | cut -d":" -f2`;
echo $port--$pid;
done
Instead of echo $port--$pid you can use lsof -p $pid. Currently, this handles only tcp ports. for udp ports some changes may be required. I'll try to refine it more later. I am also assuming that you have awk(gawk).
Sponsored Links
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need advice naner9 UNIX for Dummies Questions & Answers 1 11-01-2007 11:05 AM
Script Help/Advice earnstaf Shell Programming and Scripting 14 10-11-2007 06:53 PM
Script Advice please? earnstaf Shell Programming and Scripting 12 06-18-2007 05:55 PM
Advice on Script greengrass Shell Programming and Scripting 4 02-11-2007 12:49 AM
c-shell script advice please. killerserv Shell Programming and Scripting 2 06-20-2004 03:51 AM



All times are GMT -4. The time now is 02:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0