Jim
Jim

Reputation: 161

bash pass ip address to a URL in my script

http://www.geoiptool.com/en/?IP=

I need some help to write a bash script that will allow me to pass any given IP to this URL. The site above is URL I'd like to my script to call. Can someone get me started? Thanks!

I'd like to use my script like this: echo IP_ADDRESS | geoiptool

When I echo an IP and pipe it to my script, I get this error below. Is that because the pipe command is not installed?

-bash-3.2$ echo 173.192.108.135 | geo -bash: echo: write error: Broken pipe

Upvotes: 1

Views: 2730

Answers (3)

Alex Howansky
Alex Howansky

Reputation: 53553

I'd recommend using a command line parameter instead of reading from stdin. You can use $1 to refer to the first parameter, $2 for the second, etc.

#!/bin/bash
curl -s http://www.geoiptool.com/en/?IP=$1

Then you just fire it by:

geoiptool <IP>

Upvotes: 1

larsks
larsks

Reputation: 311526

Okay.

You can use the read command to read a line from stdin and assign it to a variable:

read variable
echo "You said: $variable"

And curl is a commonly installed command-line tool for interacting with HTTP URLs:

curl http://www.google.com/

(You can also use wget or lynx or another tool, depending on your requirements).

So a very simple script might look like:

URL="http://www.geoiptool.com/en/?IP="
read ipaddr
curl --silent "$URL$ipaddr"

Upvotes: 1

Adam Liss
Adam Liss

Reputation: 48290

You can use the read command to read from stdin and store the address in a variable. Then use wget to issue the HTTP request:

read ip
wget http://www.geoiptool.com/en/?IP=$ip

Upvotes: 1

Related Questions