25

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
  • 98,183
  • 15
  • 197
  • 293
Tara S Volpe
  • 479
  • 1
  • 5
  • 13

3 Answers3

37

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
  • 105,631
  • 46
  • 284
  • 425
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
Ali Razmdideh
  • 5,710
  • 2
  • 34
  • 51
  • 2
    @TaraSVolpe I'm glad I could help you – Ali Razmdideh Sep 22 '17 at 13:11
  • 2
    This answer relies on the layout of this website, while ipinfo.io uses a well-defined JSON format. I don't understand why this answer is more upvoted. – Maya Sep 23 '17 at 15:16
  • 2
    @NieDzejkob Maybe because ipinfo.io/org only give the AS number but not the name of the ISP – SebMa Mar 30 '18 at 17:31
  • 1
    @SebMa When I ran Byte's answer on my machine in Ubuntu in Windows 10 (WSL) it returned the AS number plus the ISP name using `curl ipinfo.io/org`. None-the-less I up-voted both answers and the question because it's all good :) – WinEunuuchs2Unix Mar 30 '18 at 17:44
  • @NieDzejkob `curl ipinfo.io/org` does not output the full name of the AS but `curl -s ipinfo.io/ASxxx | grep as-name` does :) – SebMa Mar 30 '18 at 17:49
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,081
  • 3
  • 28
  • 38
  • 1
    For me it's not working. I just tested all answers in both Ubuntu in Windows 10 (WSL) and Ubuntu 16.04 with Kernel 4.14.27. Your option 1 above (Byte's answer) returns `AS852 TELUS Communications Inc.`. Your option 2 above returns nothing. The accepted answer uses `curl -s https://www.whoismyisp.org | grep -oP '\bisp">\K[^<]+'` and returns `Telus Communications` which is a limited version of Byte's answer but still good. This is one of those **YMMV** (Your Mileage May Vary) answers. – WinEunuuchs2Unix Mar 30 '18 at 18:02
  • 2
    Apparently it depends on the provider or on what `ipinfo.org` has in their data collection. For me `curl -s ipinfo.io/org` gives `AS3320 Deutsche Telekom AG` while `curl -s ipinfo.io/AS3320 | grep as-name` gives `DTAG` (after a while). Also, the latter is again parsing HTML output (error-prone!). So I'll stick with ByteCommander's answer. – PerlDuck Mar 30 '18 at 18:03
  • @PerlDuck When I use Germany's `AS3320` I get the same `DTAG` output you get. But I wonder if you use Canada's `curl -s ipinfo.io/AS852 | grep as-name` you get null output like me. – WinEunuuchs2Unix Mar 30 '18 at 18:07
  • @WinEunuuchs2Unix Yes, same for me. No output. I reckon `curl -s ipinfo.io/org` returns data based on the requesting IP while `curl -s ipinfo.io/AS3320` returns data based on the given parameter (ignoring the request's IP). Anyway. Parsing HTML without a proper parser is almost always a bad idea. Do you know this [famous answer](https://stackoverflow.com/a/1732454/5830574) on [SO]? It's fun to read. – PerlDuck Mar 30 '18 at 18:23
  • @PerlDuck It is a fun read especially all the weird characters at the end. Throwing caution to the wind I did parse HTML code in bash: https://askubuntu.com/questions/900319/code-version-control-between-local-files-and-au-answers/900609#900609 – WinEunuuchs2Unix Mar 30 '18 at 18:40
  • @PerlDuck Hi, I've updated my answer so has to parse ASCII output instead of HTML output – SebMa Apr 16 '18 at 09:09
  • @WinEunuuchs2Unix Hi, I've updated my answer so has to parse ASCII output instead of HTML output – SebMa Apr 16 '18 at 09:30