Skip to content Skip to content


How to find your public IP address with the Linux command line Kā atrast savu publisko IP adresi ar Linux komandrindas

Linux

Sometimes I need to find out what the public IP address of a particular machine is. Dažreiz man ir nepieciešams, lai uzzinātu, kas publiskā IP adrese, jo īpaši mašīna. If I have access to a web browser on that machine I just hope over to a service such as Ja man ir pieejama tīmekļa pārlūku uz šo mašīnu es ceru, ka vairāk nekā uz tādu pakalpojumu kā http://www.whatismyip.com http://www.whatismyip.com or vai myip.dk myip.dk to find this information. atrast šo informāciju. However, on a remote Linux server it is not practical to use the web browser method. Tomēr, tālvadības Linux serveris nav praktiski izmantot interneta pārlūku metodi. So, instead, I use the following hack. Tātad, tā vietā, es izmantot šādas kapāt.

I enter the following line into a Linux command line: Ievadīt šādu rindu stājas Linux komandrindas:

# curl -s myip.dk | grep '"Box"' | egrep -o '[0-9.]+' # Cirtot-s myip.dk | grep "" Box " '| egrep-o' [0-9.] +"
122.172.9.222 122.172.9.222

Pretty useful stuff this. Diezgan noderīgs stuff to. If you need to use this command often it might be a good idea to create a script with the command so that you need not have to remember it. Ja jums ir nepieciešams izmantot šo komandu bieži vien tā varētu būt laba ideja izveidot skriptu ar komandu, lai jums nebūtu to atcerēties. Create a new file using your favorite text editor. Izveidot jaunu failu ar jūsu iecienītākajā teksta redaktorā. Enter the following lines: Ievadiet šādas rindas:

#!/bin/bash #! / bin / bash
curl -s myip.dk | grep '"Box"' | egrep -o '[0-9.]+' cirtot-s myip.dk | grep "" Box " '| egrep-o' [0-9.] +"

Now save the file as something like /usr/bin/myipaddress . Tagad saglabājiet failu kā kaut ko, piemēram, / usr / bin / myipaddress. Grant it executable privileges like this: Tai piešķirtu izpildāmā privilēģijas, piemēram, šādi:

# chmod +x /usr/bin/myipaddress # Chmod + x / usr / bin / myipaddress

Now you have a script that you can access from anywhere on your computer by running myipaddress in the command line. Tagad jums ir skripts, ka jūs varat piekļūt no jebkuras vietas datorā, palaižot myipaddress ar komandrindu. It will output your current public IP address. Tas izejas jūsu pašreizējo publisko IP adresi. This can be quite useful if you use things like IP-based authentication, or if you use a dynamic DNS service. Tas var būt diezgan noderīga, ja Jūs lietojat lietām, piemēram, IP balstītu autentifikāciju vai, ja jūs izmantojat dinamisko DNS pakalpojumu.

Although the above example should work most of the time, there might be times when the service that gives you your public IP address is down or has changed the format with which they display the data, thus breaking the script. Kaut gan iepriekš minētajā piemērā būtu jāstrādā lielāko daļu laika, varētu būt reizes, kad pakalpojums, kas sniedz jums jūsu publiskā IP adrese nav noteikti vai ir mainījis formātu, ar kurām tiem piemīt datiem, tādējādi laužot skriptu. Here's a backup method in case the command shown earlier does not work for you. Lūk rezerves metode, ja komanda rāda agrāk nedarbojas jums. It used the service Tā izmantoja dienests http://www.formyip.com/ http://www.formyip.com/ . .

# links -dump http://www.formyip.com/ | awk "/IP is/{print $NF}" # Links-dump http://www.formyip.com/ | awk "/ IP / (print $ NF)"

You could also use this with lynx : Jūs arī varētu izmantot šo ar lynx:

# lynx -dump http://www.formyip.com/ | awk "/IP is/{print $NF}" # Lynx-dump http://www.formyip.com/ | awk "/ IP / (print $ NF)"

Again, feel free to enter this command into a script like I showed you earlier. Atkal justies brīvi ievadīt šo komandu stāšanās skriptu, piemēram, man parādīja jūs agrāk. It will make using the command a breeze. Tas liks izmantojot komandu brīze.

Posted in Posted in Home Networking Home Networking , , Linux Linux . .

Get Simple Help tutorials just like this one in your email inbox every day - for free! Get Simple Palīdzība konsultācijas tāpat kā šo vienu savā e-pastā ik dienas - bez maksas! Just enter your email address below: Vienkārši ievadiet savu e-pasta adresi:

You can always opt out of this email subscription at any time. Jūs vienmēr varat atteikties no šīs e-pasta abonementu jebkurā laikā.

6 Responses 6 Atbildes

Stay in touch with the conversation, subscribe to the Uzturēt kontaktus ar sarunu, abonēt RSS feed for comments on this post RSS barību komentārus par šo ziņu . .

  1. Mike Mike says saka

    When I copy-pasted the script into my own file, I saw that your quote marks don't match up. Kad es copy-ielīmēts skriptu savu failu, es redzēju, ka jūsu pēdiņām nesakrīt augšu.

    … '[0-9.]+' ... "[0-9.] +"

    I changed the first quote before the [ character to ', making the final product read: Es mainīja pirmo citātu pirms [rakstzīmi ", padarot gala produkts read:

    #!/bin/bash #! / bin / bash
    curl -s myip.dk | grep '"Box"' | egrep -o '[0-9.]+' cirtot-s myip.dk | grep "" Box " '| egrep-o' [0-9.] +"

    By the way, I apologize for the slight messiness of this, but it's very difficult to quote a quote mark. Ko citu, es atvainojamies par nelielu messiness uz to, bet tas ir ļoti grūti citēt citātu zīmes. ;)

  2. spuffler says spuffler saka

    … evidently, the author doesn't read our replies, otherwise, I'd have had a successful execution of the script file, because I would have copied corrected text. ... Acīmredzot, autors nelasa mūsu atbildes, pretējā gadījumā, man ir bijusi sekmīga izpilde skripta fails, jo es būtu kopēt jālabo teksts. ...

  3. Tom says Toms saka:

    Using ifconfig works just as well, you'll still need to grep for the proper lines, and then possibly use sed to extract the actual IP address as this script does, and it doesn't rely on a specific site actually being up… Izmantojot ifconfig darbojas tikpat labi, jums joprojām ir nepieciešams grep par pareizu līnijas, un pēc tam iespējams izmantot sed iegūt faktisko IP adresi, kā šis skripts dara, un tas nevar atsaukties uz konkrētu vietu faktiski līdz ...

  4. Sukrit D Sukrit D says saka

    Sorry for the delayed reply. Atvainojamies par aizkavētu atbildi. You can also use the following command to get your public IP: Jūs varat arī izmantot šādu komandu, lai saņemtu savu publisko IP:

    wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

  5. Sionide21 says

    You can use Jūs varat lietot
    http://www.whatismyip.org/ http://www.whatismyip.org/
    To get just the ip in plaintext form. Lai iegūtu tikai IP atklāta teksta formā.

Continuing the Discussion Turpinot iztirzājumu

  1. Find your external IP from the CLI « Linux & Stuff Atrast savu ārējo IP no CLI «Linux & Stuff linked to this post on May 10, 2009 saistībā ar šo post May 10, 2009

    [...] 09/05/2009 in CLI | Tags: CLI Here's how. [...] 09/05/2009 in CLI | Tags: CLI Lūk, kā. [...] [...]



Some HTML is OK Daži HTML ir OK

or, reply to this post via vai, atbildot uz šo ziņu izmantojot trackback Trackback . .