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}' Code:
10.20.92.1 Code:
snmpget -cpublic -v2c $IPADDRESS SysName.0 Is there any way to do this at the command line, or do I need to script it? Many Thanks, DR |
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 |
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}') Edit: too late... arungoodboy beat me! ;) |
Sweet!
Works a treat, thank you very much for the rapid answer:-) Edit: Thank you both :-D |
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}'`; 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 |
Sorry - please ignore - just figured out to use echo -n !!
Why does that always happen as soon as I ask a question!? :-D |
Quote:
|
All times are GMT -5. The time now is 03:07 PM. |