Author Topic: bash pipe problem  (Read 1673 times)

0 Members and 1 Guest are viewing this topic.

Offline neusbeer

  • Knight
  • **
  • Posts: 223
  • Cookies: 11
  • Beer makes you stronger XD
    • View Profile
    • http://www.facebook.nl/hackneus
bash pipe problem
« 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,...
--Neusbeer

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: bash pipe problem
« Reply #1 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.


Offline neusbeer

  • Knight
  • **
  • Posts: 223
  • Cookies: 11
  • Beer makes you stronger XD
    • View Profile
    • http://www.facebook.nl/hackneus
Re: bash pipe problem
« Reply #2 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.

--Neusbeer

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: bash pipe problem
« Reply #3 on: January 13, 2012, 08:25:20 pm »
Code: [Select]
whatweb $(getip.sh neusbeer.nl)

?

Offline neusbeer

  • Knight
  • **
  • Posts: 223
  • Cookies: 11
  • Beer makes you stronger XD
    • View Profile
    • http://www.facebook.nl/hackneus
Re: bash pipe problem
« Reply #4 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!
--Neusbeer

Offline xzid

  • Knight
  • **
  • Posts: 329
  • Cookies: 41
    • View Profile
Re: bash pipe problem
« Reply #5 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

Offline neusbeer

  • Knight
  • **
  • Posts: 223
  • Cookies: 11
  • Beer makes you stronger XD
    • View Profile
    • http://www.facebook.nl/hackneus
Re: bash pipe problem
« Reply #6 on: January 13, 2012, 09:39:34 pm »
yes this I often use..
thnxs for the tips  ;D
--Neusbeer