LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   A Question about PIPING (https://www.linuxquestions.org/questions/linux-newbie-8/a-question-about-piping-702461/)

DRendar1978 02-05-2009 06:09 AM

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

indeliblestamp 02-05-2009 06:14 AM

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).

colucix 02-05-2009 06:16 AM

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! ;)

DRendar1978 02-05-2009 06:23 AM

Sweet!

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

Edit: Thank you both :-D

DRendar1978 02-05-2009 06:37 AM

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

DRendar1978 02-05-2009 06:41 AM

Sorry - please ignore - just figured out to use echo -n !!

Why does that always happen as soon as I ask a question!? :-D

colucix 02-05-2009 07:16 AM

Quote:

Originally Posted by DRendar1978 (Post 3432947)
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.. ;)


All times are GMT -5. The time now is 03:07 PM.