Orion SecureCRT Session Creation

From Internetworkpro

Jump to: navigation, search
#!perl
#*******************************************************************************
# SecureCRT session generator from Solarwinds Orion nodes
#
# Written by: Brandon Bennett
#             <bennetb@gmail.com>
#             nemith on #cisco@irc.freenode.net
#
# Connects to the Orion SQL database and pulls all nodes.  Nmap is ran on the node
# to determine if it can run SSH or Telnet and create a 
#
#
# TODO:
#  * More error handling
#  * split into smarter subs
#  * Create all sessions in temporary directory and then move over after done
#
#  need to install DBI, DBD::ADO, and XML::Simple
#
# ActivePerl:
#   ppm install DBI
#   ppm install DBD-ADO
#   ppm install XML-Simple
#
use DBI;
use XML::Simple;
use File::Copy;
use Data::Dumper;
 
# Location of nmap on the system (I am using 4.01 compiled for win32)
my $nmap_location  = "c:\\Program Files\\nmap\\nmap.exe";  
 
# Options to use for nmap.  The defaults are fine and should be left alone 
my $nmap_options   = "-v -v -v -sS -p 22-23 -O -A -oX -";  # -oX is required!
 
# Orion server and database names
my $orion_server   = "SRVITSQL1";
my $orion_database = "OrionNPM";
 
# Where to save the created sessions
my $session_path = "C:\\Documents and Settings\\bbennett\\Application Data\\VanDyke\\Config\\Sessions";
 
# The default session file to use.  Make sure the options:
#      s:"Protocol Name"
#      s:"Hostname"
#      s:"Color Scheme"
# are not present in this file.  All other options are valid
my $default_session_file = "C:\\Docs\\Devel\\Perl\\default-session.ini";
 
#
# List of cols from SQL to group the sessions by.  Each additional group added will
# become a subgroup of the group above it.
#
$group[0] = "State";
$group[1] = "City";
 
#################################################################################
 
my $dsn = "Provider=sqloledb;Data Source=$orion_server;Initial Catalog=$orion_database;Integrated Security=SSPI;";
 
my $dbh = DBI->connect("dbi:ADO:$dsn")
  or die "Couldn't connect to database: " . DBI->errstr;
 
my $custom_cols = join(',', @group);
 
$sth = $dbh->prepare("SELECT IP_Address, SysName, Caption, $custom_cols FROM Nodes ORDER BY $custom_cols")
  or die "Couldn't prepare statement: " . $dbh->errstr;
 
$sth->execute();
 
my @last_group = ();
my $path = "";
 
while( $hash = $sth->fetchrow_hashref() ) {
 
  foreach my $i (0..$#group) {
    if ($last_group[$i] ne $hash->{$group[$i]}) {
      $path = $session_path;
      foreach my $j (0..$i) {
        $path .= "\\" . $hash->{$group[$j]};
      }
      mkdir($path);
      print "$group[$i]: " . $hash->{$group[$i]} . "\n";
    }
  }
 
  my $sysname = trim($hash->{'SysName'});
  $sysname = trim($hash->{'Caption'}) unless $sysname;  
  $sysname = [split (/\./, $sysname)]->[0];
 
  my $ip = $hash->{'IP_Address'};
 
  $xml = new XML::Simple;
  $output = `\"$nmap_location\" $nmap_options $ip`;
  $data = $xml->XMLin($output);
 
  if ($data->{'host'}->{'ports'}->{'port'}) {
    for (reverse(@{$data->{'host'}->{'ports'}->{'port'}})) {
      if ($_->{'state'}->{'state'} eq "open") {       # Make sure the port is open
 
        if ($_->{'portid'} == 22) {                   # SSH baby
          if ($_->{'service'}->{'version'} < 2) {
            $protocol = "sshv1";                      # SSH Version 1
          } else {
            $protocol = "sshv2";                      # SSH Version 2
          }   
        } elsif ($_->{'portid'} == 23) {              # Telnet
          $protocol = "telnet";
        } else {
          $protocol = "unknown";
        }
      }
    }
  } else {
    $protocol = "unknown";
  }
 
  print "$protocol:    ip:$ip\tsysname: $sysname\n";
 
  my $file = "$path\\$sysname.ini";
 
  print "copying $default_session_file to $file\n";
 
  copy($default_session_file, $file)
    or die "File cannot be copied.";
 
  open(SES, ">>" . $file);
 
  if ($protocol eq "sshv1") {
    print SES <<ENDOF;
s:"Protocol Name"=SSH1
s:"Hostname"=$ip
s:"Color Scheme"=Yellow / Black
ENDOF
  } elsif ($protocol eq "sshv2") {
    print SES <<ENDOF;
s:"Protocol Name"=SSH1
s:"Hostname"=$ip
s:"Color Scheme"=Yellow / Black
ENDOF
  } elsif ($protocol eq "telnet") {
    print SES <<ENDOF;
s:"Protocol Name"=Telnet
s:"Hostname"=$ip
s:"Color Scheme"=Traditional
ENDOF
  }
 
  close(SES);
 
  foreach my $i (0..$#group) {
    $last_group[$i] = $hash->{$group[$i]};
  }
 
}
 
$sth->finish;
$dbh->disconnect;
 
##
## Trim the spaces from the front and rear
##
sub trim($) {
  my $string = shift;
  $string =~ s/^\s+//;
  $string =~ s/\s+$//;
  return $string;
}
Personal tools
Namespaces
Variants
Actions
Navigation
Categories
Toolbox