Perl Net::IP not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Net::IP not working
# 1  
Old 01-15-2016
Perl Net::IP not working

Experts - I have a snippet of code I can't figure out. I was hoping someone could help me here.
I have a file of IPv6 address that I need to format correct.
Example in:
Code:
2620:0:2d0:200::7
2620:0:2d0:200:a0:c
2620:0a:3f0:200:a0:c

I need to convert them to:
Code:
2620:0000:02d0:0200:0000:0000:0000:0007
2620:0000:02d0:0200:00a0:0000:0000:000c
2620:000a:03f0:0200:00a0:0000:0000:000c

I'm reading the file into @tstIPs and then I'm trying to loop through them and update accordingly into @testIPs using Net::IP per below.

Code:
open(FH, "<", $fh) or die "Can't open file: $fh$!\n";
my @tstIPs=<FH>;
close(FH);

foreach my $i (@tstIPs) {
   my $tstip = new Net::IP ($i,6);
   push @testIPs, $tstip->ip();

}

I'm getting the following error:

Code:
Can't call method "ip" on an undefined value at ./dbi.pl line 43.

I can't figure out what's going on here and need help.

Thanks
Moderator's Comments:
Mod Comment Please use CODE tags for sample input and sample output as well as for sample code.

Last edited by Don Cragun; 01-15-2016 at 01:45 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 01-15-2016
Quote:
Originally Posted by timj123
[...]
Example in:
Code:
2620:0:2d0:200::7
2620:0:2d0:200:a0:c
2620:0a:3f0:200:a0:c

I need to convert them to:
Code:
2620:0000:02d0:0200:0000:0000:0000:0007
2620:0000:02d0:0200:00a0:0000:0000:000c
2620:000a:03f0:0200:00a0:0000:0000:000c

I'm reading the file into @tstIPs and then I'm trying to loop through them and update accordingly into @testIPs using Net::IP per below.

Code:
open(FH, "<", $fh) or die "Can't open file: $fh$!\n";
my @tstIPs=<FH>;
close(FH);

foreach my $i (@tstIPs) {
   my $tstip = new Net::IP ($i,6); # this will return undef if it can not make sense of $i
   push @testIPs, $tstip->ip(); # $tstip is undef if the above fails

}

Take a look at the highlighted in red comments. Check the return of new Net::IP before assuming that it created an object. Use Net::IP::Error().

Why does it fail?
It needs another : or it will be too ambiguous.
Code:
2620:0:2d0:200:a0::c
2620:0a:3f0:200:a0::c

Code:
2620:0000:02d0:0200:00a0:0000:0000:000c
2620:000a:03f0:0200:00a0:0000:0000:000c


Last edited by Aia; 01-15-2016 at 02:31 AM.. Reason: Corrects misspell
This User Gave Thanks to Aia For This Post:
# 3  
Old 01-15-2016
Thanks for the rapid reply.
How can I use the code you suggested below?

Code:
Net::IP::Error()

---------- Post updated at 01:18 AM ---------- Previous update was at 12:47 AM ----------

Okay. I think I got the error working to display.

It displaying:

Code:
Invalid IP address 2620:0:2d0:200:0:0:0:7

But it looks like a good IPv6 to me.
# 4  
Old 01-15-2016
Yes it's a good one.
Code:
perl -e '
require Net::IP;
$str = "2620:0:2d0:200:0:0:0:7";
$ip = new Net::IP($str, 6);
print $ip->print, "\n";
'
2620:0:2d0:200::7/128

This User Gave Thanks to yazu For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Ssh authentication from .NET webpage to UNIX is not working

My .NET website invokes a perl script to perform GIT operations on Gerrit server running UBuntu. In the perl script I connect using passwordless authentication to Gerrit server as below: system ( "ssh gitadmin@gerritserver.com 'cd /xyz && git clone xxx' "); I verified that ssh authentication... (3 Replies)
Discussion started by: tkota
3 Replies

2. Shell Programming and Scripting

Net::SSH::Perl slow to login.

I have some sample code that's supposed to ssh to another machine using Net::SSH::Perl, execute a command, and print the output of that command. It's very basic, and it works. However, I noticed that upon logging in: $ssh->login('username','password'); It takes roughly 10-13 seconds to... (2 Replies)
Discussion started by: mrwatkin
2 Replies

3. Shell Programming and Scripting

Net::OpenSSH not working on Unix machine.

So, I installed the Net::OpenSSH module and I am having trouble getting it to work. It appears to be failing on this code: my $ssh = Net::OpenSSH->new('user:passwd@host.com'); $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error; It's pretty simple code. I keep... (0 Replies)
Discussion started by: mrwatkin
0 Replies

4. Shell Programming and Scripting

Perl - Net::FTP issue

Wondering if anyone can help or advise on following issue. The below script should simply connect to a different server and retrieve certain files for me. use lib "/xxxxx/xxxxx/xxxxx/xxxx"; use Net::FTP; my $directory = "xxxxxxxx"; my $destinationDir = "xxxxxxxxx"; my $filePrefix =... (1 Reply)
Discussion started by: meevagh
1 Replies

5. Shell Programming and Scripting

Net::Ftp in perl

I am trying to execute a script in another server, i used Net::Ftp module How to execute unix command in another server by using Net::Ftp module.. #!/usr/bin.perl ### Perl script to $ftp->login($user_name,'password') or die "Cannot login ", $ftp->message;... (2 Replies)
Discussion started by: pritish.sas
2 Replies

6. Shell Programming and Scripting

Net::SMTP via PERL is not working in few cases

We are getting some special scenarios where Net::SMTP is not working for some mails. we are sending a similar kind of data via mails but some of the mails are not received. After checking the Net::SMTP in debug mode we found below difference: Good One - (Mail sent properlly) --- -----... (0 Replies)
Discussion started by: NIMISH AGARWAL
0 Replies

7. Shell Programming and Scripting

Corrupted Download with Net::FTP (perl)

Hello, I am trying to download several files from a remote FTP server using Net::FTP from within a perl script. The files download alright, except they appear to be corrupted. I know this because once after downloading, I use bzcat to pipe the files to a split command, and bzcat complains saying... (3 Replies)
Discussion started by: amcrisan
3 Replies

8. Shell Programming and Scripting

PERL: NET::FTP..>Debug Messages

HI All, NET::FTP->new($server, DEBUG=>1); I need to get all the Debug Messages in an array or a file.... Please suggest!! (0 Replies)
Discussion started by: angad.makkar
0 Replies

9. Shell Programming and Scripting

Best place on the net to get Perl tutorials ?

I am looking for a full Perl course on internet (tutorial) to learn everything about Perl. I already know a bunch of programing languages. I have started Unix interests this year ever since I installed Ubuntu Linux on my PC AND I also got a job at IBM doing software distributions with... (1 Reply)
Discussion started by: Browser_ice
1 Replies

10. Shell Programming and Scripting

perl + Net::FTP::Recursive

Problem: It will not advance to the next user in the list. It always dies right after it sends the 2/2 files from the first users dir. $USERLIST="/export/home/mxdooley/perl_ftp/userlist"; $USER_DIR="/export/home/mxdooley/perl_ftp/homes";... (2 Replies)
Discussion started by: Optimus_P
2 Replies
Login or Register to Ask a Question