LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-23-2016, 08:42 AM   #1
hnasr2001
Member
 
Registered: Nov 2012
Posts: 97

Rep: Reputation: Disabled
awk, redirecting to a file


Hello;
I am trying to use tcpdump command to find the number of packet per second, then store it in a file.
I have the code at bellow;
it does open the file, print the number of packets on display but the file stay empty! any help will be appreciated.
---------------------------------------------------
#!/bin/bash
count=0

tcpdump greater 10 and -i eth1 port 30785 | \
awk '{
t=substr($1, 0, 8);
count=count+1
if(t != pt){
print t, count;
print t >> "/home/radarop/Desktop/statistic.txt";
print count >> "/home/radarop/Desktop/statistic.txt";
count = 0;
}
pt=t;
}'
-----------------------------------------------
Thanks in advance
 
Old 11-23-2016, 09:14 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,237

Rep: Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656
IMO - tcpdump is huge and un-needed for a task like this. It doesn't really allow you to do on the spot status checks or analysis. It's more of a 'capture to a file, then analyze the file later' deal.

So, here is a way to look at the number of packets received on an interface every second that outputs to a file with a timestamp every second. With seeing the logic, you should be able to modify it to your needs... ie: filenames, tx packets, interface...

Code:
while true; do
 timestamp=$(date +%s) 
 received_before=$(cat /sys/class/net/eth0/statistics/rx_packets)
 sleep 1
 received_after=$(cat /sys/class/net/eth0/statistics/rx_packets)
 pps=`expr $received_after - $received_before`
 echo "received $pps packets per second"
 echo "$timestamp : received $pps packets per second" >> some_file.txt
done
 
1 members found this post helpful.
Old 11-23-2016, 09:28 AM   #3
hnasr2001
Member
 
Registered: Nov 2012
Posts: 97

Original Poster
Rep: Reputation: Disabled
thak you szboardstretcher;

Is there a way to modify your code to change eth0 to port number 30785?
 
Old 11-23-2016, 09:34 AM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,237

Rep: Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656Reputation: 1656
I didn't see that you were doing that by port. In the case of port traffic I would use 'tcpstat' which should do what you like.

For example, packets per second on an interface:

Code:
tcpstat -i eth0 -o "time: %S pps: %p"
You can do tcpdump style filters as well.

https://linux.die.net/man/1/tcpstat

Last edited by szboardstretcher; 11-23-2016 at 09:39 AM.
 
1 members found this post helpful.
Old 11-23-2016, 09:41 AM   #5
hnasr2001
Member
 
Registered: Nov 2012
Posts: 97

Original Poster
Rep: Reputation: Disabled
szboardstretcher;
Thanks again...
The piece of code that I post works too. It does print the number of packets, only it doesn't redirect to a file, any idea on this issue?
 
Old 11-23-2016, 09:48 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,549

Rep: Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085Reputation: 2085
FWIW, the reason your awk script did not appear to be writing to the file is that output to a file is block-buffered. Nothing is written until you fill a (default) 4096-byte buffer (or close the file). You would have to include an fflush() call to see the output in real time.
Code:
awk '{
    t=substr($1, 0, 8);
    count=count+1
    if(t != pt){
        print t, count;
        print t >> "/home/radarop/Desktop/statistic.txt";
        print count >> "/home/radarop/Desktop/statistic.txt";
        fflush("/home/radarop/Desktop/statistic.txt");
        count = 0;
    }
    pt=t;
}'
 
2 members found this post helpful.
Old 11-23-2016, 09:56 AM   #7
hnasr2001
Member
 
Registered: Nov 2012
Posts: 97

Original Poster
Rep: Reputation: Disabled
Thank you, now it is working...
 
  


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
awk: how can I print out a message to the screen when redirecting the output to file. quanba Programming 8 07-13-2015 01:54 AM
Question about redirecting awk script output to a csv file? xpac Programming 5 05-28-2015 04:02 PM
Problem in redirecting awk output into a file paldebojyoti Linux - General 2 10-06-2012 05:19 PM
redirecting input from file in awk script Trd300 Linux - Newbie 51 09-27-2012 05:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:00 PM.

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
Open Source Consulting | Domain Registration