WaltSteadman
MIS
Greetings all,
I have almost got this working but I am missing something very simple I am sure. I had never used subs in my perl scripts so this is new to me. Any assistance would be much appreciated.
When I run my script here is the error I get:
login failed: ********************************************************************
Couldn't connect to switchname
below is my script:
I have almost got this working but I am missing something very simple I am sure. I had never used subs in my perl scripts so this is new to me. Any assistance would be much appreciated.
When I run my script here is the error I get:
login failed: ********************************************************************
Couldn't connect to switchname
below is my script:
Code:
#!/usr/bin/perl -w
#print "Content-type: text/plain; charset=iso-8859-1\n\n";
#require "cgi-lib.pl";
use Date::Calc qw(:all);
use Text::Wrap;
use CGI::Pretty qw/:standard :cgi-lib/;
use Getopt::Long;
use Net::Telnet;
use Net::SSH::Perl;
#use CGI::Carp qw(fatalsToBrowser);
use strict;
#($year,$month,$day) = Today();
#$date = join ('',Today());
my @switch=();
my $user='userid';
my $swipwd='password';
my $switch='switchname';
my $ns_ssh = ConnectToVPN($user, $swipwd, $switch);
if ($ns_ssh) {
sleep(1);
my @version = $ns_ssh->cmd("show version");
print @version;
print "\n";
}
print "\n";
#================= SUBS =====================
sub ConnectToVPN {
my ($user, $swipwd, $switch) = @_;
my $vpn_prompt = '/[>] $/';
## Start ssh program.
my $vpn_pty = &spawn("ssh", "-l", $user, $switch);
#, "-F", "/home/vpn/newvpncheck/config");
# spawn() defined below
## Create a Net::Telnet object to perform I/O on ssh's tty.
my $vpn_ssh = new Net::Telnet (-fhopen => $vpn_pty,
-prompt => $vpn_prompt,
-telnetmode => 0,
-cmd_remove_mode => 1,
-output_record_separator => "\n",
);
## Login to remote host.
## Catch key fingerprint and add it if we don't have it.
if ($vpn_ssh->waitfor(-match => '/yes/', -errmode => "return", -timeout => 5)) {
$vpn_ssh->print("yes");
}
my $vpn_test = $vpn_ssh->waitfor(-match => '/password: ?$/i',
-errmode => "return");
# or warn "problem connecting to $vpn_host: ";
## exit if we can't connect
if(!$vpn_test) { print "Couldn't connect to $switch\n"; return; }
$vpn_ssh->print($swipwd);
$vpn_test = $vpn_ssh->waitfor(-match => $vpn_ssh->prompt,
-errmode => "return")
or warn "login failed: ", $vpn_ssh->lastline;
if(!$vpn_test) { print "\nCouldn't connect to $switch\n\n"; return; }
$vpn_ssh;
}
sub spawn {
my(@cmd) = @_;
my($pid, $pty, $tty, $tty_fd);
## Create a new pseudo terminal.
use IO::Pty ();
$pty = new IO::Pty or die $!;
## Execute the program in another process.
unless ($pid = fork) { # child process
die "problem spawning program: $!\n" unless defined $pid;
## Disassociate process from existing controlling terminal.
use POSIX ();
POSIX::setsid
or die "setsid failed: $!";
## Associate process with a new controlling terminal.
$pty->make_slave_controlling_terminal;
$tty = $pty->slave;
$tty_fd = $tty->fileno;
close $pty;
## Make stdio use the new controlling terminal.
open STDIN, "<&$tty_fd" or die $!;
open STDOUT, ">&$tty_fd" or die $!;
open STDERR, ">&STDOUT" or die $!;
close $tty;
## Execute requested program.
exec @cmd
or die "problem executing $cmd[0]\n";
} # end child process
$pty;
} # end sub spawn