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

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

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.

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.

I realized that the lsof -i -P does not list all the open ports and therefore I now use

netstat -tuv

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

lsof -i:port number

which displays the process and the PID and now with

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.

A very crude script

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).

Thanks. I am currently working on a slightly different version but I am not sure that it will function properly so I would appreciate your help. I will post my code when I'm done.

Greetings

This is the script I have and I think it is fairly simple since I was able to come up with it on my own. It would have been perfect but unfortunately the last part with the showNumberOfFiles shows an error message instead of the number of files. It says that no PID has been specified.
I am confused because I am using the same method for obtaining the data in both functions.
Can anyone tell me how I can fix this?

#!/bin/bash

showProcesses()
{
	netstat -tuv | awk '{print $4}' > portslist.list
	lsof -i < portslist.list | awk '{print $1}'
}

showNumberOfFiles()
{
	lsof -i < portslist.list | awk '{print $2}' > fileslist.list
	lsof -p < fileslist.list | awk '{print $1}' > numberoffiles.list
	wc -l < numberoffiles.list
}

echo The following are the open ports on your computer:
netstat -tuv | awk '{print $4}'

echo These are the processes on each port:
showProcesses

echo This shows the number of files:
showNumberOfFiles

Could you post the contents of netstat -tuv or fileslist.list

netstat -tuv
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 margarita.bg.nc:38486 jynx.bg.nc:pop3 TIME_WAIT
tcp 0 0 margarita.bg.nc:38175 cub.bg.nc:jabber-client ESTABLISHED
tcp 0 0 margarita.bg.nc:38218 tiger.bg.nc:3389 ESTABLISHED
tcp 0 0 margarita.bg.nc:32768 hal.bg.nc:netbios-ssn ESTABLISHED

And the result of this

lsof -i < portslist.list | awk '{print $2}'

is

PID
56
87
606
608
5583

so I assume this must be the content of fileslist.list

as per lsof man page

-p requires "," separated pid list(ie in 1 line). Your file fileslist.list provides them in multiple lines. Either you change all the pids in multiple lines to pid in 1 line separated by comma or use a loop to call lsof -p separately for each pid.

eg

while read pid; do
lsof -p $pid
done < fileslist.list

Thanks you for your help, vish_indian.
I tried your suggestion but it shows an error message because the first line of the column says "PID" and then it doesn't go on the chech the others. Is there a simple way to start from the second line, or to cut the PID even before placing it into the file. This is my code for the function now:

showNumberOfFiles()
{
	lsof -i < portslist.list | awk '{print $2}' > fileslist.list
	while read pid; do
	lsof -p $pid | awk '{print $1}' > numberofiles.list
	done < fileslist.list
	wc -l < numberoffiles.list
}

Change awk '{print $2}' > fileslist.list to

Cool.
Thank you very much for guiding me through this first sciprt, vish_indian. I am really grateful.
I will be back very soon probably because I am quite excited that this thing worked out and I'm eager to try sth else.

Best wishes,
V

You don't need awk in the above function. Also no need to create another file numberofiles.list.
You can simply redirect the output of lsof -p $pid to wc -l.

Thanks once again. I realised that I don't need the awk thing and removed it myself but I wasn't aware that the file was unnecessary too. I will remove it- it is a serious optimisation.