简体   繁体   English

bash 在我的脚本中将 ip 地址传递给 URL

[英]bash pass ip address to a URL in my script

http://www.geoiptool.com/en/?IP= http://www.geoiptool.com/zh/?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.我需要一些帮助来编写一个 bash 脚本,该脚本允许我将任何给定的 IP 传递给这个 URL。上面的站点是 URL 我想调用我的脚本。 Can someone get me started?有人可以让我开始吗? Thanks!谢谢!

I'd like to use my script like this: echo IP_ADDRESS |我想像这样使用我的脚本: echo IP_ADDRESS | geoiptool地理工具

When I echo an IP and pipe it to my script, I get this error below.当我将 IP 和 pipe 回显到我的脚本时,出现以下错误。 Is that because the pipe command is not installed?是因为没有安装pipe命令吗?

-bash-3.2$ echo 173.192.108.135 | -bash-3.2$ 回声 173.192.108.135 | geo -bash: echo: write error: Broken pipe geo -bash: echo: 写入错误: 损坏 pipe

You can use the read command to read from stdin and store the address in a variable.您可以使用read命令从stdin读取并将地址存储在变量中。 Then use wget to issue the HTTP request:然后使用wget发出HTTP请求:

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

Okay.好的。

You can use the read command to read a line from stdin and assign it to a variable:您可以使用read命令从stdin读取一行并将其分配给一个变量:

read variable
echo "You said: $variable"

And curl is a commonly installed command-line tool for interacting with HTTP URLs:curl是一个通常安装的命令行工具,用于与 HTTP URL 进行交互:

curl http://www.google.com/

(You can also use wget or lynx or another tool, depending on your requirements). (您也可以使用wgetlynx或其他工具,具体取决于您的要求)。

So a very simple script might look like:所以一个非常简单的脚本可能看起来像:

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

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.您可以使用 $1 来引用第一个参数,使用 $2 来引用第二个,等等。

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

Then you just fire it by:然后你只需通过以下方式触发它:

geoiptool <IP>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM