[31651] in Perl-Users-Digest
Perl-Users Digest, Issue: 2914 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Apr 20 00:09:24 2010
Date: Mon, 19 Apr 2010 21: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 Mon, 19 Apr 2010 Volume: 11 Number: 2914
Today's topics:
getting full URL from relative links <sstark@hi-beam.net>
Re: getting full URL from relative links <jurgenex@hotmail.com>
Re: getting full URL from relative links <smallpond@juno.com>
Re: getting full URL from relative links <derykus@gmail.com>
Re: getting full URL from relative links <sstark@hi-beam.net>
Re: Pod errors: Unterminated S<...> sequence <smallpond@juno.com>
Problems with "show tech" using the Net::Telnet Module <lange.gerhard@gmail.com>
Re: Problems with "show tech" using the Net::Telnet Mod sln@netherlands.com
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 19 Apr 2010 08:51:55 -0700 (PDT)
From: slugger3113 <sstark@hi-beam.net>
Subject: getting full URL from relative links
Message-Id: <439491f4-9fe5-4f2c-94f4-27f9c68c0e17@11g2000yqr.googlegroups.com>
Hi, I'm trying to get full/absolute URLs from relative links in HTML
documents. I've been trying to fudge this using File::Basename,
WWW::Mechanize, etc. but was wondering if there's a more ready-made
way to do this.
For example, if my main doc is:
http://www.abc.com/x/y/z/mydoc.html
and it contains a relative link to:
../../otherdir/yourdoc.html
how do I get the absolute URL to "yourdoc.html"? Using the above
modules I've been able to get:
http://www.abc.com/x/y/z/../../otherdir/yourdoc.html
when what I want is:
http://www.abc.com/x/otherdir/yourdoc.html
Of course I could try and parse all of the possible variations for
relative paths, but it's making my head hurt and I was wondering if
there's a module that could help with this. Any thoughts would be
appreciated.
thanks
Scott
------------------------------
Date: Mon, 19 Apr 2010 09:07:48 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: getting full URL from relative links
Message-Id: <5svos59tstba9qfbs1q73reqiqouqea18n@4ax.com>
slugger3113 <sstark@hi-beam.net> wrote:
>http://www.abc.com/x/y/z/../../otherdir/yourdoc.html
>
>when what I want is:
>
>http://www.abc.com/x/otherdir/yourdoc.html
For file names there is a module that will compute the canonical path,
but I can't remember the name right now. And I don't know if it will
work with URLs, either.
jue
------------------------------
Date: Mon, 19 Apr 2010 12:11:03 -0400
From: Steve C <smallpond@juno.com>
Subject: Re: getting full URL from relative links
Message-Id: <hqhvb2$l2p$1@news.eternal-september.org>
slugger3113 wrote:
> Hi, I'm trying to get full/absolute URLs from relative links in HTML
> documents. I've been trying to fudge this using File::Basename,
> WWW::Mechanize, etc. but was wondering if there's a more ready-made
> way to do this.
>
> For example, if my main doc is:
>
> http://www.abc.com/x/y/z/mydoc.html
>
> and it contains a relative link to:
>
> ../../otherdir/yourdoc.html
>
> how do I get the absolute URL to "yourdoc.html"? Using the above
> modules I've been able to get:
>
> http://www.abc.com/x/y/z/../../otherdir/yourdoc.html
>
> when what I want is:
>
> http://www.abc.com/x/otherdir/yourdoc.html
>
> Of course I could try and parse all of the possible variations for
> relative paths, but it's making my head hurt and I was wondering if
> there's a module that could help with this. Any thoughts would be
> appreciated.
You also need to know if there is a base tag in the head section
since that changes the meaning of a relative link.
------------------------------
Date: Mon, 19 Apr 2010 09:19:50 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: getting full URL from relative links
Message-Id: <4e9370c5-b3a6-4cc6-bd2c-cd9a6f52e452@u34g2000yqu.googlegroups.com>
On Apr 19, 8:51=A0am, slugger3113 <sst...@hi-beam.net> wrote:
> Hi, I'm trying to get full/absolute URLs from relative links in HTML
> documents. I've been trying to fudge this using File::Basename,
> WWW::Mechanize, etc. but was wondering if there's a more ready-made
> way to do this.
>
> For example, if my main doc is:
>
> http://www.abc.com/x/y/z/mydoc.html
>
> and it contains a relative link to:
>
> ../../otherdir/yourdoc.html
>
> how do I get the absolute URL to "yourdoc.html"? Using the above
> modules I've been able to get:
>
> http://www.abc.com/x/y/z/../../otherdir/yourdoc.html
>
> when what I want is:
>
> http://www.abc.com/x/otherdir/yourdoc.html
>
> Of course I could try and parse all of the possible variations for
> relative paths, but it's making my head hurt and I was wondering if
> there's a module that could help with this. Any thoughts would be
> appreciated.
>
See: perldoc URI
eg, print URI->new_abs('../../otherdir/yourdoc.html' ,
'http://www.abc.com/x/y/z/')
--
Charles DeRykus
------------------------------
Date: Mon, 19 Apr 2010 09:24:03 -0700 (PDT)
From: slugger3113 <sstark@hi-beam.net>
Subject: Re: getting full URL from relative links
Message-Id: <888833da-0e87-4265-b194-34e2149e3579@k41g2000yqf.googlegroups.com>
On Apr 19, 11:07=A0am, J=FCrgen Exner <jurge...@hotmail.com> wrote:
> For file names there is a module that will compute the canonical path,
> but I can't remember the name right now. And I don't know if it will
> work with URLs, either.
>
> jue
Hm it looks like File::Spec will do what I want:
my($dpath) =3D "/one/two/../three/four";
my $cpath =3D File::Spec->canonpath( $dpath );
print $cpath,$/;
result: /one/three/four
thanks for the tip on "canonical" (whatever that means)!
Scott
------------------------------
Date: Mon, 19 Apr 2010 08:31:16 -0400
From: Steve C <smallpond@juno.com>
Subject: Re: Pod errors: Unterminated S<...> sequence
Message-Id: <hqhier$gcf$1@news.eternal-september.org>
Mumia W. wrote:
> I have a problem with the POD docs on my system.
>
> Most of the time when I try to get documentation on a
> perl-tk widget or function, the POD has errors that cause
> the example code to be missing, e.g.
>
> Whenever "S< >" is used, the entire line containing it
> is removed from the output, and an error message appears
> at the bottom of the rendered file:
>
> Unterminated S<...> sequence
>
> Obviously, this is not how POD is supposed to work; how can
> I get the S sequences to display properly. I'm using Debian Lenny
> with Perl 5.10.0 and perldoc 3.14.02.
>
Most likely not a perl issue, but...
What specific command fails?
What do you have LANG set to?
What shell are you using?
What type of window are you in? (eg. xterm, text console, emacs shell, etc.)
------------------------------
Date: Mon, 19 Apr 2010 10:49:37 -0700 (PDT)
From: Asterix <lange.gerhard@gmail.com>
Subject: Problems with "show tech" using the Net::Telnet Module
Message-Id: <387f9569-79ea-4e8e-beb0-39f27d41cc28@12g2000yqi.googlegroups.com>
I've made a little script to capture the output of networking
devices.
Everything works fine, until I use the "show tech" command in an Cisco
device.
The script does not save anything form the "show tech",
but I can see that the TELNET Session gets back some output.
But I can't figure out how to save this output from "show tech" into a
file.
There is no problem with commands which produce smaller output.
I don't want to use the "Net::Telnet:Cisco" Module:
#!/usr/bin/perl
# Autor: Ing. Gerhard Hermann Lange
# Date: 12/04/2010
# Description: Script for doing a TELNET to a CISCO devices and
executing some comands.
# Long outputs from the TELNET Session (-> show tech) is not
possible
# due to the TELNET session timeout of 5 seconds
use Net::Telnet;
#
my $prg_name = $0; $prg_name =~ s|^.*[\\/]||;
# Usage:
my $usage="Syntax: $prg_name <hostname> <ip> <username> <password>
<enable-pwd>\n";
if (! defined ($ARGV[0])) { print "$usage"; exit; }
#
$SIG{'INT'} = sub {
# Subprocedure for finishing all the work
# printf OUTPUT_FILE "@output\n\n";
close(OUTPUT_FILE);
print STDERR "Termination of program ... !\n";
exit;
};
#
$hostname = $ARGV[0];
$ip = $ARGV[1];
$username = $ARGV[2];
$password = $ARGV[3];
$enpwd = $ARGV[4];
$cmd_file = "cmds.txt";
#
my ($sec, $min, $hr, $mday, $mon, $year, @etc) = localtime(time);
$mon++; $year=$year+1900;
my $now=sprintf("%.4d%.2d%.2d%.2d%.2d", $year, $mon, $mday, $min,
$sec);
my $today=sprintf("%.2d/%.2d/%.4d", $mday, $mon, $year );
#
# $out_file=$ip;
$out_file=$ip."_$now".".cfg";
#
# For debugging the TELNET Session: uncomment this line !
# $filename="telnet_dump.txt";
#
$errmode='return'; # Default: 'die'
#
# Set cmd_remove_mode to the number of lines to remove (0 in this
case).
# http://www.perlmonks.org/?node_id=736670
$errmode="return";
$telnet = new Net::Telnet ( Timeout=>5, Errmode=>$errmode ,
cmd_remove_mode => '0', Dump_Log =>
$filename);
# $telnet = new Net::Telnet ( Timeout=>3, Errmode=>$errmode ,
Output_record_separator => "\r",cmd_remove_mode => '2', Dump_Log =>
$filename);
# -output_record_separator => "\r");
$telnet->open($ip);
#
$telnet->waitfor('/Username: $/i');
$telnet->print($username);
$telnet->waitfor('/Password: $/i');
$telnet->print($password);
#
# Wait for the prompt, send "enable" and enable-pwd
@output = $telnet->waitfor('/>/');
$telnet->print('enable');
$telnet->waitfor('/Password:/i');
$telnet->print($enpwd);
#
@output = $telnet->waitfor('/#/i');
#
open(OUTPUT_FILE, ">>$out_file");
printf OUTPUT_FILE "IP Address: $ip\n";
printf OUTPUT_FILE "Hostname: $hostname\n";
printf OUTPUT_FILE "Data: $today\n";
printf OUTPUT_FILE "\n";
#
# Creat an universal Prompt for this hostname
my $prompt = $hostname."#";
$telnet->prompt("/$prompt\$/");
@output = $telnet->cmd(String => "terminal length 0");
printf OUTPUT_FILE "@output\n\n";
#
open(CMDFILE, "<$cmd_file");
while (my $record = <CMDFILE>) {
if ($record !~ /^(#|!|\s)/) {
chomp($record);
my @record_array = (split(/;/,$record));
my $command = $record_array[0]; chomp($cmd);
#
$telnet->prompt("/$prompt\$/");
@output = $telnet->cmd(String => "$command", Timeout=>30);
#
printf OUTPUT_FILE "@output\n\n";
}
}
# Close the command File for this device
close(CMDFILE);
#
$telnet->print('exit');
close(OUTPUT_FILE);
------------------------------
Date: Mon, 19 Apr 2010 15:44:26 -0700
From: sln@netherlands.com
Subject: Re: Problems with "show tech" using the Net::Telnet Module
Message-Id: <rvjps5tfjpt71clsuqanl763lo5cjsh6au@4ax.com>
On Mon, 19 Apr 2010 10:49:37 -0700 (PDT), Asterix <lange.gerhard@gmail.com> wrote:
>I've made a little script to capture the output of networking
>devices.
>Everything works fine, until I use the "show tech" command in an Cisco
>device.
>The script does not save anything form the "show tech",
>but I can see that the TELNET Session gets back some output.
>But I can't figure out how to save this output from "show tech" into a
>file.
>There is no problem with commands which produce smaller output.
>I don't want to use the "Net::Telnet:Cisco" Module:
>
>#!/usr/bin/perl
># Autor: Ing. Gerhard Hermann Lange
># Date: 12/04/2010
># Description: Script for doing a TELNET to a CISCO devices and
>executing some comands.
># Long outputs from the TELNET Session (-> show tech) is not
>possible
># due to the TELNET session timeout of 5 seconds
>use Net::Telnet;
>#
>my $prg_name = $0; $prg_name =~ s|^.*[\\/]||;
># Usage:
>my $usage="Syntax: $prg_name <hostname> <ip> <username> <password>
><enable-pwd>\n";
>if (! defined ($ARGV[0])) { print "$usage"; exit; }
>#
>$SIG{'INT'} = sub {
> # Subprocedure for finishing all the work
> # printf OUTPUT_FILE "@output\n\n";
> close(OUTPUT_FILE);
> print STDERR "Termination of program ... !\n";
> exit;
>};
>#
>$hostname = $ARGV[0];
>$ip = $ARGV[1];
>$username = $ARGV[2];
>$password = $ARGV[3];
>$enpwd = $ARGV[4];
>$cmd_file = "cmds.txt";
>#
>my ($sec, $min, $hr, $mday, $mon, $year, @etc) = localtime(time);
>$mon++; $year=$year+1900;
>my $now=sprintf("%.4d%.2d%.2d%.2d%.2d", $year, $mon, $mday, $min,
>$sec);
>my $today=sprintf("%.2d/%.2d/%.4d", $mday, $mon, $year );
>#
># $out_file=$ip;
>$out_file=$ip."_$now".".cfg";
>#
># For debugging the TELNET Session: uncomment this line !
># $filename="telnet_dump.txt";
>#
>$errmode='return'; # Default: 'die'
>#
># Set cmd_remove_mode to the number of lines to remove (0 in this
>case).
># http://www.perlmonks.org/?node_id=736670
>$errmode="return";
>$telnet = new Net::Telnet ( Timeout=>5, Errmode=>$errmode ,
>cmd_remove_mode => '0', Dump_Log =>
>$filename);
>
># $telnet = new Net::Telnet ( Timeout=>3, Errmode=>$errmode ,
>Output_record_separator => "\r",cmd_remove_mode => '2', Dump_Log =>
>$filename);
># -output_record_separator => "\r");
>$telnet->open($ip);
>#
>$telnet->waitfor('/Username: $/i');
>$telnet->print($username);
>$telnet->waitfor('/Password: $/i');
>$telnet->print($password);
>#
># Wait for the prompt, send "enable" and enable-pwd
>@output = $telnet->waitfor('/>/');
>$telnet->print('enable');
>$telnet->waitfor('/Password:/i');
>$telnet->print($enpwd);
>#
>@output = $telnet->waitfor('/#/i');
>#
>open(OUTPUT_FILE, ">>$out_file");
>printf OUTPUT_FILE "IP Address: $ip\n";
>printf OUTPUT_FILE "Hostname: $hostname\n";
>printf OUTPUT_FILE "Data: $today\n";
>printf OUTPUT_FILE "\n";
>#
># Creat an universal Prompt for this hostname
>my $prompt = $hostname."#";
>$telnet->prompt("/$prompt\$/");
>@output = $telnet->cmd(String => "terminal length 0");
>printf OUTPUT_FILE "@output\n\n";
>#
>open(CMDFILE, "<$cmd_file");
>while (my $record = <CMDFILE>) {
> if ($record !~ /^(#|!|\s)/) {
> chomp($record);
> my @record_array = (split(/;/,$record));
> my $command = $record_array[0]; chomp($cmd);
> #
> $telnet->prompt("/$prompt\$/");
> @output = $telnet->cmd(String => "$command", Timeout=>30);
> #
> printf OUTPUT_FILE "@output\n\n";
> }
>}
># Close the command File for this device
>close(CMDFILE);
>#
>$telnet->print('exit');
>close(OUTPUT_FILE);
Its hard to tell what the problem is.
What does the LOG tell you? Where does that stop at?
Does it complete all the commands?
If something doesen't go right you should ISOLATE it, and just send that
command to find out why!
Chop up your code into a test bed for that item. Do enough to log in
and send the command then log off (which btw, you don't end the session
in the code).
Some things you can try:
- Use dump_log() to debug (but I think you do this).
- max_buffer_length($len); the default is 1 MB, take it up to 10
- Consider using a combination of print() and waitfor() as an alternative
to cmd() if the command you send prompts for input.
** The last time I used Telnet (2004), I only used this combination.
You can still capture the output if you use waitfor() in list context.
- When mode is "return" then the method generating the error places an
error message in the object and returns an undefined value in a scalar
context and an empty list in list context.
The error message may be obtained using errmsg().
** You have set this, but you don't check for !@output to see if
there was an error. Try using the default 'die' error mode.
- Make sure the server shell is not in console mode that buffers output.
You want it in line or stream mode.
Finally, using the print()-waitfor() pair, try to modify the below
method so its a function. You can use the $prematch to capture the output.
But more importantly, it gives you a finer granularity and more flexibility
deciphering the output stream. (There is a whole module this goes with,
but I didn't include it because it was written with a broader scope than
what your looking for).
-sln
#####################################################
# Send command and wait for reply
# - May wait for one of many reply regxs' passed in
# via the 'Reply' array. Each MUST be single
# quoted regex expressions. ie: '/any/i'
# IN:
# cmd - the shell command or program
# waitsecs - total secs willing to wait (up to)
# show_wait - 'yes' shows the seconds while waiting
# Reply - list of matches will wait for
# OUT:
# Returns index+1 into the 'Reply' list passed in,
# of the first match found in reply stream.
# Otherwise returns 0, meaning timeout or other
# error (check $self->{'Error'})
#####################################################
sub SendCommand
{
my ($self, $cmd, $waitsecs, $show_wait, @Reply) = @_;
my ($pre, $match);
if (!defined $tln) {
$self->{'Error'} = "Session not open";
return 0;
}
$waitsecs = $self->{'Waitsecs'} unless (defined $waitsecs);
$show_wait = $self->{'Show_Wait'} unless (defined $show_wait);
my @args = ('Timeout', 0);
if (@Reply == 0) { push (@Reply, $self->{'Prompt'}) }
for (@Reply) {
push (@args, 'Match');
push (@args, $_);
}
my $savedtimeout = $tln->timeout(0);
$tln->print ($cmd);
print "Sent: $cmd\n" if ($self->{'Debug'});
for (my $i = 0; $i < $waitsecs; $i++) {
($pre, $match) = $tln->waitfor(@args);
if (!$tln->timed_out) {
print "\rRecieved ($i seconds): $match \n" if ($self->{'Debug'});
print "\n$pre\n" if (lc($self->{'Show_Prematch'}) eq 'yes');
last;
}
sleep (1);
if ($show_wait eq lc('yes')) {
print "\rWait progress: ".($i+1)." seconds " ;
print "\n" if ($i == ($waitsecs-1));
}
}
$tln->timeout($savedtimeout);
## check if timed out
if ($tln->timed_out) {
print "\r** WAIT EXPIRED - $waitsecs seconds ** \n" if ($self->{'Debug'});
$self->{'Error'} = "Timed out ($waitsecs) executing command: $cmd";
return 0;
}
## return the index of the matched @Reply
#return 1 if (!@Reply);
my $pos = 0;
for (@Reply) {
$pos++;
my $patcheck = "last if (\$match =~ $_);"; # pattern match check
#print "$patcheck\n";
eval $patcheck;
}
$self->{'Error'} = '';
return $pos;
}
------------------------------
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 2914
***************************************