有时候有需求要查询连接到某接口的ip地址(比如ss的连接用户),只是查询ip地址的话,Toyo的脚本可以办到。不过他的脚本只给出了ip地址,没有给出ip所在地,自己研究了一下,找了几个脚本,给综合了一下,弄了个自己的脚本。
一、checkip.sh
这个脚本是读取某个存有ip地址的文件,比如ip.txt,然后查询其中的ip地址的归属地的脚本。
1 |
vi checkip.sh |
写入下面的脚本内容并保存退出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#!/bin/bash #Purpose: check ip location ipp (){ exec < $1 while read a do sring=`curl -s http://freeapi.ipip.net/${a} | sed 's/\[\|\]\|,\|"//g;s/$/\n/g'` echo $a $sring sleep 1 done } case $1 in -f) shift ipp $1 ;; -i) shift sring=`curl -s http://freeapi.ipip.net/${a} | sed 's/\[\|\]\|,\|"//g;s/$/\n/g'` echo $1 $sring ;; *) echo "[Help] $0 need -f or -i -f ------- argument is a file -i ------- argument is a IP [For example]: $0 -f filename $0 -i ipadress " ;; esac |
其用法为:
1 2 3 4 |
bash checkip.sh -f filename #filename为文件名,可以为ip.txt,则查询该文件中的ip地址的归属地,如有修改,ip.sh中也要相应改动。 bash checkip.sh -i ip ip为ip地址,比如8.8.8.8。 |
二、ip.sh
获取端口连接的ip并调用checkip.sh脚本查询其归属地
1 |
vi ip.sh |
写入下面的脚本内容,并保存退出。
1 2 3 4 |
localIP=$(ip a|grep -w 'inet'|grep 'global'|sed 's/^.*inet //g'|sed 's/\/[0-9][0-9].*$//g') netstat -anp |grep 'ESTABLISHED' |grep "${localIP}:443 " |awk '{print $5}' |awk -F ":" '{print $1}' |sort -u |grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}" > ip.txt #其中443为要查询的端口号。 bash checkip.sh -f ip.txt |
修改shell文件的权限
1 |
chmod +x ip.sh |
执行下面命令可以查看连接到443端口的ip归属地:
1 2 3 |
./ip.sh #或者 bash ip.sh |
运行结果:
至于如何剔除掉非ss客户端的ip,Toyo的脚本里面有方法。
——————————
更新日志:
2018.09.22
修改了查询IP的方式,现在只会出现连接到本机的IP,不会出现所访问的IP了。
参考网页:
1、查询ip归属地的shell脚本
2、http://tyr.gift/ss-monitor.html
3、终端查看 ip 归属地的小工具
- 本文固定链接: https://www.ljchen.com/archives/461
- 转载请注明: ljchen 于 三言两语 发表