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 11-14-2014, 08:43 AM   #1
vipinsqa
Member
 
Registered: Oct 2014
Posts: 40

Rep: Reputation: Disabled
Print fields to a file based on protocol type from netstat.


Hi Guys,

I am trying to fetch the network connections on Centos host and write to a log. However, as UDP doesnt have a connection state, the report has a problem with displaying data as tcp has a state in the corresponding field. I am trying to handle this using below:-

if [$(netstat -anputw | awk '{print $1}')=="tcp"] then
netstat -anputw | awk '{print $1,",",$4,",",$5,",",$6,",",$7}' >> $HOME/MyLog/connections_$HOSTNAME.csv

elif [$(netstat -anputw | awk '{print $1}')=="udp"] then
netstat -anputw | awk '{print $1,",",$4,",",$5,",",",",$6}' >> $HOME/MyLog/connections_$HOSTNAME.csv
fi

So, I am trying to see wherever the protocol from netstat command is tcp, it prints the required fields and when its udp, then the corresponding state field should be empty.

However, it doesnt work. Any idea what could be wrong here?
 
Old 11-14-2014, 09:40 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,577
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
The test commands should have spaces between the brackets and the items inside them.

The "then" and "elif" should be preceded by semicolon if on the line with the test - really they should be on the line on which you're doing the awk instead (i.e. start of following line rather than end of preceding line).

I'd suggest using "=" rather than "==" in your test AND putting a space before and after the "=" to separate the elements.

However the main issue is your conditional is testing the entire output of netstat as if it were one long line. You need to create a while loop so it processes each line as it gets to it from the netstat output.

If you do a web search for terms:
while read on netstat output
You'll find various examples of while loops related to netstat output.
 
  


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
How to compare two files using four fields and print out the mismatched fields ? avatar007 Programming 3 05-04-2014 12:35 PM
bash: renaming file extension based on actual file type alekone Linux - General 9 12-28-2008 10:12 AM
how not to print the 4th field from a text file with six fields livetoday Red Hat 3 10-02-2007 02:19 PM


All times are GMT -5. The time now is 01:01 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