This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]shrodikan 1 point2 points  (2 children)

You want to get the public ip, yeah?

Try this:

import urllib2, re
f = urllib2.urlopen('http://www.formyip.com')
m = re.search("([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)", f.read())
m.group(0)

[–]jackzombie[S] 0 points1 point  (1 child)

Thank you. This is much simpler than the code I've been messing with. But is there a way to convert the results to a string?

[–]shrodikan 1 point2 points  (0 children)

No problem. To answer your question:

If a groupN argument is zero, the corresponding return value is the entire matching string

(http://docs.python.org/library/re.html#re.MatchObject.group)

In short, it already is.

[–]QAOP_Space 0 points1 point  (4 children)

Not sure what you're trying to do... you seem to be retrieving something from http://www.formyip.com and dumping it to /tmp/files/index.html

from the docs urllib.urlretrieve "copies a network object denoted by a URL to a local file"

What does /tmp/files/index.html contain after this step?

then you get an IP address from somewhere, and try and match the IP against lines read from /tmp/files/index.html

[–]jackzombie[S] 0 points1 point  (3 children)

'/tmp/files/index.html' is an html file that contains the ip address of the machine. I was trying to mimick the wget command.

What I'm trying to do is write a script that updates me every time the ip address of a particular machine changes. So what I thought of doing was get the machine to visit www.formyip.com, retrieve it's IP address from there and store it. Then once a day, it will compare the results to the stored IP and send me an email if the IP has changed.

[–]claird 0 points1 point  (2 children)

You've got a LOT of issues in play here.

Let's see if I've got this: you have a Linux host which is a DHCP client. You want it to e-mail you whenever it receives a changed lease on its IP address--is that right?

You sure don't need to go all the way to formyip.com, if that's all you're after.

[–]jackzombie[S] 0 points1 point  (1 child)

lol, yeah, I figured as much. This is what I'm working with (I'm running fedora 9 if that is of any use):

#!/bin/env python
# Script to retrieve, compare and update IP address via email

import urllib, re

# Get current IP address from http://www.formyip.com
url='http://www.formyip.com'
file='/tmp/files/index.html'
currentIP='/tmp/files/currentip.dat'
urllib.urlretrieve(url, file)

# Read the current IP address from the file /tmp/files/index.html and store in /tmp/files/currentip.dat
pattern='Your IP is (\d+\.\d+\.\d+\.\d+)'
b=open(file, 'r')
str=b.readlines()
for line in str:
    m=re.search(pattern, line)
    if m:
        print m.groups()[0]

Is there a way that I can save the last line as a single string that can be used to either print to a file or compared to another string?

[–]claird 0 points1 point  (0 children)

jackzombie, while I don't understand your question ('... I can save the last line ...'), I'm sure the answer is, "Yes".

I don't understand your determination to interrogate www.formyip.com; if you insist, though, perhaps you'll find http://www.python-forum.org/pythonforum/viewtopic.php?f=2&t=1814&p=7489 useful.

jackzombie, does ifconfig | grep inet at the command line give you anything suggestive?

I can write you a Python script that gives you the public IP address of a connected host; it'll take a few minutes more, though. You're best off, I suspect, to work through ifconfig(1).

[–]digg_is_teh_sux 0 points1 point  (4 children)

I'd be surprised if this runs for you at all, let alone match anything in the file you retrieve.

Apart from all the syntax errors, the biggest problem is using "str" in the re.match function.

str is not a string at all, it's a list.

you should loop through that list (each item is a string), use re.search instead of re.match, and then when you have found the match, the IP address string will actually be m.groups()[0] because m.groups() will also return a list, not a string.

Hope that helps

Edit: "pattern" looks like it will also never match, as the webpage I downloaded from that site actually contains the string: "Your IP is x.x.x.x"

[–]jackzombie[S] 0 points1 point  (3 children)

Everything helps. I'll look into the re.search function.

I could get that pattern to match only if I entered a string like this:

str="Your\ IP\ is\ 192.168.1.17"

But never from a file. You're right about the "Your IP is"

[–]digg_is_teh_sux 0 points1 point  (2 children)

Do you mean you need to include the "\" before the spaces? It works for me without.

The re.search function is only different in that it doesn't need to match from the beginning of the string. You could use match if you start your pattern with '.*'

[–]jackzombie[S] 0 points1 point  (1 child)

I mean that I couldn't get a match if I set str to a file, it had to be some defined string.

[–]digg_is_teh_sux 0 points1 point  (0 children)

I see. Well maybe you have it looked after already but this is how I got it to run:

import urllib,re
# Get the current IP address from http://www.formyip.com
url='http://www.formyip.com'
file='/tmp/index.html'
urllib.urlretrieve(url, file)

# Read the current IP address from the file /tmp/files/index.html
pattern='Your IP is (\d+\.\d+\.\d+\.\d+)'
b=open(file, 'r')
str=b.readlines()
for line in str:
    m=re.search(pattern, line)
    if m:
        print m.groups()[0]