Find the answer to your Linux question:
Results 1 to 4 of 4
Code: #! /usr/bin/perl -w # # lg.pl Cisco Looking glass. To get this to work you need # to install Net::Telnet from the CPAN archives. # # Version: @(#)lg.pl 1.01 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2003
    Posts
    27

    Code interpretation help


    Code:
    #! /usr/bin/perl -w
    #
    # lg.pl		Cisco Looking glass. To get this to work you need
    #		to install Net::Telnet from the CPAN archives.
    #
    # Version:	@(#)lg.pl  1.01  21-Oct-1997  miquels@cistron.nl
    #
    #		Copyright (C) 1997 Cistron Internet Services B.V.
    #
    #	This program is free software; you can redistribute it and/or modify
    #	it under the terms of the GNU General Public License as published by
    #	the Free Software Foundation; version 2 dated June, 1991.
    
    use Net::Telnet 3.00;
    
    require '/wwwroot/cgi-bin/lg.cfg';
    #require '/var/www/virtual/html/lg.cfg';
    
    $| = 1;
    print <<EOF;
    Pragma&#58; no-cache
    Content-type&#58; text/html
    
    EOF
    
    #
    #	Procedure to unquote a string.
    #
    sub http_unquote &#123;
    	$_ = $_&#91;0&#93;;
    	s/\+/ /g;
    	s/\\&#40;.&#41;/$1/g;
    	s/%&#40;..&#41;/sprintf &#40;"%c", hex $1&#41;/ge;
    	s/\r//g;
    	$_;
    &#125;
     
    if &#40;$ENV&#123;'REQUEST_METHOD'&#125; eq 'POST'&#41; &#123;
    	sysread&#40;STDIN, $ENV&#123;'QUERY_STRING'&#125;, $ENV&#123;'CONTENT_LENGTH'&#125;&#41;;
    &#125;
    $cnt = 0;
    @pairs = split&#40;/&/, $ENV&#123;'QUERY_STRING'&#125;&#41;;
    foreach &#40;@pairs&#41; &#123;
    	$_ = &http_unquote &#40;$_&#41;;
    	&#40;$name, $val&#41; = split&#40;/=/,$_, 2&#41;;
    	$VAR&#123;$name&#125; = $val;
    &#125;
    $query = $VAR&#123;'query'&#125;;
    $Company = $config&#58;&#58;Company;
    $Logo    = $config&#58;&#58;Logo;
    $Email   = $config&#58;&#58;Email;
    
    #
    #	Empty query? Show the basic form.
    #
    if &#40;$query eq ''&#41; &#123;
    	$options = "\n";
    	for &#40;$i = 0; $i <= $#config&#58;&#58;Routers; $i++&#41; &#123;
    		$sel = &#40;$i == 0&#41; ? ' Selected' &#58; '';
    		$val = $config&#58;&#58;Routers&#91;$i&#93;;
    		$options .= "<Option Value = $val$sel> $val\n";
    	&#125;
    	require './lg.html.inc';
    	#require '/var/www/virtual/html/lg.html.inc';
    	
    	exit&#40;0&#41;;
    &#125;
    
    #
    #	Find out which cisco we want to use.
    #
    if &#40;$VAR&#123;'router'&#125; eq ''&#41; &#123;
    	print "No router defined\n";
    	exit&#40;1&#41;;
    &#125;
    $nick = $config&#58;&#58;cfg&#123;$VAR&#123;'router'&#125;&#125;;
    if &#40;&#40;$Host = $nick->&#123;Host&#125;&#41; eq ''&#41; &#123;
    	print "Unknown $VAR&#123;'router'&#125;\n";
    	exit&#40;1&#41;;
    &#125;
    $Port = $nick->&#123;Port&#125;;
    $Prompt = $nick->&#123;Prompt&#125;;
    $Login = $nick->&#123;Login&#125;;
    $Pass = $nick->&#123;Pass&#125;;
    $Timeout = $nick->&#123;Timeout&#125;;
    $IXPoint = $nick->&#123;IXPoint&#125;;
    
    #
    #	Try to make it secure.
    #
    $addr = $VAR&#123;'addr'&#125;;
    $addr =~ s/&#91;^\-a-zA-Z0-9. \t_^\*\$&#93;//g;
    $addr =~ s/^\s+//;
    $addr =~ s/\s+$//;
    $addr = "0.0.0.0" if &#40;$addr eq ''&#41;;
    
    print <<EOF;
    <Html>
    <Head>
    <Title>$Company - StarHub Looking Glass Results</Title>
    </Head>
    <Body>
    
    <center>
    <table>
    	<td>
    		[img]$Logo[/img]
    	</td>
    	
    	<td></td>
    
    	<td>
    		<font face="Arial" size="6">Looking Glass</font>
    	</td>
    
    </table>
    </center>
    
    <Center>
    <Hr size=2 width=85%><nobr>
    <font face="Arial" size="3">$IXPoint Looking Glass Results</font>
    <nobr><Hr size=2 width=85%>
    </Center>
    
    
    
    
    
    
    
    
    <Center>
    Query&#58; $query
    EOF
    
    #
    #	See what command we want to send.
    #
    
    if &#40;$query eq 'bgp'&#41; &#123;
    	$cmd = "show ip bgp $addr";
    &#125; elsif &#40;$query eq 'summary'&#41; &#123;
    	$cmd = "show ip bgp summary";
    	$addr = '';
    &#125; elsif &#40;$query eq 'neighbours'&#41; &#123;
            $cmd = "show ip bgp neigh $addr";
    &#125; elsif &#40;$query eq 'received-routes'&#41; &#123;
            $cmd = "show ip bgp neigh $addr received-routes";
    &#125; elsif &#40;$query eq 'bgp dampened-paths'&#41; &#123;
    	$cmd = "show ip bgp dampened-paths";
    	$addr = '';
    &#125; elsif &#40;$query eq 'environmental'&#41; &#123;
    	$cmd = "show environment";
    	$addr = '';
    &#125; elsif &#40;$query eq 'bgp flap-statistics'&#41; &#123;
    	$cmd = "show ip bgp flap-statistics";
    	$addr = '';
    &#125; elsif &#40;$query eq 'ping'&#41; &#123;
    	$cmd = "ping $addr";
    &#125; elsif &#40;$query eq 'trace'&#41; &#123;
    	$cmd = "trace $addr";
    &#125; else &#123;
    	$cmd = "show clock";
    	$addr = '';
    &#125;
    print "
    Addr&#58; $addr\n" if &#40;$addr ne ''&#41;;
    print "</Center>
    
    <Pre>\n";
    
    $ses = new Net&#58;&#58;Telnet  &#40;Host => $Host,
                            Port => $Port,
                            Prompt => "/$Prompt/i",
    			Timeout => $Timeout&#41;;
    
    if &#40;!$ses&#41; &#123;
    	print "lg.pl&#58; connect to router $Host failed\n";
    	exit&#40;1&#41;;
    &#125;
    
    #
    #	Login and turn off `more' style paging.
    #
    if &#40;$Login ne '' and $Pass ne ''&#41; &#123;
    	$ses->login&#40;$Login, $Pass&#41;;
    &#125; elsif &#40;$Login eq '' and $Pass ne ''&#41; &#123;
    	$ses->waitfor&#40;'/password&#91;&#58; &#93;*$/i'&#41;;
    	$ses->cmd&#40;$Pass&#41;;
    &#125; else &#123;
    	$ses->waitfor&#40;"/$Prompt/i"&#41;;
    &#125;
    $ses->cmd&#40;"term len 0"&#41;;
    
    #
    #	Send command.
    #
    $ses->print&#40;$cmd&#41;;
    
    #
    #	Print the output up to the next prompt.
    #
    $first = 1;
    $ses->input_record_separator&#40;"\xff"&#41;;
    
    while &#40;&#40;$junk, $_&#41; = $ses->waitfor&#40;"/&#40;^.*\n|$Prompt&#41;/i"&#41;&#41; &#123;
    	if &#40;$first&#41; &#123;
    		$first = 0;
    		next;
    	&#125;
    	last if &#40;/$Prompt/&#41;;
    	print;
    &#125;
    $ses->input_record_separator&#40;"\n"&#41;;
    $ses->cmd&#40;"exit"&#41;;
    $ses->close;
    
    print <<EOF;
    </Pre>
    
    
    
    <Hr size=2 width=85%>
    
    
    
    </Body>
    <Tail>
    <Center>
    
      Please e-mail questions or comments to
     <A href=mailto&#58;$Email>$Email</A>.
    
    </Center>
    </Tail>
    </Html>
    EOF
    
    #0;
    Can someone explain top me what this part does?
    Code:
    $ses->input_record_separator&#40;"\xff"&#41;;
    
    while &#40;&#40;$junk, $_&#41; = $ses->waitfor&#40;"/&#40;^.*\n|$Prompt&#41;/i"&#41;&#41; &#123;
    	if &#40;$first&#41; &#123;
    		$first = 0;
    		next;
    	&#125;
    	last if &#40;/$Prompt/&#41;;
    	print;
    &#125;

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Well, I don't really know how the net::telnet thing works, but it seems that it gets a line, skips the first line and then prints all lines until it gets a line matching $Prompt.

  3. #3
    Just Joined!
    Join Date
    Sep 2003
    Posts
    27
    What about the
    Code:
    $ses->input_record_separator&#40;"\xff"&#41;;
    What does the "\xff" do?

    I also can't understand the variables for perl. What's it with ($junk, $_)
    2 variables? I come from C background so PHP was alot easier for me.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    "\xff" is a standard C-style escape sequence, which equals the character represented by ASCII 255.

    ($junk, $_) constructs an array containing the variables $junk and $_. $ses->waitfor probably (I'm guessing here, since I don't know the net::telnet module) returns an array that contains the substring matches of the regexp. The first element is probably the whole match and the second one the first substring match and so on. When this returned array is applied on the one contructed by ($junk, $_), the elements are assigned in their respective order to the elements in the left-hand array, so that the entire match is put into $junk, and substring match (ie. either the entire line if it didn't match $Prompt, or $Prompt if it did match $Prompt) is stored into $_.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •