PDA

View Full Version : Using postfix in C / detecting IP addresses



sean
02-26-2009, 01:59 PM
So I'm setting up Ubuntu server 8.04 on a dynamic IP - and I'm writing a script that needs to send me an email whenever the IP changes. So I'm frantically researching the cleanest ways to do this in C - but if you can make some recommendations, you'll probably save me a lot of time and I could avoid going down the wrong path.

1. I'm behind a router - how can I find the routers WAN address? I know I'm 192.168.1.100 and it's 192.168.1.1, but how can I tell what my address is to the outside?

2. I see postfix is the default MTA in Ubuntu... I can't seem to get it work though. I try executing it from the command line and from my program through system(), and each time the program ends with no message, and I never receive an email from the other end. I'm guessing I have it configured wrong - I have check with my ISP and they do allow mail servers. Any thoughts?

I'm aware that Google results for both these topics are numerous - but they all seem to be very low quality, and I'm wary of "trying" any more solutions presented because none of them have worked so far.

Salem
02-26-2009, 02:34 PM
> tracert -h 2 www.google.com

Tracing route to www.l.google.com [74.125.79.103]
over a maximum of 2 hops:

1 <1 ms <1 ms <1 ms 192.168.0.1
2 6 ms 5 ms 5 ms myisp.com [12.34.56.78]

Trace complete.

Or as many hops as it takes to get across your intranet and one step onto the internet.

Oddly, I get a different answer from the likes of http://www.whatismyip.com/

sean
02-26-2009, 03:08 PM
I was hoping I was missing an obvious system call - I guess it's up to system and manual parsing, eh? Any idea why the IP is different?

IceDane
02-26-2009, 03:33 PM
I highly recommend you use some language like perl or any other language that supports regexes, and then parse it from the HTML of www.whatsmyip.com

brewbuck
02-26-2009, 07:08 PM
ifconfig $IFACE | head -2 | tail -1 | awk '{print $2}' | awk -F: '{print $2}'


Where $IFACE is your interface (e.g "eth0")

EDIT: Oh. You're behind a router, so you want your gateway's external address. You'll need to use traceroute to find it.

carrotcake1029
02-26-2009, 07:08 PM
I believe there is a posix regex library. I don't quite know how good it is though.

sean
02-26-2009, 08:15 PM
You'll need to use traceroute to find it.

I don't see how that's gonna work... It shows my router with it's LAN address... The first address that isn't under my LAN is my ISP's router.... Am I mistaken?

sean@BlackBear:~$ traceroute gmail.com
traceroute to gmail.com (64.233.161.83), 30 hops max, 40 byte packets
1 192.168.1.1 (192.168.1.1) 2.388 ms 4.710 ms 5.079 ms
2 69.169.152.1.provo.static.broadweave.net (69.169.152.1) 14.457 ms 14.835 ms 15.173 ms
3 ge-6-6.car1.SaltLakeCity1.Level3.net (4.53.40.29) 15.544 ms 15.911 ms 16.278 ms

If I'm not mistaken, 69.169.152.1 is NOT my house. Unless I am indeed mistaken, I guess it's down to using PHP...

brewbuck
02-26-2009, 08:39 PM
I don't see how that's gonna work... It shows my router with it's LAN address... The first address that isn't under my LAN is my ISP's router.... Am I mistaken?

No, I'm just dumb.