LinuxQuestions.org
Register a domain and help support LQ
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 02-05-2009, 07:09 AM   #1
DRendar1978
LQ Newbie
 
Registered: Feb 2009
Distribution: SME Server (Centos based)
Posts: 4

Rep: Reputation: 0
Question A Question about PIPING


Hi All, this is my first post on LQ, so please be gentle.

I Have searched on the LQ forums and also done some general googleing on the subject but I'm starting to go cross-eyed.

Basically I need to know how to pipe a multi-line output / file to a command, and have that command run against each line.

e.g. I want to know the sysname of every network device in a specific subnet.

So first I use nmap to ping scan the subnet then awk to strip out only the IP addresses of the live devices

Code:
nmap -sP 10.20.92.0/24 | awk '/appears to be up/ {print $2}'
This gives:
Code:
10.20.92.1
10.20.92.2
10.20.92.3
10.20.92.99
10.20.92.190
10.20.92.191
10.20.92.194
10.20.92.195
10.20.92.196
10.20.92.197
10.20.92.198
10.20.92.199
10.20.92.200
10.20.92.207
10.20.92.208
10.20.92.209
10.20.92.210
10.20.92.211
10.20.92.212
What I want is for each line of this output to be fed into:
Code:
snmpget -cpublic -v2c $IPADDRESS SysName.0
Obviously each line should replace $IPADDRESS.

Is there any way to do this at the command line, or do I need to script it?

Many Thanks,

DR
 
Old 02-05-2009, 07:14 AM   #2
indeliblestamp
Member
 
Registered: Feb 2006
Distribution: Fedora
Posts: 341
Blog Entries: 3

Rep: Reputation: 39
You can put it in a for loop, like this:
Code:
for i in `nmap -sP 192.168.0.0/24  | awk '/appears to be up/ {print $2}'`; do echo "$i appears to be up"; done
(Replace the 'echo' part with your snmp command, and use $i where you need the ipaddress).
 
Old 02-05-2009, 07:16 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
First of all... Hi and welcome to LQ!

You need just a for loop, like this:
Code:
for IPADDRESS in $(nmap -sP 10.20.92.0/24 | awk '/appears to be up/ {print $2}')
do
  snmpget -cpublic -v2c $IPADDRESS SysName.0
done
you can write this directly at the shell prompt (you will see the secondary prompt ">" until the loop is complete) or put this in a shell script, at your pleasure.

Edit: too late... arungoodboy beat me!

Last edited by colucix; 02-05-2009 at 07:17 AM.
 
Old 02-05-2009, 07:23 AM   #4
DRendar1978
LQ Newbie
 
Registered: Feb 2009
Distribution: SME Server (Centos based)
Posts: 4

Original Poster
Rep: Reputation: 0
Thumbs up

Sweet!

Works a treat, thank you very much for the rapid answer:-)

Edit: Thank you both :-D

Last edited by DRendar1978; 02-05-2009 at 07:24 AM.
 
Old 02-05-2009, 07:37 AM   #5
DRendar1978
LQ Newbie
 
Registered: Feb 2009
Distribution: SME Server (Centos based)
Posts: 4

Original Poster
Rep: Reputation: 0
Sorry, just a very quick extra - any way to prevent a \n character from preceeding an output?

i.e. if I do:

Code:
for i in `nmap -sP 10.20.92.0/24 | awk '/appears to be up/ {print $2}'`;
  do echo -e "$i\t";
  snmpget -cpublic -v2c -Ov $i sysName.0;
done
It will print each snmpget output on a new line - any way to get to to print immediately after the "$i\t" ?

Sorry for such a basic question, but it is something I've tried to figure out before and failed - do I need to use some kind of trim() function maybe? (I'm primarily a PHP guy! )

Thanks again in advance.

DR
 
Old 02-05-2009, 07:41 AM   #6
DRendar1978
LQ Newbie
 
Registered: Feb 2009
Distribution: SME Server (Centos based)
Posts: 4

Original Poster
Rep: Reputation: 0
Sorry - please ignore - just figured out to use echo -n !!

Why does that always happen as soon as I ask a question!? :-D
 
Old 02-05-2009, 08:16 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by DRendar1978 View Post
Why does that always happen as soon as I ask a question!? :-D
He he... just because your brain is busy while you're formulating a question! Then you can start to think a solution..
 
  


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
Stoopid n00b piping question jholovacs Linux - General 10 01-12-2009 05:51 PM
piping question trist007 Linux - Newbie 5 06-05-2008 08:28 PM
Very difficult piping question... 3saul Linux - Software 1 02-28-2006 01:49 AM
Help with piping mijohnst Linux - General 7 10-21-2005 05:14 PM
Piping question OtisLinux Linux - Software 1 02-12-2004 02:38 PM


All times are GMT -5. The time now is 06: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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration