LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-09-2009, 02:47 PM   #1
madpear
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Rep: Reputation: 0
Awk within Awk


I am trying to get the result of a netstat -na | grep specific_ip, run remotely via a batch script calling plink.exe to be formatted into a set of ip/port pairs as:

Code:
IP- client_ip_1
Port- client_port_1

IP- client_ip_2
Port- client_port_2
netstat -na outputs like so, IPs have been changed:

Code:
tcp        0      0 ::ffff:server_ip:22        ::ffff:client_ip_1:client_port_1   ESTABLISHED
tcp        0     52 ::ffff:server_ip:22        ::ffff:client_ip_2:client_port_2   ESTABLISHED
The problem I have is that when I use the command:

Code:
"C:\Program Files\PuTTY\plink.exe" "-l" "user" "-pw" "pass" server_ip "netstat -na | grep 172.16 | awk -F: '{print \"IP-\t\"$8\"\nPort-\t\"$9}'"
My output shows:

Code:
IP- client_ip_1
Port- client_port_1   ESTABLISHED

IP- client_ip_2
Port- client_port_2   ESTABLISHED
I want to remove the word ESTABLISHED, but since I am running this via plink I would like to do it in one command. I tried to use awk within awk to accomplish this:

Code:
"C:\Program Files\PuTTY\plink.exe" "-l" "user" "-pw" "pass" server_ip "netstat -na | grep 172.16 | awk -F: '{print \"IP-\t\"$8\"\nPort-\t\"; print awk '{print $1}' $9}'"
But I got an error as seen in the attached jpg.

Any solutions/suggestions are welcome.
Attached Thumbnails
Click image for larger version

Name:	error_awk.JPG
Views:	25
Size:	16.2 KB
ID:	1912  

Last edited by madpear; 11-09-2009 at 03:08 PM.
 
Old 11-09-2009, 03:23 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Do you have reason to believe that AWK can be called from within another AWK invocation? I would have assumed no.

I'm missing what you mean by "1 command"---why not this:

awk '{print $4}' | awk -F: <the rest of your code>
 
Old 11-09-2009, 03:23 PM   #3
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Try:

Code:
"C:\Program Files\PuTTY\plink.exe" "-l" "user" "-pw" "pass" server_ip "netstat -na | grep 172.16 |sed 's/ESTABLISHED//'| awk -F: '{print \"IP-\t\"$8\"\nPort-\t\"$9}'"
 
Old 11-09-2009, 03:27 PM   #4
madpear
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Original Poster
Rep: Reputation: 0
I meant I wanted to just have one command to send via plink.

Anyway, that did the trick, here's the winning line:

Code:
"C:\Program Files\PuTTY\plink.exe" "-l" "user" "-pw" "pass" 10.12.1.11 "netstat -na | grep 172.16 | awk '{print $5}' | awk -F: '{print \"IP-\t\"$4\"\nPort-\t\"$5}'"
In fact, both Disillusionist's and pixellany's suggestions worked.

Last edited by madpear; 11-09-2009 at 03:29 PM.
 
Old 11-09-2009, 05:18 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
forget about grep when you use awk. its redundant.
Code:
"C:\Program Files\PuTTY\plink.exe" "-l" "user" "-pw" "pass" 10.12.1.11 "netstat -na | awk -F":" '/172.16/{split($5,a,":");print \"IP-\t\"a[4]\"\nPort-\t\"a[5]}'"
 
Old 11-10-2009, 02:14 AM   #6
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Hey ghostdog,

Nice awk example.

Would the double quotes around the field delimiter cause issues with passing to plink?
 
Old 11-10-2009, 02:23 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
don't understand. do you mean -F":" ?? and why is it passing to plink? isn't plink's output passed to awk.?
 
Old 11-10-2009, 02:52 AM   #8
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
As I read it, neststat is being passed to awk, and that is being passed as a parameter to plink.

The plink parameters are enclosed in double quotes, which would mean that the opening quote for the -F":" would close the parameter.

The rest of the double quotes in the awk statement are inside single quotes so I would guess that they wouldn't cause an issue.
 
Old 11-10-2009, 03:16 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
i see. the whole part of netstat onwards is part of an argument to plink. well if it doesn't work, escape !
 
  


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 question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
awk , I need help for awk, just a display function mcandy General 1 12-15-2008 12:21 PM
using awk on a awk result jpgauvin Programming 1 12-15-2008 03:57 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
Some comments on awk and awk scripts makyo Programming 4 03-02-2008 05:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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