How to release & renew DHCP Lease?

Tiltle says it all: How can I release & renew my DHCP Lease via Applescript?

I live over a coffeeshop that provides free broadband access. The manager told me to feel free to use the link, but it only gives a one hour lease.

Rather than opening the Network Preferences pane and renewing the lease all the time, I’d like to automate the process, preferably using the underlying system rather than scripting an application, but I’m open to any method.

Any ideas, anyone?


chico

Kind of a pain if you’re in the middle of something when your cron kicks in and dumps you, but…

Do a man on ifconfig. For example…

Take the interface down then up:
sudo ifconfig en1 down
sudo ifconfig en1 up

en1 is typically the airport interface
en0 is usually the wired interface

you can use the command

ifconfig -a
to list all of your network interfaces

If it’s one of thos linksys things where you have to init your MACID by going to a web page, here’s some perl…

use strict;
use warnings;
use LWP::UserAgent;

my $ip = ‘123.112.1.1’; # change this to your router’s address if necessary
my $user = ‘admin’; # leave blank if you use anonymous
my $password = ‘’; # fill in your password here
my $release = 0; # Set this to 1 to release the lease before renewing. Some
# people who otherwise get a one hour lease find they get a
# one day lease if they try this.

get( “http://$user:$password@$ip/Gozila.cgi?dhcpAction=0” ) if $release;
get( “http://$user:$password@$ip/Gozila.cgi?dhcpAction=1” );

sub get {
my $url = shift;

my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(
GET => $url,
);

my $res = $ua->request($req);
unless ($res->is_success) {
print $res->status_line, "n";
}

}

I’m ok with that. It happens anyway when my lease expires.

Anyway, thanks for the reply. Most helpful. I haven’t tried it yet, but I’ll post my results.

If I understand correctly, I can set up a chron to handle this, and avoid applescript?

I haven’t done any applescripting since OS9, and remain a bit gunshy because of all the tiny but time-consuming gotchas that appleScript can throw at you.

How would I set up a chron via applescript? Or, could you point me to a tutorial or info source where I could get up to speed on applescript under OSX?

Thanks…

chico

Using “sudo ifconfig en1 down ; sudo ifconfig en1 up” works…BUT:

Just turning the Airport off and on via the menubar creates a more graceful lease renewal.

Using ipNetMonitor, I can see that there are a bunch of messages going between the client and DHCP server, some of which are NAKs, and the client loses its IP address for a number of seconds (turns to a 169.xxx value) before getting it back.

By toggling the Airport via the menubar, there’s a quick exchange between server and client, and voila! the lease is renewed. It doesn’t seem to go as deep into the system as the ifconfig commands do.

It’s probably not a big deal, but is there any way to toggle the airport card off and on via a script?

Or alternatively, how would one duplicate pressing the “Renew DHCP Lease” button in the Network Prefs panel, which also appears more graceful than taking down the interface?