EvilZone

Programming and Scripting => Scripting Languages => Topic started by: neusbeer on January 13, 2012, 05:35:51 PM

Title: bash pipe problem
Post by: neusbeer on January 13, 2012, 05:35:51 PM
(not sure if this is the right place to ask)


I have a small problem with pipe in linux.


case:
if have a script wich output for me a IP adres and I want to pipe it to whatweb.


script is a simple way to output ip from a domain.
so I tried


getip domain.com | whatweb


but whatweb doesn't get the IP which is given bij script 'getip'.
do I need to put a variable after whatweb
like - or "$1" or something.
Kinda stuck here,...
Title: Re: bash pipe problem
Post by: xzid on January 13, 2012, 06:08:59 PM
you need to read from stdin, thats what that pipe does sends the output from a command to the input of another.

ex:

Code: [Select]
$ foo() { echo "10.10.10.10"; }
$ foo
10.10.10.10
$ bar() { read IP; echo "The IP is: $IP"; }
$ bar
this is my input
The IP is: this is my input
$ foo | bar
The IP is: 10.10.10.10

if you wanted to use $1, you could do something like:

Code: [Select]
$ foo() { echo "10.10.10.10"; }
$ bar() { echo "The IP is: $1"; }
$ bar $(foo)

if you used |+$1, you could use xargs:

Code: [Select]
$ foo() { echo "10.10.10.10"; }
$ cat bar.sh
#!/bin/bash
echo "The IP is: $1"
$ foo | xargs ./bar.sh
The IP is: 10.10.10.10

although xargs won't reconize bash functions. There's a million different ways to do the same thing in bash.

Title: Re: bash pipe problem
Post by: neusbeer on January 13, 2012, 08:04:44 PM
hmm I'm not 100% getting the clue sorry..


here's the script:

getip.sh
Code: [Select]
#!/bin/bash
ping "$1" 64 1 | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | uniq


now if I type
$ getip.sh neusbeer.nl
130.12.321.100


it will result in my ip as output now I want to direct this ip to
WhatWeb (Wich identify the server,ect).
whatweb's normaly commands with
./whatweb <ip>


How can I resolve this in such I can combine various outputs from my
small bash scripts.

Title: Re: bash pipe problem
Post by: xzid on January 13, 2012, 08:25:20 PM
Code: [Select]
whatweb $(getip.sh neusbeer.nl)

?
Title: Re: bash pipe problem
Post by: neusbeer on January 13, 2012, 09:16:35 PM
ahh yes.. It works! thnxs..


But this don't work with multiply lines I guess?
ahh well.. got something to work with.. thankyou!
Title: Re: bash pipe problem
Post by: xzid on January 13, 2012, 09:38:40 PM
But this don't work with multiply lines I guess?
ahh well.. got something to work with.. thankyou!

no but this will

Code: [Select]
cat iplist.txt | while read IP; do whatweb $IP; done
cat iplist.txt | xargs -L1 whatweb
Title: Re: bash pipe problem
Post by: neusbeer on January 13, 2012, 09:39:34 PM
yes this I often use..
thnxs for the tips  ;D