首页 > 解决方案 > 使用 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

标签: bashif-statementcurldns

解决方案


试试这个:

#!/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

推荐阅读