LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-03-2015, 07:28 AM   #1
Revenge7
Member
 
Registered: Mar 2015
Posts: 38

Rep: Reputation: Disabled
Counting IP connection number


Hi guys!

I'm writing a script for counting how many connection from each IP (source) to destination IP is established. How can I obtain that number? I mean, the result should be like

2015-06-03_14:08
Source Destination Count
127.0.0.1 127.0.0.1 3
127.0.0.1 127.0.0.4 2
10.11.82.94 10.11.77.95 7

Thank you for your attention!

p.s.: I just need to know how to calculate count number, I've already gain the table from netstat.

Last edited by Revenge7; 06-03-2015 at 07:32 AM.
 
Old 06-03-2015, 10:51 AM   #2
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Fedora, CentOS, Scientific Linux
Posts: 275

Rep: Reputation: 10
If you can get a list looking a bit like this:

Code:
10.11.82.94 10.11.77.95
127.0.0.1 127.0.0.1
127.0.0.1 127.0.0.4
127.0.0.1 127.0.0.1
10.11.82.94 10.11.77.95
127.0.0.1 127.0.0.1
10.11.82.94 10.11.77.95
10.11.82.94 10.11.77.95
127.0.0.1 127.0.0.4
10.11.82.94 10.11.77.95
10.11.82.94 10.11.77.95
10.11.82.94 10.11.77.95
You should be able to use this "sort [the list] | uniq -c"
Either the list is a file or you can pipe your output to sort and uniq.


UPDATE: Although the 'Count' result will be in column 1.
Code:
7 10.11.82.94 10.11.77.95
3 127.0.0.1 127.0.0.1
2 127.0.0.1 127.0.0.4

UPDATE2: If you really need to get the count value in the last column you can use awk.
Code:
sort [the list] | uniq -c | awk -F, '{print $2,$3,$1}' FS=" "

Last edited by Basher52; 06-03-2015 at 04:04 PM.
 
1 members found this post helpful.
Old 06-04-2015, 02:56 AM   #3
Revenge7
Member
 
Registered: Mar 2015
Posts: 38

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Basher52 View Post
If you can get a list looking a bit like this:

Code:
10.11.82.94 10.11.77.95
127.0.0.1 127.0.0.1
127.0.0.1 127.0.0.4
127.0.0.1 127.0.0.1
10.11.82.94 10.11.77.95
127.0.0.1 127.0.0.1
10.11.82.94 10.11.77.95
10.11.82.94 10.11.77.95
127.0.0.1 127.0.0.4
10.11.82.94 10.11.77.95
10.11.82.94 10.11.77.95
10.11.82.94 10.11.77.95
You should be able to use this "sort [the list] | uniq -c"
Either the list is a file or you can pipe your output to sort and uniq.


UPDATE: Although the 'Count' result will be in column 1.
Code:
7 10.11.82.94 10.11.77.95
3 127.0.0.1 127.0.0.1
2 127.0.0.1 127.0.0.4

UPDATE2: If you really need to get the count value in the last column you can use awk.
Code:
sort [the list] | uniq -c | awk -F, '{print $2,$3,$1}' FS=" "
Thank you ! It solved my problem.
 
Old 06-04-2015, 05:28 AM   #4
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: Fedora, CentOS, Scientific Linux
Posts: 275

Rep: Reputation: 10
Your welcome, glad to help

Last edited by Basher52; 06-04-2015 at 05:31 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
counting number of occurances in a column atjurhs Linux - Newbie 5 01-09-2013 03:08 PM
[SOLVED] Counting the number of cfiles in a project Ajit Gunge Linux - Newbie 1 06-15-2010 08:09 AM
counting the output packet number zhoufanking Programming 4 07-06-2008 02:14 AM
Counting number of system reboots rbh123 Linux - Newbie 2 11-22-2007 04:28 AM
counting number of files akin81 Linux - Newbie 6 03-25-2004 02:53 PM


All times are GMT -5. The time now is 04:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration