CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
[solved] Socket issue / Can't use an undefined value as a symbol reference

 



SteffenBaier
Novice

Feb 17, 2011, 7:01 AM

Post #1 of 3 (1266 views)
[solved] Socket issue / Can't use an undefined value as a symbol reference Can't Post

If I run below Perl script it fails after the Socket cannot connect / timeout to a device.


Quote
Can't use an undefined value as a symbol reference at test.pl line 41




Code
 #!/usr/bin/perl -w  
#rebootutility.pl,v 0.98 2011/02/16 Steffen Baier $

use Socket;
use IO::Socket;
use strict;
use warnings;

#####################################
# Netmask 255.255.255.0 = 0xFFFFFF00#
# Netmask 255.255.0.0 = 0xFFFF0000#
# Netmask 255.0.0.0 = 0xFF000000#
# Netmask 0.0.0.0 = 0x00000000#
#####################################

my $netmask = 0xFFFFFF00; # 255.255.255.0

my $start_address = unpack 'N', inet_aton( '10.252.75.1' );
my $finish_address = unpack 'N', inet_aton( '10.252.75.254' );


for ( my $address = $start_address; $address <= $finish_address; ++$address ) {
next if ( $address & $netmask ) == $address or ( $address & ~$netmask ) == ~$netmask;


print inet_ntoa( pack 'N', $address);
print "\n";
my $destination = inet_ntoa( pack 'N', $address);
my $socket = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => "$destination",
PeerPort => "80",
Timeout => "2",
);
if ($@) {
print "Failed,Could not connect!\n";
}
else {
print "Passed,connected successfully.\n";
}
print $socket "GET $destination/reg_1.htm HTTP/1.1\r\nAuthorization: Basic\r\n\r\n";
}





Above script basically sends a HTTP GET request to devices. It incriments the IP Address of the defined Range and should not stop if it cannot reach a device .... Any Idea where I go wrong ..?

I used 2x different Perl scripts combined to archive above ..


(This post was edited by SteffenBaier on Feb 17, 2011, 8:43 AM)


BillKSmith
Veteran

Feb 17, 2011, 8:35 AM

Post #2 of 3 (1264 views)
Re: [SteffenBaier] [question] Socket issue / Can't use an undefined value as a symbol reference [In reply to] Can't Post

Do not try to print to socket if connection failed. Try next; after print "failed......
Good Luck,
Bill


SteffenBaier
Novice

Feb 17, 2011, 8:45 AM

Post #3 of 3 (1262 views)
Re: [BillKSmith] [question] Socket issue / Can't use an undefined value as a symbol reference [In reply to] Can't Post

THX Bill ;-) !!

working now:


Code
 #!/usr/bin/perl -w  
#rebootutility.pl,v 0.98 2011/02/16 Steffen Baier $

use Socket;
use IO::Socket;
use strict;
use warnings;

#####################################
# Netmask 255.255.255.0 = 0xFFFFFF00#
# Netmask 255.255.0.0 = 0xFFFF0000#
# Netmask 255.0.0.0 = 0xFF000000#
# Netmask 0.0.0.0 = 0x00000000#
#####################################

my $netmask = 0xFFFFFF00; # 255.255.255.0

my $start_address = unpack 'N', inet_aton( '10.252.75.1' );
my $finish_address = unpack 'N', inet_aton( '10.252.75.254' );


for ( my $address = $start_address; $address <= $finish_address; ++$address ) {
next if ( $address & $netmask ) == $address or ( $address & ~$netmask ) == ~$netmask;


print inet_ntoa( pack 'N', $address);
print "\n";
my $destination = inet_ntoa( pack 'N', $address);
my $socket = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => "$destination",
PeerPort => "80",
Timeout => "2",
);
if ($@) {
print "Failed,Could not connect!\n";
next;
}
else {
print "Passed,connected successfully.\n";
}
print $socket "GET /reg_1.htm HTTP/1.1\r\nAuthorization: Basic\r\n\r\n";
}



UPDATE: removed wrong $destination before the GET


(This post was edited by SteffenBaier on Feb 17, 2011, 8:53 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives