0

I am beginner to Perl and I have to write a script which could change the IP address after every 1 hour. I want to change it because I receive some data from a dongle from a website and that website has some time limit to receive that data, so currently we need to unplug the connecting device and use another to change IP address. (I mean I have to request DHCP for another IP.)

But currently I am asked to write a script using Perl. Could someone please help me how to do that?

My try to do it is :

#!/usr/bin/perl
# Simple DHCP client - sending a broadcasted DHCP Discover request

use IO::Socket::INET;
use Net::DHCP::Packet;
use Net::DHCP::Constants;

use POSIX qw(setsid strftime);

# sample logger
sub logger{
    my $str = shift;
    print STDOUT strftime "[%d/%b/%Y:%H:%M:%S] ", localtime;
    print STDOUT "$str\n";
}

logger("DHCPd tester - dummy client");

logger("Opening socket");
$handle = IO::Socket::INET->new(Proto => 'udp',
                                Broadcast => 1,
                                PeerPort => '67',
                                LocalPort => '68',
                                PeerAddr => '255.255.255.255',
                )
      || die "Socket creation error: $@\n";     # yes, it uses $@ here

# create DHCP Packet DISCOVER
$discover = Net::DHCP::Packet->new(
                      Xid => 0x12345678,
                      DHO_DHCP_MESSAGE_TYPE() => DHCPDISCOVER(),
                      DHO_VENDOR_CLASS_IDENTIFIER() => 'foo',
                      );
logger("Sending DISCOVER to 127.0.0.1:67");
logger($discover->toString());
$handle->send($discover->serialize())
              or die "Error sending:$!\n";

logger("Waiting for response from server");
$handle->recv($buf, 4096) || die("recv:$!");
logger("Got response");
$response = new Net::DHCP::Packet($buf);
logger($response->toString());

# create DHCP Packet REQUEST
$request = Net::DHCP::Packet->new(
                      Xid => 0x12345678,
                      Ciaddr => $response->yiaddr(),
                      DHO_DHCP_MESSAGE_TYPE() => DHCPREQUEST(),
                      DHO_VENDOR_CLASS_IDENTIFIER() => 'foo',
                      DHO_DHCP_REQUESTED_ADDRESS() => $response->yiaddr(),
                      );

logger("Sending REQUEST to 127.0.0.1:67");
logger($request->toString());

$handle->send($request->serialize())
              or die "Error sending:$!\n";
logger("Waiting for response from server");
$handle->recv($buf, 4096) || die("recv:$!");
logger("Got response");
$response = new Net::DHCP::Packet($buf);
logger($response->toString());

It's output on terminal is :

C:\shekhar_Axestrack_Intern\IpAddressChangeScripts>test6.pl
[08/Jan/2015:18:01:01] DHCPd tester - dummy client
[08/Jan/2015:18:01:01] Opening socket
[08/Jan/2015:18:01:01] Sending DISCOVER to 127.0.0.1:67
[08/Jan/2015:18:01:01] op = BOOTREQUEST
htype = HTYPE_ETHER
hlen = 6
hops = 0
xid = 12345678
secs = 0
flags = 0
ciaddr = 0.0.0.0
yiaddr = 0.0.0.0
siaddr = 0.0.0.0
giaddr = 0.0.0.0
chaddr =
sname =
file =
Options :
 DHO_DHCP_MESSAGE_TYPE(53) = DHCPDISCOVER
 DHO_VENDOR_CLASS_IDENTIFIER(60) = foo
padding [0] =

[08/Jan/2015:18:01:01] Waiting for response from server
//And it is stuck here since last 45 minutes....

My idea to do it is: I will send a request to server (DHCP) (I think DHCPREQUEST() do that)that please provide me new IP adress.

Could some one please let me know if my last line will print ID adress or not ? I mean :

$response = new Net::DHCP::Packet($buf);
logger($response->toString());

EDIT:

I also tried this on suggestion by the experienced guys below but it still do not change IP adress (even i tried to run this perl code 4 times without success in IP change-Even i tried to run manually ipconfig /renew but still the IP is same all the time).

my $ipconfig = `ipconfig /renew`;
my $ipcfg_success = $?;
print $ipconfig;
if ($ipcfg_success == 0) {
    do print "n succesfully runned \n";
} else {
    do "\n succesfully NOT sunned \n";
}
user3085082
  • 133
  • 4
  • 16
  • I have no idea why you're trying to do this; what's wrong with `dhclient`? – Elliott Frisch Jan 08 '15 at 13:21
  • @ElliottFrisch i have to change the IP address by a script in perl when i connect to internet and it should happen every 1 hour. It is my try to achieve it.. but it is stuck at "waiting for response frim server since an hour". Could you please explain me your way of achieveing the same ?(with bit more detail) – user3085082 Jan 08 '15 at 13:24
  • @ElliottFrisch please note that i am using Windows 8 – user3085082 Jan 08 '15 at 13:25
  • 1
    I don't really do Windows, but you'll have to write a lot more of a DHCP client to change your ip address in Windows. I suspect it'll be easiest to script `ipconfig /renew`. – Elliott Frisch Jan 08 '15 at 13:28
  • @ElliottFrisch but i have been told that it is not sure that renew will always give us a fresh ip adress – user3085082 Jan 08 '15 at 13:30
  • So you're cycling IPs because a remote service operates a constraint? That seems like it's trying to circumvent something they've deliberately set in place. – Sobrique Jan 08 '15 at 13:30
  • @Sobrique yes you are right. we have dongle to receive data and we need to unplug in bw to change IP so we loose some data. Thats why i am been asked to write a script to solve the issue.. and being beginer of perl and networking i need help. could you please help me ..what i need to change in my code ? How target can be achieved ?(I am in problems) – user3085082 Jan 08 '15 at 13:57
  • 1
    I think the issue is - the remote site doesn't want you to do what you're doing. The _solution_ would be to talk to them, rather than abusing their service. – Sobrique Jan 08 '15 at 14:54

2 Answers2

2

This more of a comment but it grew too long.

Just have your script call ipconfig /release and ipconfig /renew with a few seconds in between. That will request a new IP from the DHCP server, just as your script apparently tries to do.

Of course you are not exactly guaranteed a new IP that way, that depends on the configuration of the DHCP server but you are dependent on that either way. If the possible range of addresses is very small and you are afraid you might get the old IP by bad luck, check and renew again if it happened. If you get the same IP every time, it most likely means that the server recognizes you (by MAC or hostname) and assigns your static IP to you. In that case all you can do is talk to your network adminstrator (a course of action i would suggest anyways).

If you really need a guarantee, then you have to ditch DHCP and set your own IP. That however requires that you have some range of IPs reserved just for you. Otherwise your network administrator might hunt you down with their crossbow.

Be aware that depending on what that dongle is for and who set up that time limit, they may do anyways.

DeVadder
  • 1,404
  • 10
  • 18
  • thanks for the reply but i have been obliged by tutor to DHCP server – user3085082 Jan 08 '15 at 14:02
  • @user3085082 Then you have to hope that it is set up in a way that gives you new IPs. Or talk to whoever did set it up. A good idea either way, ill add it to my answer. – DeVadder Jan 08 '15 at 14:04
  • @user3085082 Or do you mean that you have been instructed to write your own DHCP _client_? Because `ipconfig /renew` does work with the DHCP _server_, so that should not be an issue. – DeVadder Jan 08 '15 at 14:13
  • @ikegami That sounds right, thanks. I removed those parentheses. – DeVadder Jan 08 '15 at 14:19
  • ..Yes you are right .. its something like writing own DHCP//.. how to do that ? could you please help me. – user3085082 Jan 08 '15 at 15:42
2

Writing a DHCP client isn't going to change your system's IP address. You need to use the system's client.

system('ipconfig /release & ipconfig /renew');

You're not guaranteed to get a new address, though. It causes less headaches if machines always have the same IP address, so DHCP servers tend to always give the same address to a machine.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • @ikegami..Ok thanks but there has to be a timestamp..like it must change after every 1 hour.. how can it be managed for timestamp using perl script? – user3085082 Jan 08 '15 at 15:44
  • Are you asking how to get the current time? [`time`](http://perldoc.perl.org/functions/time.html) will do the trick. – ikegami Jan 08 '15 at 16:28
  • thanks for answer i have tried what you said but still it do not change IP adress. please see the part edited in my question at last – user3085082 Jan 08 '15 at 16:30
  • 1) `ipconfig /renew` is not the same thing as `ipconfig /release & ipconfig /renew`, 2) I explained that `ipconfig /renew` is almost guaranteed not to work (in comments to the other answer), 3) I explained that even if you used `ipconfig /release & ipconfig /renew`, there's a good chance DHCP server might give you back the same address you already had. That's totally up to the DHCP server. – ikegami Jan 08 '15 at 16:41
  • ..OK but even this syntax is not working system('ipconfig /release & ipconfig /renew'); they have to be run seperately in script. – user3085082 Jan 09 '15 at 01:19
  • Huh? Are you perhaps running a perl with a Unix emulation environment like cygwin? – ikegami Jan 09 '15 at 01:29
  • That answers neither questions. You haven't specified what's a syntax error. You haven't even specified if the error is in the Perl statement, in the shell command, or in `ipconfig`'s command-line arguments – ikegami Jan 09 '15 at 04:24
  • Thanks for reply ..anyways i am strictly not allowed to use ipconfi release/renew script. I have to write my own DHCP conversation through perl script. – user3085082 Jan 09 '15 at 08:07
  • I repeat: Writing a DHCP client isn't going to change your system's IP address. You need to use the system's client. If you are strictly not allow to use `ipconfig`, you are strictly not allowed to change your dynamically obtained IP address. (You could possibly change a static IP address.) – ikegami Jan 10 '15 at 17:04