bash - 使用 Bash 和 Curl 选择工作域
问题描述
我正在尝试找到一个正在运行并使用它的域,但理想情况下是一个随机选择的域。现在它似乎只使用 if 语句中的最后一个域。我能做些什么来改变这一点,而不是使用随机选择的域。谢谢
#!/bin/bash
One() {
ip=$(curl https://ipapi.co/ip)
country=$(curl https://ipapi.co/$ip/country/)
echo $ip - $country 1
}
Two() {
ip=$(curl https://api.db-ip.com/v2/free/self/ipAddress)
country=$(curl https://api.db-ip.com/v2/free/$ip/countryCode)
echo $ip - $country 2
}
Three() {
ip=$(curl https://api.ipdata.co/ip?api-key=test)
country=$(curl https://api.ipdata.co/$ip/country_code?api-key=test)
echo $ip - $country 3
}
Fore() {
ip=$(curl http://api.ipaddress.com/myip)
country=$(curl -s http://api.ipaddress.com/iptocountry)
echo $ip - $country 4
}
if curl -k --max-time 10 --head --request GET https://ipapi.co/ip | grep "200 OK" > /dev/null; then
One
elif curl -k --max-time 10 --head --request GET https://api.db-ip.com/v2/free/self/ipAddress | grep "200 OK" > /dev/null; then
Two
elif curl -k --max-time 10 --head --request GET https://api.ipdata.co/ip?api-key=test | grep "200 OK" > /dev/null; then
Three
elif curl -k --max-time 10 --head --request GET http://api.ipaddress.com/iptocountry | grep "200 OK" > /dev/null; then
Fore
else
echo
fi
解决方案
试试这个:
#!/bin/bash
One() {
ip=$(curl https://ipapi.co/ip)
country=$(curl https://ipapi.co/$ip/country/)
echo $ip - $country 1
}
Two() {
ip=$(curl https://api.db-ip.com/v2/free/self/ipAddress)
country=$(curl https://api.db-ip.com/v2/free/$ip/countryCode)
echo $ip - $country 2
}
Three() {
ip=$(curl https://api.ipdata.co/ip?api-key=test)
country=$(curl https://api.ipdata.co/$ip/country_code?api-key=test)
echo $ip - $country 3
}
Fore() {
ip=$(curl http://api.ipaddress.com/myip)
country=$(curl -s http://api.ipaddress.com/iptocountry)
echo $ip - $country 4
}
DOMAIN[0]=One # define your Domains
DOMAIN[1]=Two
DOMAIN[2]=Three
DOMAIN[3]=Fore
NR=$((RANDOM % ${#DOMAIN[@]})) # generate pseudorandom
${DOMAIN[$NR]} # call function
推荐阅读
- android - 在底部导航活动/片段中创建选项卡式活动
- python - 当我想将整个网络分成两个模型时,为什么在 Keras 中会发生此错误,“Graph disconnected”?
- c# - 如何清空 CosmosDB 集合并再次填充并保留其指标/日志
- ios - 可以区分用户滚动和编程滚动吗?
- javascript - 我无法使用 NativeScript 在 jsPDF 中渲染图像
- python - Kinit 在 python 控制台中不起作用
- excel - 在 Excel 中绘制图形的公式
- javascript - Mongoose - 在文档中的特定日期执行函数
- php - 如何定义用于用户名而不是电子邮件的属性?
- python - ImportError:无法从“ecdsa”导入名称“SECP256kl”