gpt4 book ai didi

bash - 读取包含 IP 的文件并将国家代码写入新的 IP.txt 文件

转载 作者:行者123 更新时间:2023-11-29 09:25:36 25 4
gpt4 key购买 nike

我需要有关脚本的帮助,该脚本正在查找文件 (serverlist.txt) 的 ip:port。

serverlist.txt 包含IP:端口号。

服务器列表.txt

1.2.3.4:27967
5.6.7.8:27962
6.7.8.9:27968
2.5.6.7:27964

脚本应该切断端口,然后使用 geoiplookup 请求国家代码。完成后,每个 ip 都应该在一个新的自己的文件中有国家代码,如下所示:

1.2.3.4:27967.txt

US

5.6.7.8:27962.txt

DE

这是我到目前为止的代码:

getcountry.sh

#!/bin/bash
cat serverlist.txt |while read $socket; do cut -f1 -d":">ip.txt; done;
cat ip.txt | while read ip; do geoiplookup $ip | awk -v FS="(GeoIP Country Edition: |,)" '{print $2}'>>countrycode.txt ; done;

我可以获得 ips 和国家代码。但我不知道如何将它们匹配到新文件中。谢谢你的帮助。 ;)

最佳答案

我们不要创建中介 ip.txt文件并循环遍历 serverlist.txt 中的内容.

通过将内部字段分隔符 (IFS) 设置为 : , 你可以同时提取 ipport .然后,运行 geoiplookup反对它,管道到awk正如您所做的那样,将输出存储在变量 $country 中.

最后写入以ip命名的文件中:

while IFS=":" read ip port; do
country=$(geoiplookup "$ip" | awk -v FS="(GeoIP Country Edition: |,)" '{print $2}')
printf "$country\n" >> $ip:$port.txt
done < serverlist.txt

这与您所做的非常相似,您只需要引用变量 $ip存储名称。

cat ip.txt | while read ip; do  ... >> $ip.txt ; done;
# ^^^

注意我使用的语法是 while read; do ... done < file而不是 cat file | while ... ,这样您就不会打开子 shell,并且在循环内定义的所有变量都可以在其外部使用。

关于bash - 读取包含 IP 的文件并将国家代码写入新的 IP.txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37024580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com