30

I want to use my Internet Service Provider's name in a script, and I don't know how can I do this.

Please help me, thanks in advance.

terdon
  • 104,119

4 Answers4

43

You could use e.g. the services of ipinfo.io to determine your public IP including some additional information like the provider company name.

The site can be normally visited in your browser, but if you query it from the command-line with e.g. curl, they respond in a clean and well-defined JSON format so that you don't need to parse any HTML:

$ curl ipinfo.io
{
  "ip": "xxx.xxx.xxx.xxx",
  "hostname": "xxxxxxxxxxxxxxxxxxxxxxxxxxx.xx",
  "city": "xxxxxxxx",
  "region": "xxxxxxxxxx",
  "country": "xx",
  "loc": "xxx.xxxx,xxx.xxxx",
  "org": "xxxxxxxxxxxx",
  "postal": "xxxxx"
}

To only show one value, you can directly send a request to the respective path. E.g. for the ISP name (org), try this:

curl ipinfo.io/org

Inspired by this answer.

Byte Commander
  • 110,243
28

You can use many websites provided to find your ISP name. One of them is whoismyisp.

To get your ISP name in a bash script, you can get this site by something like curl.

header='--header=User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11'
wget "$header" -q -O - whoismyisp.org | grep -oP -m1 '(?<=block text-4xl\">).*(?=</span)'

Also you can find ISP of any desired IPs with this command:

header='--header=User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11'
wget "$header" -q -O - whoismyisp.org/ip/xxx.xxx.xxx.xxx | grep -oP -m1 '(?<=block text-4xl\">).*(?=</span)'

Where xxx.xxx.xxx.xxx is that IP you want to find its ISP.


Additional information: You can find your IP by bash with this command (that may be helpful for scripts):

dig +short myip.opendns.com @resolver1.opendns.com
2

I actually really like to use api.bgpview.io and ifconfig.me:
A bash one-liner (requires apt install curl jq) is:

curl -L -s "https://api.bgpview.io/ip/$(curl -s ifconfig.me)" | jq -r '.data.prefixes | .[] | (.prefix | tostring) + " " + (.ip | tostring) + " " + (.asn.asn | tostring) + " " + .asn.name + " " + .asn.description'
zx485
  • 2,865
1

First I fetch the Autonomous System number :

$ curl -s ipinfo.io/org
AS2094 Renater

Then I fetch the full name of that AS :

$ curl -s ipinfo.io/$(curl -s ipinfo.io/org | cut -d" " -f1) | awk '/as-name/{print$NF}'

$ whois $(curl -s ipinfo.io/org | cut -d" " -f1) | awk -F: 'BEGIN{IGNORECASE=1}/(as-?name|org-?name):/{sub("^  *","",$2);print$2}'
FR-TELECOM-MANAGEMENT-SUDPARIS
Renater
SebMa
  • 2,927
  • 5
  • 35
  • 47