954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem with TCP/UDP

Hi there,
I have a little problem with commands. I know this is about shell scripting but i couldn't find a thread for commandlines. So i have this assignment to "display all the lines for tcp- services and udp- services and count the lines" and i honestly don't have an idea how to do that, i know how to count the lines tho XD...if someone could help me i would be grateful. :)

Elandir
Newbie Poster
6 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

if it is on linux system and you are referring to the active connections to the server use the following:

netstat -a

to list a count of all of the UDP entries use:
netstat -a | grep udp | wc

rch1231
Posting Shark
959 posts since Sep 2009
Reputation Points: 119
Solved Threads: 142
 

Thanks alot :)

Elandir
Newbie Poster
6 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

If you just want TCP info from netstat, you can do netstat -at , or netstat -au for UDP.

You can also get some good information from lsof -i

A little late, but I hope this helps!

Gromit
Posting Whiz in Training
212 posts since Sep 2008
Reputation Points: 47
Solved Threads: 31
 

So the same command works for tcp netstat -a| grep tcp| wc -l ?
or i can use netstat -at| wc -l and netstat -au| wc -l ?
I tried them and got difrent results :D

Elandir
Newbie Poster
6 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Well both of those methods are imperfect for counting connections, but you're probably getting a more accurate result with grep in this case, because it won't count the first two lines (if your output is like mine)

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State


Also consider that the result may actually be different from one moment to the next.

Here's perhaps a more accurate way to do it:

echo "TCP Connections: $(netstat -ant|awk 'END {print NR-2}')"
echo "UDP Connections: $(netstat -anu|awk 'END {print NR-2}')"


OR I like to do things where we're getting multiple numbers from the same output, in more of a snapshot form since the numbers CAN change from one moment to the next. Something like this may be more appropriate, depending on the ultimate goal of your script:

NETSTAT=$(netstat -an)
echo "TCP: $(echo "$NETSTAT"|grep -c ^tcp)"
echo "UDP: $(echo "$NETSTAT"|grep -c ^udp)"
Gromit
Posting Whiz in Training
212 posts since Sep 2008
Reputation Points: 47
Solved Threads: 31
 

yeah, i thought they may differ because of the two first lines :D

Thanks for the help:)

Elandir
Newbie Poster
6 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You