Hello,
I've just finished my first script (about displaying open ports on the computer and who opened them) and everything is fine with it but I want to style it a little bit just for the sake of learning how to do this.
What I want to do is make the display like the one of ps, for example, i.e make it look like a table:
port process files
port1 pr1 #of files
port2
I've tried setting the port process and files with awk '{print "process" "port"}' and manipulating the FS but it is way not I wanted to achieve. Could anyone outline me the process of creating a table? Or even redirect me to some good reference on the web because I couldn't find one except for the Gawk variables but I can't figure it out from there.
Here is my source in case it would be easier for you this way:
#!/bin/bash
echo
echo This script will find all the open ports on a computer.
echo It will also show the process that has opened each one and the number of files opened per process.
echo
echo
showProcesses()
{
netstat -atuv | awk '{print $4}' > ports.list
lsof -i < ports.list | awk 'NR>1{print $1}'
}
showNumberOfFiles()
{
lsof -i < ports.list | awk 'NR>1{print $2}' > files.list
while read pid; do
lsof -p $pid | awk '{print $1}' > numberoffiles.list
wc -l < numberoffiles.list
done < files.list
}
echo Open ports on your computer:
echo
netstat -tuv | awk 'NR>2{print $4}'
echo
echo
echo Processes on each port:
echo
showProcesses
echo
echo
echo Number of files per process:
echo
showNumberOfFiles