tags:

views:

624

answers:

4

I'm new to Perl. And I have used following code from one forum to connect to one of the server. but throwing error messages

[root@Cus]# cat tt.pl
#!/usr/bin/perl
use Net::Telnet;
$telnet = new Net::Telnet ( Timeout=>2, Errmode=>'die');
$telnet->open('10.0.0.28');
$telnet->waitfor('/login:/');
$telnet->print('administrator');
$telnet->waitfor('/Password:/');
$telnet->print('test');
$telnet->waitfor('/switch8-12>/');
$telnet->print('whoamI');
$output=$telnet->waitfor('/switch8-12>/');
print $output;

But throwing following error messages.

[root@Cus]# ./tt.pl
./tt.pl: line 3: use: command not found
./tt.pl: line 4: syntax error near unexpected token `('
./tt.pl: line 4: `$telnet = new Net::Telnet ( Timeout=>2, Errmode=>'die');'
+2  A: 

My guess is that you're using a weird flavour of unix that doesn't respect the #! line, and is trying to run the script via the shell instead of via perl.

Another reason why this might happen is if tt.pl starts with a blank line. The #! must appear at the very start of the file.

Try running perl tt.pl and see what happens.

Dave Hinton
even that is also throwing same error messages
What does "perl -v" show? Is it possible that you have something called "perl" on your path which isn't actually a Perl interpreter? (Bit of a long-shot, I admit.)
Matthew Wilson
+1  A: 

I don't see these lines at the top of your script, which are essential for all perl modules and scripts:

use strict;
use warnings;

You didn't say which environment this is running in -- are you using bash on linux? Whatever shell you are using does not understand the shebang (#!/usr/bin/perl), and is trying to execute the script in its own language rather than invoking Perl to run it.

Try using /bin/bash, and then your shebang line will work. Or, simply invoke perl explicitly: perl tt.pl.

Ether
even after adding these lines getting error messages
[root@CusCent5U2-64CX gannu]# perl tt.plCan't locate Net/Telnet.pm in @INC (@INC contains: /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.7/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.6/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.5/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl/5.8.7 /usr/lib/perl5/site_perl/5.8.6 /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.7/x86_64-linux-thread-multi
/usr/lib64/perl5/vendor_perl/5.8.6/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.5/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5/vendor_perl/5.8.6 /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 .) at tt.pl line 5.BEGIN failed--compilation aborted at tt.pl line 5.[root@CusCent5U2-64CX gannu]# ./tt.pl./tt.pl: line 3: use: command not found./tt.pl: line 4: use: command not found./tt.pl: line 5: use: command not found
./tt.pl: line 5: use: command not found./tt.pl: line 6: syntax error near unexpected token `('./tt.pl: line 6: `$telnet = new Net::Telnet ( Timeout=>2, Errmode=>'die');'[root@CusCent5U2-64CX gannu]#
@chiran The required libraries are not installed on `CusCent5U2-64CX` Either use the OS's package management tools to install them or use `cpan`. Also, read http://perldoc.perl.org/perlmodinstall.html **AND** http://blogs.msdn.com/oldnewthing/archive/2010/03/24/9983984.aspx
Sinan Ünür
Add clarifying information and updates to your answer. Don't hide them in comments.
brian d foy
The answer to your "Can't locate..." problem is in many places on Stackoverflow. Try the SO serach box :)
brian d foy
+2  A: 

I had similar error messages and found the reason to be that the perl file was using the wrong character encoding (don't ask me why this mattered to perl). Perl was installed correctly, paths were in order, script syntax was perfect (I even got the "use: command not found" error for a one line "Hello World!" script). Check that tt.pl is UTF8 no-BOM.

Timo
Yes if you save with a tool (such as windows notepad) it can add a BOM marker byte. Than upset the #!, but not be visible.
Gavin Brock
Man, it took me the better part of an afternoon to figure it out at the time, since most text editors, of course, interpret and display the character encoding automatically "correctly".
Timo
A: 

Thank you Gavin! You just saved me hours of troubleshooting. I had the same error as above and it turned out to be a characterset issue.

Scott