[32480] in Perl-Users-Digest
Perl-Users Digest, Issue: 3745 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jul 27 06:09:15 2012
Date: Fri, 27 Jul 2012 03:09:03 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 27 Jul 2012 Volume: 11 Number: 3745
Today's topics:
Re: Linux, IO::Socket::INET and recv'ing broadcasted UD mkparam@gmail.com
Re: Pls help old perl-monger with new version syntax? <rweikusat@mssgmbh.com>
Re: Pls help old perl-monger with new version syntax? <dmcanzi@uwaterloo.ca>
Re: Pls help old perl-monger with new version syntax? <rweikusat@mssgmbh.com>
Regx explanation please <dave@invalid.invalid>
Re: Regx explanation please <rweikusat@mssgmbh.com>
Re: Regx explanation please <NoSpamPleaseButThisIsValid3@gmx.net>
Re: Regx explanation please <dave@invalid.invalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 26 Jul 2012 10:03:42 -0700 (PDT)
From: mkparam@gmail.com
Subject: Re: Linux, IO::Socket::INET and recv'ing broadcasted UDP
Message-Id: <daf2dded-8890-435b-8eae-f1dd83fee2b8@googlegroups.com>
All
I know this is a pretty old thread, but looks like i hit the exact same pro=
blem this week while trying to use DHCP::Packet.=20
My requirement is that i want to use this perl module to get the IP's from =
DHCP server to do some PoC. I have two VM's , one act as DHCP server (RHEL5=
64bit), and another RHEL5 64bit to run this script to get IP's from the DH=
CP server , both having two interfaces eth0 and eth1; eth1 of both the VM's=
connected to a VMware VSwitch to isolate these two VM's from others; eth0 =
of both the VM's having a valid IP in the range 10.72.x.x ; DHCP server lis=
tens on eth1 from the first VM.
When i use a script like this one, i can see the DHCPDISCOVER is sent prope=
rly as it reaches the server and DHCPOFFER is also being made by the server=
as per the logs ; but my script does not pick up the packet for some reaso=
ns which i am unable to understand.
--script --
#!/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 =3D shift;
print STDOUT strftime "[%d/%b/%Y:%H:%M:%S] ", localtime;
print STDOUT "$str\n";
}
logger("DHCPd tester - dummy client");
logger("Opening socket");
$handle =3D IO::Socket::INET->new(Proto =3D> 'udp',
Broadcast =3D> 1,
PeerPort =3D> 67,
LocalPort =3D> 68,
LocalAddr =3D> '192.168.1.3',
PeerAddr =3D> '255.255.255.255')
|| die "Socket creation error: $@\n"; # yes, it uses $@ here
# create DHCP Packet DISCOVER
$discover =3D Net::DHCP::Packet->new(
Xid =3D> 0x12345678,
Flags =3D> 1,
Chaddr =3D> '0050569821A9',
DHO_DHCP_MESSAGE_TYPE() =3D> DHCPDISCOVER(),
DHO_VENDOR_CLASS_IDENTIFIER() =3D> 'foo',
);
logger("Sending DISCOVER to 192.168.1.2:67");
logger($discover->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 =3D new Net::DHCP::Packet($buf);
logger($response->toString());
-- /var/log/messages of DHCP Server ---
Jul 26 22:21:03 vmlnx64-30 dhcpd: DHCPDISCOVER from 00:50:56:98:21:a9 via e=
th1
Jul 26 22:21:03 vmlnx64-30 dhcpd: DHCPOFFER on 192.168.1.49 to 00:50:56:98:=
21:a9 via eth1
** TCPDUMP of DHCP Server and Client when this is being run ***
DHCPSERVER
**********
dhcp-Server>tcpdump -i eth1 -vvv
tcpdump: listening on eth1, link-type EN10MB (Ethernet), capture size 96 by=
tes
17:40:27.555097 IP (tos 0x0, ttl 1, id 0, offset 0, flags [none], proto: =
IGMP (2), length: 36, options ( RA (148) len=20
4 )) 10.0.0.0 > all-systems.mcast.net: igmp query v3 [max resp time 1s]
17:40:27.634538 IP (tos 0xc0, ttl 1, id 0, offset 0, flags [DF], proto: I=
GMP (2), length: 40, options ( RA (148) len 4
)) 192.168.1.2 > igmp.mcast.net: igmp v3 report, 1 group record(s) [gaddr =
224.0.0.251 is_ex { }]
17:40:33.120044 IP (tos 0x0, ttl 64, id 2290, offset 0, flags [DF], proto:=
UDP (17), length: 328) 192.168.1.3.bootpc >=20
255.255.255.255.bootps: BOOTP/DHCP, Request from 00:50:56:98:23:8d (oui Unk=
nown), length: 300, xid:0x12345678, flags: [none] (0x0000)
Client Ethernet Address: 00:50:56:98:23:8d (oui Unknown) [|bootp]
17:40:33.121657 arp who-has 192.168.1.50 tell 192.168.1.2
17:40:34.001555 IP (tos 0x10, ttl 16, id 0, offset 0, flags [none], proto:=
UDP (17), length: 328) 192.168.1.2.bootps > 192.168.1.50.bootpc: BOOTP/DHC=
P, Reply, length: 300, xid:0x12345678, flags: [none] (0x0000)
Your IP: 192.168.1.50
Client Ethernet Address: 00:50:56:98:23:8d (oui Unknown) [|bootp]
17:40:34.122468 arp who-has 192.168.1.50 tell 192.168.1.2
17:40:34.554985 IP (tos 0x0, ttl 1, id 0, offset 0, flags [none], proto: =
IGMP (2), length: 36, options ( RA (148) len 4 )) 10.0.0.0 > all-systems.mc=
ast.net: igmp query v3 [max resp time 1s]
17:40:34.585378 IP (tos 0xc0, ttl 1, id 0, offset 0, flags [DF], proto: I=
GMP (2), length: 40, options ( RA (148) len 4 )) 192.168.1.2 > igmp.mcast.n=
et: igmp v3 report, 1 group record(s) [gaddr 224.0.0.251 is_ex { }]
17:40:35.122279 arp who-has 192.168.1.50 tell 192.168.1.2
17:41:06.957198 rarp who-is 00:50:56:98:23:8d (oui Unknown) tell 00:50:56:9=
8:23:8d (oui Unknown)
17:41:06.957210 rarp who-is 00:50:56:98:23:8d (oui Unknown) tell 00:50:56:9=
8:23:8d (oui Unknown)
17:41:06.957211 rarp who-is 00:50:56:98:23:8d (oui Unknown) tell 00:50:56:9=
8:23:8d (oui Unknown)
17:41:06.957223 rarp who-is 00:50:56:98:23:8d (oui Unknown) tell 00:50:56:9=
8:23:8d (oui Unknown)
17:41:07.556639 rarp who-is 00:50:56:98:23:8d (oui Unknown) tell 00:50:56:9=
8:23:8d (oui Unknown)
17:41:07.630083 IP (tos 0xc0, ttl 1, id 0, offset 0, flags [DF], proto: I=
GMP (2), length: 40, options ( RA (148) len 4 )) 192.168.1.3 > igmp.mcast.n=
et: igmp v3 report, 1 group record(s) [
gaddr 224.0.0.251 is_ex { }]
17:41:08.558188 rarp who-is 00:50:56:98:23:8d (oui Unknown) tell 00:50:56:9=
8:23:8d (oui Unknown)
17:41:09.557206 rarp who-is 00:50:56:98:23:8d (oui Unknown) tell 00:50:56:9=
8:23:8d (oui Unknown)
17:41:09.706661 IP (tos 0x0, ttl 64, id 38874, offset 0, flags [DF], proto=
: UDP (17), length: 328) 192.168.1.3.bootpc > 255.255.255.255.bootps: BOOTP=
/DHCP, Request from 00:50:56:98:23:8d (oui Unknown), length: 300, xid:0x123=
45678, flags: [none] (0x0000)
Client Ethernet Address: 00:50:56:98:23:8d (oui Unknown) [|bootp]
17:41:09.706840 IP (tos 0x10, ttl 16, id 0, offset 0, flags [none], proto:=
UDP (17), length: 328) 192.168.1.2.bootps > 192.168.1.50.bootpc: BOOTP/DHC=
P,
Reply, length: 300, xid:0x12345678, flags: [none] (0x0000)
Your IP: 192.168.1.50
Client Ethernet Address: 00:50:56:98:23:8d (oui Unknown) [|bootp]
17:41:11.556846 rarp who-is 00:50:56:98:23:8d (oui Unknown) tell 00:50:56:9=
8:23:8d (oui Unknown)
17:41:14.557189 rarp who-is 00:50:56:98:23:8d (oui Unknown) tell 00:50:56:9=
8:23:8d (oui Unknown)
17:41:14.643834 IP (tos 0xc0, ttl 1, id 0, offset 0, flags [DF], proto: I=
GMP (2), length: 40, options ( RA (148) len 4 )) 192.168.1.3 > igmp.mcast.n=
et: igmp v3 report, 1 group record(s) [
gaddr 224.0.0.251 is_ex { }]
17:41:19.557221 rarp who-is 00:50:56:98:23:8d (oui Unknown) tell 00:50:56:9=
8:23:8d (oui Unknown)
17:41:27.559216 rarp who-is 00:50:56:98:23:8d (oui Unknown) tell 00:50:56:9=
8:23:8d (oui Unknown)
17:41:40.558913 rarp who-is 00:50:56:98:23:8d (oui Unknown) tell 00:50:56:9=
8:23:8d (oui Unknown)
DHCP CLIENT
***********
dhcp-Client>tcpdump -i eth1 -vvv
tcpdump: listening on eth1, link-type EN10MB (Ethernet), capture size 96 by=
tes
17:41:07.553418 IP (tos 0x0, ttl 1, id 0, offset 0, flags [none], proto: =
IGMP (2), length: 36, options ( RA (148) len 4 )) 10.0.0.0 > all-systems.mc=
ast.net: igmp query v3 [max resp time 1s]
17:41:07.626696 IP (tos 0xc0, ttl 1, id 0, offset 0, flags [DF], proto: I=
GMP (2), length: 40, options ( RA (148) len 4 )) 192.168.1.3 > igmp.mcast.n=
et: igmp v3 report, 1 group record(s) [gaddr 224.0.0.251 is_ex { }]
17:41:09.703284 IP (tos 0x0, ttl 64, id 38874, offset 0, flags [DF], proto=
: UDP (17), length: 328) 192.168.1.3.bootpc > 255.255.255.255.bootps: BOOTP=
/DHCP, Request from 00:50:56:98:23:8d (oui Unknown), length: 300, xid:0x123=
45678, flags: [none] (0x0000)
Client Ethernet Address: 00:50:56:98:23:8d (oui Unknown) [|bootp]
17:41:09.703796 IP (tos 0x10, ttl 16, id 0, offset 0, flags [none], proto:=
UDP (17), length: 328) 192.168.1.2.bootps > 192.168.1.50.bootpc: BOOTP/DHC=
P, Reply, length: 300, xid:0x12345678, flags: [none] (0x0000)
Your IP: 192.168.1.50
Client Ethernet Address: 00:50:56:98:23:8d (oui Unknown) [|bootp]
17:41:14.553952 IP (tos 0x0, ttl 1, id 0, offset 0, flags [none], proto: =
IGMP (2), length: 36, options ( RA (148) len 4 )) 10.0.0.0 > all-systems.mc=
ast.net: igmp query v3 [max resp time 1s]
17:41:14.640463 IP (tos 0xc0, ttl 1, id 0, offset 0, flags [DF], proto: I=
GMP (2), length: 40, options ( RA (148) len 4 )) 192.168.1.3 > igmp.mcast.n=
et: igmp v3 report, 1 group record(s) [gaddr 224.0.0.251 is_ex { }]
17:41:21.554244 rarp who-is 00:50:56:98:04:36 (oui Unknown) tell 00:50:56:9=
8:04:36 (oui Unknown)
There is no iptables as i can verify.
Any help to figure out why my script unable to receive the DHCP Offer Packe=
ts(that has the Your_IP details )would be much appreciated. Thanks for the =
help.
PK
------------------------------
Date: Thu, 26 Jul 2012 14:26:54 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Pls help old perl-monger with new version syntax?
Message-Id: <87r4ry1wm9.fsf@sapphire.mobileactivedefense.com>
Rainer Weikusat <rweikusat@mssgmbh.com> writes:
> Wolf Behrenhoff <NoSpamPleaseButThisIsValid3@gmx.net> writes:
[...]
>> sub foo {
>> ...
>> for my $i (1..100) {
>> my $inner = do_something($i);
>> ...
>> }
>> ...
>> }
>>
>> The $inner is now declared in a block (the "for") of a block ("sub"). So
>> the $inner should be declared before the loop?
>
> Decidedly yes, because (AFAIK) this code creates a new my variable on
> each iteration
According to a simple experiment I just made, this seems to be wrong.
In this case, this means the complete for-statement would be a good
candidate for becoming its own subroutine, except that it already is
'its own subroutine' in these pathological examples because the code
outside of it does nothing: This is no longer a case of using nameless
'subprograms' inside a larger aggregate but just an attempt to confuse
the reader as much as possible (not really applicable to this
contrived example) by using 'hand-optimized' individual lifetimes for
(possibly) all variables declared inside a subroutine: There a fifteen
$i in here and they are all different!
-----------
use Devel::Peek;
for (1 .. 2) {
my $a = 4;
Dump($a);
$a = 5;
}
------------------------------
Date: Thu, 26 Jul 2012 14:02:26 +0000 (UTC)
From: "David Canzi" <dmcanzi@uwaterloo.ca>
Subject: Re: Pls help old perl-monger with new version syntax?
Message-Id: <jurili$cso$1@rumours.uwaterloo.ca>
Rainer Weikusat <rweikusat@mssgmbh.com> wrote:
>Ben Morrow <ben@morrow.me.uk> writes:
>> Quoth Rainer Weikusat <rweikusat@mssgmbh.com>:
>>> Wolf Behrenhoff <NoSpamPleaseButThisIsValid3@gmx.net> writes:
>>> >
>>> > Just to make sure I understand you correctly: given the two following subs:
>>> >
>>> > sub foo {
>>> > ...
>>> > for my $i (1..100) { ... }
>>> > ...
>>> > }
>>> >
>>> > sub bar {
>>> > my $i;
>>> > ...
>>> > for $i (1..100) { ... }
>>> > ...
>>> > }
>>> >
>>> > are you suggesting to prefer bar over foo because in foo the scope for
>>> > $i is limited to the loop?
>>>
>>> There's no variable being declared in an inner scope in either example
>>> so why do you ask?
>>
>> Yes there is. In the first example $i is scoped over the loop block,
>> rather than the whole sub. Even in an example like this:
>>
>> sub foo {
>> my ($x) = @_;
>> $x or return;
>> my $y = ...;
>> }
>>
>> $y is in a smaller scope than $x, that scope just doesn't happen to be a
>> full BLOCK.
>>
>>> (NB: That's a stupid retort based on tangential
>>> technicalities of the example).
>>
>> No, it's a stupid retort based on a complete misunderstanding of the
>> example,
>
>I assumed that 'inner scope' was sufficiently clear to be understood
>as it was meant,
It wasn't.
--
David Canzi | TIMTOWWTDI (tim-toe-woe-dee): There Is More Than One
| Wrong Way To Do It
------------------------------
Date: Thu, 26 Jul 2012 17:55:47 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Pls help old perl-monger with new version syntax?
Message-Id: <878ve6a2cs.fsf@sapphire.mobileactivedefense.com>
Rainer Weikusat <rweikusat@mssgmbh.com> writes:
> Rainer Weikusat <rweikusat@mssgmbh.com> writes:
>> Wolf Behrenhoff <NoSpamPleaseButThisIsValid3@gmx.net> writes:
>
> [...]
>
>>> sub foo {
>>> ...
>>> for my $i (1..100) {
>>> my $inner = do_something($i);
>>> ...
>>> }
[...]
>> this code creates a new my variable on each iteration
>
> According to a simple experiment I just made, this seems to be
> wrong.
Likely, the test in the previous posting was too simplistic and the
two variables just happened to be using the same memory area. The code
below:
-----------
my @s;
for (0 .. 1) {
my $a;
$s[$_] = sub { return ++$a; };
}
print $s[0]->(), "\n";
print $s[1]->(), "\n";
print $s[0]->(), "\n";
print $s[1]->(), "\n";
------------
shows that there are indeed two independent variables being created
and according to
------------
use Benchmark;
timethese(-20,
{
a => sub {
for (1 .. 1000) {
my $a = $_;
}
},
b => sub {
my $a;
for (1 .. 1000) {
$a = $_;
}
}});
------------
[averaged over three runs] the first loop executes at about 78.5% of
the speed of the second (absolute difference being around 0.00002s).
------------------------------
Date: Thu, 26 Jul 2012 13:17:05 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Regx explanation please
Message-Id: <fV45K0OBJxbE-pn2-vLudXUj07ZUh@localhost>
Yesterday I needed to be able to print large numbers with commas. ie
1,234,567
Google turned up the following:
1 while s/^(-?\d+)(\d{3})/$1,$2/;
Two questions arise from this.
1) I have not seen the "1 while........" construct before - How does
that work? Or is it just syntatic suger to avoid empty curlies?
2) How does that regex actually work? I get the followed by three
digits bit - but its the -?\d+ bit I don't understand. And my camel
book does not mention -? Nor does perlre. I am guessing that most of
it is "look for at least one digit followed by exactly three digits.
Change to what you found first, a comma, and then the three digits.
The while keeps it going until the regex does nothing. I ran a few
tests and it makes no difference if the -? is there or not. Or at
least I could find no value where it did. The output is the same.
TIA
--
Regards
Dave Saville
------------------------------
Date: Thu, 26 Jul 2012 14:48:33 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Regx explanation please
Message-Id: <87ipda1vm6.fsf@sapphire.mobileactivedefense.com>
"Dave Saville" <dave@invalid.invalid> writes:
> Yesterday I needed to be able to print large numbers with commas. ie
> 1,234,567
>
> Google turned up the following:
>
> 1 while s/^(-?\d+)(\d{3})/$1,$2/;
>
> Two questions arise from this.
>
> 1) I have not seen the "1 while........" construct before - How does
> that work? Or is it just syntatic suger to avoid empty curlies?
The while ... is a statement modifier and since all of the actual code
is in the condition, 'something' needs to be used as statement in
front of it. In this case, it is 1 but it could really be anything
which has no undesirable side-effects.
>
> 2) How does that regex actually work? I get the followed by three
> digits bit - but its the -?\d+ bit I don't understand. And my camel
> book does not mention -?
This means 'the leading set of digits may be prefixed by a -', IOW, it
also works for negative numbers.
------------------------------
Date: Thu, 26 Jul 2012 15:57:37 +0200
From: Wolf Behrenhoff <NoSpamPleaseButThisIsValid3@gmx.net>
Subject: Re: Regx explanation please
Message-Id: <50114cd1$0$9515$9b4e6d93@newsspool1.arcor-online.net>
Am 26.07.2012 15:17, schrieb Dave Saville:
> Yesterday I needed to be able to print large numbers with commas. ie
> 1,234,567
>
> Google turned up the following:
>
> 1 while s/^(-?\d+)(\d{3})/$1,$2/;
>
> Two questions arise from this.
In perldoc, you can find (almost) exactly this regexp. And the first
thing you see, it won't work for "+1234". :-)
perldoc -q comma
---> How can I output my numbers with commas added?
- Wolf
------------------------------
Date: Thu, 26 Jul 2012 14:43:54 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: Regx explanation please
Message-Id: <fV45K0OBJxbE-pn2-vkcyiOkw20eY@localhost>
Thanks guys
I see the perldoc one *does* work for plus and minus. Not that in my
case it makes any difference as the numbers are computed totals which,
when stringified, only get - signs if negative. ie you don't get a
positive value stringifying to "+123". So it is only a problem if the
number to convert is a signed string in the first place.
--
Regards
Dave Saville
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 3745
***************************************