Gheek.net

July 22, 2011

How to use curl to access a HTTPS URL using a Client Certificate

Filed under: Apache, linux, nix, shell scripts, tomcat — lancevermilion @ 11:14 am

I needed to check a HTTPS URL that required authentication of a client Certificate from the command line.

Using curl was a perfect fit.

curl --insecure --cert-type pem --cert /home/dummyuser/client-cert-stacked.pem --interface eth0:1 "https://192.168.1.2/GetKeepAlive"

Note: I use double quotes " " around the url because if there are any special characters or spaces it will not be read correctly.

Options Option Description
--insecure or -k Disable Certificate Verification against a Root/Intermediate
--cert or -E (HTTPS) Tells curl to use the specified certificate file. The certificate must be in PEM format.

If the optional password isn’t specified, it will be queried for on the terminal. Note that this certificate is the
private key and the private certificate concatenated!

If this option is used several times, the last one will be used.

--cert-type (SSL) Private key file type (DER, PEM, and ENG are supported).
--interface Query the URL using a specified interface.
Advertisement

February 28, 2011

Howto Make Script More Portable With #!/usr/bin/env As a Shebang

Filed under: expect, linux, nix, perl, php, shell scripts — lancevermilion @ 9:33 am

The following article is taken directly from http://www.cyberciti.biz/tips/finding-bash-perl-python-portably-using-env.html by VIVEK GITE.

You may have noticed that most shell and perl script starts with the following line:
#!/bin/bash
OR
#!/usr/bin/perl

It is called a shebang. It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash. All scripts under UNIX and Linux execute using the interpreter specified on a first line.

However there is a small problem. BASH or Perl is not always in the same location (read as PATH) such as /bin/bash or /usr/bin/perl. If you want to make sure that script is portable across different UNIX like operating system you need to use /usr/bin/env command.

env command allows to run a program in a modified environment.

Find line
#!/bin/bash

Replace with
#!/usr/bin/env bash

For example here is a small script:

#!/usr/bin/env bash
x=5
y=10
echo "$x and $y"

OR

#!/usr/bin/env perl
use warnings;
print "Hello " x 5;
print "\\n";

Now you don’t have to search for a program via the PATH environment variable. This makes the script more portable. Also note that it is not foolproof method. Always make sure you have /usr/bin/env exists or use a softlink/symbolic link to point it to correct path. And yes your work (script) looks more professional with this hack 🙂

February 21, 2011

Perl Script similar to linux command tree

Filed under: nix, perl — lancevermilion @ 5:34 pm

I had the desire to have a nice ASCII output of how a directory looks including all subdirectories and files. I ran across a nice script someone had done. The script Perl Script for Directory Tree did what I wanted for the most part but I didn’t have the ability to display things like Byte/block size of each file/directory, Computed Hash (Sha512sum,Sha1sum,md5sum,etc), file type, etc.

To get the output I wanted I rewrote the script to be more scalable for my purpose. I changed from using an arrary(@array) to a hash_ref ($hash_ref) because a hash_ref was a better fit. I ideally will tie this to a backup script and  make it a nice sub routine.

Tree.pl

#!/usr/bin/perl

use strict;
use File::Basename;

sub BytesToReadableString($) {
   my $c = shift;
   $c >= 1073741824 ? sprintf("%0.2f GB", $c/1073741824)
      : $c >= 1048576 ? sprintf("%0.2f MB", $c/1048576)
      : $c >= 1024 ? sprintf("%0.2f KB", $c/1024)
      : $c . " B";
}

my $no_of_args = @ARGV;
my $expand_size = 5;
my $max_width = 10;
my $dir_path;
my $dir_name;
my $string;

if(($no_of_args == 0) || ($no_of_args == 2))
{
  print "ERROR: Insufficient argument\n";
  goto help;
}
elsif($no_of_args == 3)
{
  if($ARGV[1] !~ "-width")
  {
    print "ERROR: Invalid argument\n";
    goto help;
  }
  elsif(($ARGV[2] > $max_width) || ($ARGV[2] < 2))
  {
    print "ERROR: Width should not lie outside 2 to $max_width.\n";
    goto help;
  }
  else
  {
    $expand_size = $ARGV[2];
  }
}
$dir_path = $ARGV[0];
$dir_path =~ s/^(\/.*)(\/$)/$1/g;
if(!(-d $dir_path))
{
  print "ERROR: $dir_path doesn't exist\n";
  goto help;
}

if(($dir_path =~ /^\.$/) || ($dir_path =~ /^\.\/$/))
{
  $string = `pwd`;
  $dir_name = basename("$dir_path");
}
else
{
  #$dir_name = $dir_path;
  $dir_name = basename("$dir_path");
}
my $hash_ref = {};
my $totalbytesize = 0;
my $totalblocks = 0;

# Process input line by line and populate the hash accordingly
open input, "ls -lasd \$(find $dir_path) |";
while(<input>)
{
  next if /^total/;
  chomp;
  split;
  $hash_ref->{$_[9]}->{'DTL'} = $_[9];
  $hash_ref->{$_[9]}->{'blocks'} = $_[0];
  $hash_ref->{$_[9]}->{'bytesize'} = $_[5];
  $hash_ref->{$_[9]}->{'md5'} = 'N/A'; #Fill all keys will some value
  $hash_ref->{$_[9]}->{'type'} = 'file' if ( -f $_[9] );
  $hash_ref->{$_[9]}->{'type'} = 'symlink' if ( -l $_[9] );
  $hash_ref->{$_[9]}->{'type'} = 'directory' if ( -d $_[9] );
  $totalblocks += $_[0];
  $totalbytesize += $_[5];
}
close(input);

# Add the file HASH
open input, "find $dir_path ! -type d -exec md5sum '{}' \\; |";
while(<input>)
{
  split;
  die "\n" . "#" x 30 . "\nNEW FILE NOT IN HASH: $_[1]\n" . "#" x 30 . "\n" if (! defined $hash_ref->{$_[1]} );
  $hash_ref->{$_[1]}->{'md5'} = $_[0];
}
close(input);

for my $file ( sort keys %$hash_ref )
{
  next if ( ! defined $file );
  $hash_ref->{$file}->{'DTL'} =~ s/^$dir_path/  /;
  $hash_ref->{$file}->{'DTL'} =~ s/.[^\/]*\//--/g;
}

for my $file ( sort keys %$hash_ref )
{
  $hash_ref->{$file}->{'DTL'} = "\|\-" . $hash_ref->{$file}->{'DTL'};
  $string = " " x ($expand_size - 1);
  while($hash_ref->{$file}->{'DTL'} =~ /--/)
  {
    $hash_ref->{$file}->{'DTL'} =~ s/--//;
    $hash_ref->{$file}->{'DTL'} =~ s/\|\-/|$string|-/;
  }
  $string = "-" x ($expand_size - 1);
  $hash_ref->{$file}->{'DTL'} =~ s/\-/$string/;
  $hash_ref->{$file}->{'DTL'} =~ s/\|$string/\+$string/;
}

my @cross = 0;
for my $file ( reverse sort keys %$hash_ref )
{
  my $line = $hash_ref->{$file}->{'DTL'};
  my @line_array = split(//, $line);
  my $no_of_char = @line_array;
  my $char_count = 0;
  while($char_count < $no_of_char)
  {
    if($line_array[$char_count] =~ /\+/)
    {
      $cross[$char_count] = 1;
    }
    elsif($line_array[$char_count] =~ /\|/)
    {
      if($cross[$char_count] == 0)
      {
        $line_array[$char_count] = " ";
      }
    }
    else
    {
      $cross[$char_count] = 0;
    }
    $char_count = $char_count + $expand_size;
  }
  $hash_ref->{$file}->{'DTL'} = join("", @line_array);
}

print "$dir_path (" . BytesToReadableString($totalbytesize) . ")\n";
for my $file ( sort keys %$hash_ref )
{
  print "$hash_ref->{$file}->{'DTL'} (" . BytesToReadableString($hash_ref->{$file}->{'bytesize'}) . ")\n" if ( $hash_ref->{$file}->{'DTL'} !~ /\s$/ );
}
exit;

help:
printf("#####################################################################\n");
printf("# Original Author: http://sandeep-vaniya.blogspot.com/2008/04/perl-script-for-directory-tree.html\n");
printf("# Modified Author: https://gheeknet.wordpress.com/?p=137\n");
printf("# To run the script,\n");
printf("# perl tree.pl <PATH> -width <width_no>\n");
printf("#    e.g. \"perl tree.pl . \" displays tree view of current dir\n");
printf("# \"-width\" expand or compress the tree. Max = $max_width, Min = 2\n");
printf("# Default width = 5\n");
printf("#####################################################################\n");

Sample output with size of directory/file.

tree.pl /tmp/collect/
/tmp/collect (66.11 MB)
     +----etc (1.00 KB)
     |    +----aliases (1.57 KB)
     |    +----hosts (236 B)
     |    +----httpd (1.00 KB)
     |    |    +----conf.d (1.00 KB)
     |    |         +----rancid.conf (1.87 KB)
     |    +----motd (0 B)
     |    +----my.cnf (441 B)
     |    +----pam.d (1.00 KB)
     |    |    +----system-auth-ac (1.06 KB)
     |    +----raddb (1.00 KB)
     |    |    +----server (1.25 KB)
     |    +----rancid.conf (3.49 KB)
     |    +----resolv.conf (84 B)
     |    +----snmp (1.00 KB)
     |    |    +----snmpd.conf (3.53 KB)
     |    +----ssh (1.00 KB)
     |    |    +----ssh_config (1.78 KB)
     |    |    +----sshd_config (3.24 KB)
     |    +----sysconfig (1.00 KB)
     |    |    +----network (62 B)
     |    |    +----network-scripts (1.00 KB)
     |    |         +----ifcfg-bond0 (279 B)
     |    |         +----ifcfg-bond1 (219 B)
     |    |         +----ifcfg-eth0 (147 B)
     |    |         +----ifcfg-eth1 (147 B)
     |    |         +----ifcfg-eth2 (155 B)
     |    |         +----ifcfg-eth3 (155 B)
     |    |         +----ifcfg-eth4 (108 B)
     |    |         +----ifcfg-eth5 (108 B)
     |    +----viewvc (1.00 KB)
     |         +----viewvc.conf (36.07 KB)
     +----opt (1.00 KB)
     |    +----opennms (1.00 KB)
     |         +----etc (6.00 KB)
     |         |    +----README.notification (9.45 KB)
     |         |    +----ackd-configuration.xml (1.08 KB)
     |         |    +----actiond-configuration.xml (129 B)
     |         |    +----ami-config.xml (116 B)
     |         |    +----asterisk-configuration.properties (2.00 KB)
     |         |    +----availability-reports.xml (1.77 KB)
     |         |    +----c3p0.properties (10.70 KB)
     |         |    +----capsd-configuration.xml (9.37 KB)
     |         |    +----categories.xml (4.92 KB)
     |         |    +----chart-configuration.xml (7.22 KB)
     |         |    +----collectd-configuration.xml (1.75 KB)
     |         |    +----configured (0 B)
     |         |    +----create.sql (98.40 KB)
     |         |    +----database-reports.xml (1.31 KB)
     |         |    +----database-schema.xml (4.79 KB)
     |         |    +----datacollection-config.xml (257.12 KB)
     |         |    +----destinationPaths.xml (378 B)
     |         |    +----dhcpd-configuration.xml (187 B)
     |         |    +----discovery-configuration.xml (355 B)
     |         |    +----eventconf.xml (96.50 KB)
     |         |    +----eventd-configuration.xml (277 B)
     |         |    +----events-archiver-configuration.xml (93 B)
     |         |    +----events.archiver.properties (3.58 KB)
     |         |    +----exclude-ueis.properties (10 B)
     |         |    +----getManagePercentAvailIntfWindow.sql (3.07 KB)
     |         |    +----getManagePercentAvailNodeWindow.sql (2.91 KB)
     |         |    +----getManagedOutageForIntfInWindow.sql (2.69 KB)
     |         |    +----getManagedOutageForNodeInWindow.sql (2.66 KB)
     |         |    +----getManagedServiceCountForIntf.sql (2.06 KB)
     |         |    +----getManagedServiceCountForNode.sql (2.16 KB)
     |         |    +----getOutageTimeInWindow.sql (4.18 KB)
     |         |    +----getPercentAvailabilityInWindow.sql (2.73 KB)
     |         |    +----groups.xml (542 B)
     |         |    +----http-datacollection-config.xml (1.14 KB)
     |         |    +----jasper-reports.xml (553 B)
     |         |    +----java.conf (31 B)
     |         |    +----javamail-configuration.properties (1.33 KB)
     |         |    +----javamail-configuration.xml (3.25 KB)
     |         |    +----jcifs.properties (5.97 KB)
     |         |    +----jdbc-datacollection-config.xml (935 B)
     |         |    +----jmx-datacollection-config.xml (10.17 KB)
     |         |    +----ksc-performance-reports.xml (51 B)
     |         |    +----libraries.properties (114 B)
     |         |    +----linkd-configuration.xml (7.43 KB)
     |         |    +----log4j-controller.properties (1.35 KB)
     |         |    +----log4j.properties (25.67 KB)
     |         |    +----magic-users.properties (2.45 KB)
     |         |    +----map.enable (547 B)
     |         |    +----map.properties (18.31 KB)
     |         |    +----mapsadapter-configuration.xml (622 B)
     |         |    +----microblog-configuration.xml (970 B)
     |         |    +----model-importer.properties (778 B)
     |         |    +----modemConfig.properties (1.59 KB)
     |         |    +----monitoring-locations.xml (205 B)
     |         |    +----notifd-configuration.xml (2.20 KB)
     |         |    +----notificationCommands.xml (7.27 KB)
     |         |    +----notifications.xml (5.76 KB)
     |         |    +----nsclient-config.xml (97 B)
     |         |    +----nsclient-datacollection-config.xml (5.96 KB)
     |         |    +----opennms-datasources.xml (959 B)
     |         |    +----opennms-server.xml (72 B)
     |         |    +----opennms.properties (18.45 KB)
     |         |    +----otrs.properties (869 B)
     |         |    +----poll-outages.xml (44 B)
     |         |    +----poller-config.properties (2.41 KB)
     |         |    +----poller-configuration.xml (14.14 KB)
     |         |    +----provisiond-configuration.xml (1.12 KB)
     |         |    +----rancid-configuration.xml (451 B)
     |         |    +----reportd-configuration.xml (1.00 KB)
     |         |    +----response-adhoc-graph.properties (1.29 KB)
     |         |    +----response-graph.properties (13.40 KB)
     |         |    +----rrd-configuration.properties (8.45 KB)
     |         |    +----rt.properties (1.95 KB)
     |         |    +----rtc-configuration.xml (232 B)
     |         |    +----rws-configuration.xml (368 B)
     |         |    +----rws-nbinventoryreport.xsl (17.93 KB)
     |         |    +----rws-rancidlistreport.xsl (16.23 KB)
     |         |    +----scriptd-configuration.xml (161 B)
     |         |    +----service-configuration.xml (14.01 KB)
     |         |    +----setIfServiceKeysOnInsertTrigger.sql (2.02 KB)
     |         |    +----setIfServiceKeysOnUpdateTrigger.sql (2.04 KB)
     |         |    +----setIpInterfaceKeysOnInsertTrigger.sql (2.42 KB)
     |         |    +----setIpInterfaceKeysOnUpdateTrigger.sql (2.49 KB)
     |         |    +----setSnmpInterfaceKeysOnInsertTrigger.sql (2.06 KB)
     |         |    +----setSnmpInterfaceKeysOnUpdateTrigger.sql (2.57 KB)
     |         |    +----site-status-views.xml (757 B)
     |         |    +----smsPhonebook.properties (23 B)
     |         |    +----snmp-adhoc-graph.properties (1.29 KB)
     |         |    +----snmp-asset-adapter-configuration.xml (3.17 KB)
     |         |    +----snmp-config.xml (102 B)
     |         |    +----snmp-graph.properties (540.77 KB)
     |         |    +----snmp-interface-poller-configuration.xml (910 B)
     |         |    +----statsd-configuration.xml (974 B)
     |         |    +----surveillance-views.xml (1.08 KB)
     |         |    +----syslogd-configuration.xml (3.17 KB)
     |         |    +----threshd-configuration.xml (2.27 KB)
     |         |    +----thresholds.xml (4.33 KB)
     |         |    +----tl1d-configuration.xml (296 B)
     |         |    +----translator-configuration.xml (6.22 KB)
     |         |    +----trapd-configuration.xml (93 B)
     |         |    +----users.xml (508 B)
     |         |    +----vacuumd-configuration.xml (16.72 KB)
     |         |    +----viewsdisplay.xml (717 B)
     |         |    +----vulnscand-configuration.xml (22.36 KB)
     |         |    +----wmi-config.xml (141 B)
     |         |    +----wmi-datacollection-config.xml (15.26 KB)
     |         |    +----xmlrpcd-configuration.xml (1.09 KB)
     |         |    +----xmp-config.xml (112 B)
     |         |    +----xmp-datacollection-config.xml (4.81 KB)
     |         |    +----xmpp-configuration.properties (1.32 KB)
     |         +----jetty-webapps (1.00 KB)
     |         |    +----opennms (1.00 KB)
     |         |         +----report (1.00 KB)
     |         |              +----index.jsp (5.45 KB)
     |         +----share (1.00 KB)
     |              +----rrd (1.00 KB)
     |                   +----.readme (32 B)
     +----tmp (1.00 KB)
     |    +----rpms.txt (642.32 KB)
     +----var (1.00 KB)
          +----lib (1.00 KB)
          |    +----pgsql (1.00 KB)
          |         +----backups (1.00 KB)
          |         |    +----pgsql_02_18_2011.pgsql (63.88 MB)
          |         |    +----pgsql_globals_02_18_2011.pgsql (611 B)
          |         +----data (1.00 KB)
          |              +----pg_hba.conf (3.50 KB)
          |              +----pg_ident.conf (1.59 KB)
          |              +----postgresql.conf (16.47 KB)
          +----net-snmp (1.00 KB)
          |    +----snmpd.conf (1.43 KB)
          +----rancid (1.00 KB)
               +----.cloginrc (116 B)
               +----.cloginrc.orig (3.81 KB)
               +----CVS (1.00 KB)
               |    +----CVSROOT (1.00 KB)
               |    |    +----.#checkoutlist (495 B)
               |    |    +----.#commitinfo (760 B)
               |    |    +----.#config (993 B)
               |    |    +----.#cvswrappers (602 B)
               |    |    +----.#editinfo (1.00 KB)
               |    |    +----.#loginfo (1.14 KB)
               |    |    +----.#modules (1.12 KB)
               |    |    +----.#notify (564 B)
               |    |    +----.#rcsinfo (649 B)
               |    |    +----.#taginfo (879 B)
               |    |    +----.#verifymsg (1.00 KB)
               |    |    +----checkoutlist (495 B)
               |    |    +----checkoutlist,v (697 B)
               |    |    +----commitinfo (760 B)
               |    |    +----commitinfo,v (962 B)
               |    |    +----config (993 B)
               |    |    +----config,v (1.17 KB)
               |    |    +----cvswrappers (602 B)
               |    |    +----cvswrappers,v (804 B)
               |    |    +----editinfo (1.00 KB)
               |    |    +----editinfo,v (1.20 KB)
               |    |    +----history (95 B)
               |    |    +----loginfo (1.14 KB)
               |    |    +----loginfo,v (1.34 KB)
               |    |    +----modules (1.12 KB)
               |    |    +----modules,v (1.32 KB)
               |    |    +----notify (564 B)
               |    |    +----notify,v (766 B)
               |    |    +----rcsinfo (649 B)
               |    |    +----rcsinfo,v (851 B)
               |    |    +----taginfo (879 B)
               |    |    +----taginfo,v (1.06 KB)
               |    |    +----val-tags (0 B)
               |    |    +----verifymsg (1.00 KB)
               |    |    +----verifymsg,v (1.20 KB)
               |    +----Entries.Log (20 B)
               |    +----GW1_Network (1.00 KB)
               |         +----router.db,v (166 B)
               +----GW1_Network (1.00 KB)
               |    +----.cvsignore (47 B)
               |    +----CVS (1.00 KB)
               |    |    +----Entries (56 B)
               |    |    +----Repository (12 B)
               |    |    +----Root (16 B)
               |    +----configs (1.00 KB)
               |    |    +----CVS (1.00 KB)
               |    |         +----Entries (2 B)
               |    |         +----Repository (20 B)
               |    |         +----Root (16 B)
               |    +----router.db (0 B)
               |    +----routers.all (0 B)
               |    +----routers.down (0 B)
               |    +----routers.up (0 B)
               +----logs (97.00 KB)

Sample output with size of directory/file and md5 hash.

tree /tmp/collect/
/tmp/collect (66.11 MB)
     +----etc (1.00 KB) [N/A]
     |    +----aliases (1.57 KB) [f0c0ac6492ff4e688030313ef186fc16]
     |    +----hosts (236 B) [78d96b0e5a9b05803fc38705ee9986b0]
     |    +----httpd (1.00 KB) [N/A]
     |    |    +----conf.d (1.00 KB) [N/A]
     |    |         +----rancid.conf (1.87 KB) [cc9cca2912a0b246601bba483f99cf53]
     |    +----motd (0 B) [d41d8cd98f00b204e9800998ecf8427e]
     |    +----my.cnf (441 B) [740258c758e3ef283d2fd5f862bc9e4c]
     |    +----pam.d (1.00 KB) [N/A]
     |    |    +----system-auth-ac (1.06 KB) [a59a64d1080fad83a54b709f18453a97]
     |    +----raddb (1.00 KB) [N/A]
     |    |    +----server (1.25 KB) [7f33f6bb81eba3e6f62dd3b395962792]
     |    +----rancid.conf (3.49 KB) [4e054d5070ca167cde85019122f52579]
     |    +----resolv.conf (84 B) [7be5a6817815c068819c6b2725079058]
     |    +----snmp (1.00 KB) [N/A]
     |    |    +----snmpd.conf (3.53 KB) [62e74a51397e77a6b6e0e9948a5111f0]
     |    +----ssh (1.00 KB) [N/A]
     |    |    +----ssh_config (1.78 KB) [acdb27cbe64eff80c6f04bb291e6d93a]
     |    |    +----sshd_config (3.24 KB) [8b5e771bdb96346dc54566fee82ae904]
     |    +----sysconfig (1.00 KB) [N/A]
     |    |    +----network (62 B) [1fc9e9eef1bac0832be5fb664bf0e191]
     |    |    +----network-scripts (1.00 KB) [N/A]
     |    |         +----ifcfg-bond0 (279 B) [cef68c3e20636e2cae644189a351c1b7]
     |    |         +----ifcfg-bond1 (219 B) [3606a8b04fb733f3f5cce6a56cd13434]
     |    |         +----ifcfg-eth0 (147 B) [73bf35db647e6e7d65f0584e966a0c03]
     |    |         +----ifcfg-eth1 (147 B) [6f422c593fe7c66a5173b3912bd31a39]
     |    |         +----ifcfg-eth2 (155 B) [c7f1e95a22a67190fc0195f5c5856414]
     |    |         +----ifcfg-eth3 (155 B) [052b2bfda2c5b3d5b613166b47d8c5d0]
     |    |         +----ifcfg-eth4 (108 B) [49c346d1828006ee562ec623d28ca472]
     |    |         +----ifcfg-eth5 (108 B) [383509c7903303cbbc6afef870c4159a]
     |    +----viewvc (1.00 KB) [N/A]
     |         +----viewvc.conf (36.07 KB) [9dedb6ff0cbdbc3779f48107da60bfaa]
     +----opt (1.00 KB) [N/A]
     |    +----opennms (1.00 KB) [N/A]
     |         +----etc (6.00 KB) [N/A]
     |         |    +----README.notification (9.45 KB) [d961758ec426c169e9a144367e14f856]
     |         |    +----ackd-configuration.xml (1.08 KB) [d04aacf6948864e2cef89c17235f263b]
     |         |    +----actiond-configuration.xml (129 B) [77fef338c0bd6fd1a3699e1d54926da6]
     |         |    +----ami-config.xml (116 B) [76f4704a682a3f32f7e507671b709833]
     |         |    +----asterisk-configuration.properties (2.00 KB) [a36b30736135499a89084a51b1543acd]
     |         |    +----availability-reports.xml (1.77 KB) [f1b9e3d31a05079fdd1dfb8c71d9d2e3]
     |         |    +----c3p0.properties (10.70 KB) [8955ad289eaa0dff8c4ab40737ca2cf2]
     |         |    +----capsd-configuration.xml (9.37 KB) [9f8816cbf07a9bafc472e37699de96bd]
     |         |    +----categories.xml (4.92 KB) [cc32c1ada3209b272ecb495c7ad995bb]
     |         |    +----chart-configuration.xml (7.22 KB) [c4164e4a1208e6cdcc2eecdefbc4c22e]
     |         |    +----collectd-configuration.xml (1.75 KB) [f1ae63ac39b7696c0b057f3ae5ce94fb]
     |         |    +----configured (0 B) [d41d8cd98f00b204e9800998ecf8427e]
     |         |    +----create.sql (98.40 KB) [91e0b8f9f26f3e3f3f32728db1d14729]
     |         |    +----database-reports.xml (1.31 KB) [c2da0fe42900725e74bafd791ecbd11c]
     |         |    +----database-schema.xml (4.79 KB) [1572364df16aaaad978fa8096ab7ac5b]
     |         |    +----datacollection-config.xml (257.12 KB) [a9b78c732059372e9223580a89189892]
     |         |    +----destinationPaths.xml (378 B) [be469adb2ecdb0b6dc980194e2040318]
     |         |    +----dhcpd-configuration.xml (187 B) [3a5ed11b912a924d20a37557aa54b195]
     |         |    +----discovery-configuration.xml (355 B) [590fadde2d886d1cd5279c195b6e3bfd]
     |         |    +----eventconf.xml (96.50 KB) [b5bd3decb222173e231cbe8ca1deeff2]
     |         |    +----eventd-configuration.xml (277 B) [1014f5140705f6637d7496d2fd8bc0c4]
     |         |    +----events-archiver-configuration.xml (93 B) [8343a94ed1f0f337379cc1c728a49996]
     |         |    +----events.archiver.properties (3.58 KB) [77d9441b2bf7839adb8712427cf8e74f]
     |         |    +----exclude-ueis.properties (10 B) [2bb393f0c80a07686e79434869ec329b]
     |         |    +----getManagePercentAvailIntfWindow.sql (3.07 KB) [5c4ecf669bed34aa8a681b54feba536f]
     |         |    +----getManagePercentAvailNodeWindow.sql (2.91 KB) [bff18e1de5f2b42165d18b2960075d15]
     |         |    +----getManagedOutageForIntfInWindow.sql (2.69 KB) [43d1c6c5fbffabec01bd11914e28cf89]
     |         |    +----getManagedOutageForNodeInWindow.sql (2.66 KB) [40650d80574d603e4e79fed6f42bb321]
     |         |    +----getManagedServiceCountForIntf.sql (2.06 KB) [28634d8162be4d43d07f97e274a375b5]
     |         |    +----getManagedServiceCountForNode.sql (2.16 KB) [359b84ff3a4b69afc252e16fd6d1d599]
     |         |    +----getOutageTimeInWindow.sql (4.18 KB) [37886fbfe635d3fe40b39f71aeb02927]
     |         |    +----getPercentAvailabilityInWindow.sql (2.73 KB) [189ae9f5c7b3f73119465a48ecddbbab]
     |         |    +----groups.xml (542 B) [a2b06815348d2a03a4ed9e67bcc779fb]
     |         |    +----http-datacollection-config.xml (1.14 KB) [1262079b7ff876aeab319f8c28e1c497]
     |         |    +----jasper-reports.xml (553 B) [a4617d8dc443d0657362fd9fa9388a36]
     |         |    +----java.conf (31 B) [1165cf7b335e8fb0b87e6edbcd417369]
     |         |    +----javamail-configuration.properties (1.33 KB) [5319370ccf1820ce79a8a8da1444a286]
     |         |    +----javamail-configuration.xml (3.25 KB) [771dba4c48f31b46ed3aa923aacb9e71]
     |         |    +----jcifs.properties (5.97 KB) [f2ddd42ca651e3ef272f40506a4f83d7]
     |         |    +----jdbc-datacollection-config.xml (935 B) [719a13c3a5361cffe560bc26c71030aa]
     |         |    +----jmx-datacollection-config.xml (10.17 KB) [6bd010587b515d1d946fa3ef5407e602]
     |         |    +----ksc-performance-reports.xml (51 B) [49e6835a261a6c92e3ba4299d170b145]
     |         |    +----libraries.properties (114 B) [4d43c3a47b3a0bf7ae88dd967cdc2925]
     |         |    +----linkd-configuration.xml (7.43 KB) [9992f744f9d016998c95ee1c49dc968b]
     |         |    +----log4j-controller.properties (1.35 KB) [6581396709b761bfe8d58a4bf0676ba5]
     |         |    +----log4j.properties (25.67 KB) [ad659767e4b49987d6956d888cad3131]
     |         |    +----magic-users.properties (2.45 KB) [6b78d67d63ce37d07d130c63d87d3b86]
     |         |    +----map.enable (547 B) [f9629c87895e8edec9ba4ebd8b6d3bb3]
     |         |    +----map.properties (18.31 KB) [cb11e9e3a6ff96e3d07ccde2263d7292]
     |         |    +----mapsadapter-configuration.xml (622 B) [1560bfbed6b4bf6e162a8a0d1714a6e5]
     |         |    +----microblog-configuration.xml (970 B) [79fdb546e6cda8803ab67681a1429294]
     |         |    +----model-importer.properties (778 B) [82dce1a2d47b79072944ebd71f71a018]
     |         |    +----modemConfig.properties (1.59 KB) [8049e6251aa22d8f7c138fb9f58fb941]
     |         |    +----monitoring-locations.xml (205 B) [099f64d51bec5bdc2399f9ddf7a66f1d]
     |         |    +----notifd-configuration.xml (2.20 KB) [9e2ac02bc672c9be29b7ee67a9e25a8b]
     |         |    +----notificationCommands.xml (7.27 KB) [450f15adc62f18b19a8035d9b43f0b8d]
     |         |    +----notifications.xml (5.76 KB) [9e9ac2e0fde37b16ea6a1f9980079c94]
     |         |    +----nsclient-config.xml (97 B) [83ca73d1aa0bd1bff8008effeb105e12]
     |         |    +----nsclient-datacollection-config.xml (5.96 KB) [011ab75520bff48c58d5bf00313ff8cb]
     |         |    +----opennms-datasources.xml (959 B) [63a742b7ff1583a137e774e5cf81d0b2]
     |         |    +----opennms-server.xml (72 B) [89271a4dac4b226872c72a37a93c1d1a]
     |         |    +----opennms.properties (18.45 KB) [d18e52e093de3d3ba37ddd39553025c0]
     |         |    +----otrs.properties (869 B) [edb20f8da5f996c3474f82a7fe4cf809]
     |         |    +----poll-outages.xml (44 B) [fa12a76a27b6322b0986f847260067f2]
     |         |    +----poller-config.properties (2.41 KB) [394f0dbff10abb1db2184eba29045783]
     |         |    +----poller-configuration.xml (14.14 KB) [1942c26201e551c9e5be1decbd7f03fd]
     |         |    +----provisiond-configuration.xml (1.12 KB) [598af2b79af7883f2b3cded666839b15]
     |         |    +----rancid-configuration.xml (451 B) [83bbb987b847ef4a3a91195775ac239a]
     |         |    +----reportd-configuration.xml (1.00 KB) [eec3eb8c6529698bfbc7f518c47080aa]
     |         |    +----response-adhoc-graph.properties (1.29 KB) [c7db71dc7459749304724c8ca2d25852]
     |         |    +----response-graph.properties (13.40 KB) [611f3f882eb43778bdfff4e2072cea4f]
     |         |    +----rrd-configuration.properties (8.45 KB) [dbaafad2c679dad38d838d116957a727]
     |         |    +----rt.properties (1.95 KB) [2da8210d4682b8a9e0c4e62dd131abd4]
     |         |    +----rtc-configuration.xml (232 B) [08942b0101c6d58d9c2295b0e13989d7]
     |         |    +----rws-configuration.xml (368 B) [c92589c0268bf21007324a5ad5f83976]
     |         |    +----rws-nbinventoryreport.xsl (17.93 KB) [2dae10e5a0780d70120cce293df26332]
     |         |    +----rws-rancidlistreport.xsl (16.23 KB) [6efb19b8f6822233fcbd18b0886f7d8d]
     |         |    +----scriptd-configuration.xml (161 B) [a8c4a9a17abef1986a150bf2cb5db8c1]
     |         |    +----service-configuration.xml (14.01 KB) [73e61c2867d1408c45d25f3832e3e730]
     |         |    +----setIfServiceKeysOnInsertTrigger.sql (2.02 KB) [30615a1d2112083bf4bc5213e89162e8]
     |         |    +----setIfServiceKeysOnUpdateTrigger.sql (2.04 KB) [a2c9aa349a3e75ce694dbe8ce9a0b27e]
     |         |    +----setIpInterfaceKeysOnInsertTrigger.sql (2.42 KB) [59ecb0023a4be4515c28c99f43c2279a]
     |         |    +----setIpInterfaceKeysOnUpdateTrigger.sql (2.49 KB) [e6a0b8f299a87c4f3bde34ae1323190f]
     |         |    +----setSnmpInterfaceKeysOnInsertTrigger.sql (2.06 KB) [770bbbbdcc44dbfb7268d415afe89aed]
     |         |    +----setSnmpInterfaceKeysOnUpdateTrigger.sql (2.57 KB) [b24be98fa73081ceab289603796006bb]
     |         |    +----site-status-views.xml (757 B) [a51b936985c185b623b80758fa862cf2]
     |         |    +----smsPhonebook.properties (23 B) [940532d790468931ee2dd15a5ea5ac3e]
     |         |    +----snmp-adhoc-graph.properties (1.29 KB) [c7db71dc7459749304724c8ca2d25852]
     |         |    +----snmp-asset-adapter-configuration.xml (3.17 KB) [3837395feac663dcb20473fed93c5459]
     |         |    +----snmp-config.xml (102 B) [b8d294bb1c7a211a618804b8036ce703]
     |         |    +----snmp-graph.properties (540.77 KB) [9ac522efb3810e4da2b5e13cbb18b3a7]
     |         |    +----snmp-interface-poller-configuration.xml (910 B) [c8092185fef01dbd5415608c5823118c]
     |         |    +----statsd-configuration.xml (974 B) [fa184ae6350cca094b197bc2105661da]
     |         |    +----surveillance-views.xml (1.08 KB) [673a350238d28b239781434f1d69e205]
     |         |    +----syslogd-configuration.xml (3.17 KB) [ce87dd5db9e459d9c152fbaabd0da4d3]
     |         |    +----threshd-configuration.xml (2.27 KB) [fa59ec13bce8ff26fd96a8a09ab50ddc]
     |         |    +----thresholds.xml (4.33 KB) [70a61e9d47e2315a876c55f2adbc9ee0]
     |         |    +----tl1d-configuration.xml (296 B) [d05b16f0ef21e719cefb1cddaf840566]
     |         |    +----translator-configuration.xml (6.22 KB) [47a5dd8fee81dc1e3f96b21a6deaa9e8]
     |         |    +----trapd-configuration.xml (93 B) [890944dc260dee8b64934459d2e0e2bc]
     |         |    +----users.xml (508 B) [c3e72d4573b1a2d2c27c01a86942f2ee]
     |         |    +----vacuumd-configuration.xml (16.72 KB) [0e03fd1c1fc54d52ae04d2421b258f5b]
     |         |    +----viewsdisplay.xml (717 B) [385609ca9f39a44fc3dcd8829a22f4ff]
     |         |    +----vulnscand-configuration.xml (22.36 KB) [05f5d32285314d559ff6f2bd740cc389]
     |         |    +----wmi-config.xml (141 B) [5ba3737880a0868f6e67e6d8955cf352]
     |         |    +----wmi-datacollection-config.xml (15.26 KB) [32b522b7548b28bb107cf2e41362aef1]
     |         |    +----xmlrpcd-configuration.xml (1.09 KB) [45965919af4be5daddd155754922a88b]
     |         |    +----xmp-config.xml (112 B) [d99d19668cb1ffa9f569f32c30588dfa]
     |         |    +----xmp-datacollection-config.xml (4.81 KB) [5f70297201bbbb5879deab6a15621a0b]
     |         |    +----xmpp-configuration.properties (1.32 KB) [482c0980b88183b22e8f73ab83ce87c3]
     |         +----jetty-webapps (1.00 KB) [N/A]
     |         |    +----opennms (1.00 KB) [N/A]
     |         |         +----report (1.00 KB) [N/A]
     |         |              +----index.jsp (5.45 KB) [870901499b24cc5e4d0f23fed1aff3f7]
     |         +----share (1.00 KB) [N/A]
     |              +----rrd (1.00 KB) [N/A]
     |                   +----.readme (32 B) [fad1fe82dae55158df2298634d084acc]
     +----tmp (1.00 KB) [N/A]
     |    +----rpms.txt (642.32 KB) [11ba7c93c9569e1d674873f37edd3e5c]
     +----var (1.00 KB) [N/A]
          +----lib (1.00 KB) [N/A]
          |    +----pgsql (1.00 KB) [N/A]
          |         +----backups (1.00 KB) [N/A]
          |         |    +----pgsql_02_18_2011.pgsql (63.88 MB) [91d6ca91f81236d0b12fd038df4b952e]
          |         |    +----pgsql_globals_02_18_2011.pgsql (611 B) [9bb559d0a627e71f8732c3ce7fe96c5e]
          |         +----data (1.00 KB) [N/A]
          |              +----pg_hba.conf (3.50 KB) [82457afdd6000213c2031dca8b831483]
          |              +----pg_ident.conf (1.59 KB) [3b96a52aed8884a160d7855d0c080c20]
          |              +----postgresql.conf (16.47 KB) [28c4f9175583597b6e1b597ae9f2cc32]
          +----net-snmp (1.00 KB) [N/A]
          |    +----snmpd.conf (1.43 KB) [8b7c9404702e27d9441da3971121e1db]
          +----rancid (1.00 KB) [N/A]
               +----.cloginrc (116 B) [9be1ede6b67129c25895ec982b2aa7bb]
               +----.cloginrc.orig (3.81 KB) [ef71ba8013e43e0ed132d5154f1f5372]
               +----CVS (1.00 KB) [N/A]
               |    +----CVSROOT (1.00 KB) [N/A]
               |    |    +----.#checkoutlist (495 B) [f85619badf0a9fba530b21abe00fd074]
               |    |    +----.#commitinfo (760 B) [7f78d59a5fe160f21ee9fd4045c5fb01]
               |    |    +----.#config (993 B) [417a2179cf0f47996b4991ff77c55eb0]
               |    |    +----.#cvswrappers (602 B) [9ac6b67a97238a90a652ca876ea542fa]
               |    |    +----.#editinfo (1.00 KB) [c0245bd6cbece787af06cb3e1969ea78]
               |    |    +----.#loginfo (1.14 KB) [5cbcdf2f6b2163317f1cc9d15b093436]
               |    |    +----.#modules (1.12 KB) [eaadfb1d78e7d7637edd0e00b819fbda]
               |    |    +----.#notify (564 B) [7d0ebc027379219ea791b09d64ca848b]
               |    |    +----.#rcsinfo (649 B) [4ca72032cff35f7d200d0778ba93ab65]
               |    |    +----.#taginfo (879 B) [6c6d903782cf3195ba68085654c8c5f3]
               |    |    +----.#verifymsg (1.00 KB) [0ad9fcdfc8f7bc24ba445a785fdd1bfc]
               |    |    +----checkoutlist (495 B) [f85619badf0a9fba530b21abe00fd074]
               |    |    +----checkoutlist,v (697 B) [d8670622e72d4de07077002318046c6a]
               |    |    +----commitinfo (760 B) [7f78d59a5fe160f21ee9fd4045c5fb01]
               |    |    +----commitinfo,v (962 B) [a6794b831a7bcf4144c3ff53b98cee56]
               |    |    +----config (993 B) [417a2179cf0f47996b4991ff77c55eb0]
               |    |    +----config,v (1.17 KB) [1770960d727fe9bf58cc13a480de1ff1]
               |    |    +----cvswrappers (602 B) [9ac6b67a97238a90a652ca876ea542fa]
               |    |    +----cvswrappers,v (804 B) [ffe1753f005d50a96bcd67d2c9971455]
               |    |    +----editinfo (1.00 KB) [c0245bd6cbece787af06cb3e1969ea78]
               |    |    +----editinfo,v (1.20 KB) [de43cb3eb3c6ff658d36a8a53c08a4b5]
               |    |    +----history (95 B) [db2c5582832f24fe5b61170f4c645a58]
               |    |    +----loginfo (1.14 KB) [5cbcdf2f6b2163317f1cc9d15b093436]
               |    |    +----loginfo,v (1.34 KB) [34731aabe679120fc49b2bf230fc162e]
               |    |    +----modules (1.12 KB) [eaadfb1d78e7d7637edd0e00b819fbda]
               |    |    +----modules,v (1.32 KB) [abe49eb8a07e0397ca33a74307b4ad7d]
               |    |    +----notify (564 B) [7d0ebc027379219ea791b09d64ca848b]
               |    |    +----notify,v (766 B) [611bf7cf6354f4390f6684e566879d43]
               |    |    +----rcsinfo (649 B) [4ca72032cff35f7d200d0778ba93ab65]
               |    |    +----rcsinfo,v (851 B) [e0a6a572618e65e63ae0a34b9955d14e]
               |    |    +----taginfo (879 B) [6c6d903782cf3195ba68085654c8c5f3]
               |    |    +----taginfo,v (1.06 KB) [20df7f27cdde886aee92f5893ae9ec6e]
               |    |    +----val-tags (0 B) [d41d8cd98f00b204e9800998ecf8427e]
               |    |    +----verifymsg (1.00 KB) [0ad9fcdfc8f7bc24ba445a785fdd1bfc]
               |    |    +----verifymsg,v (1.20 KB) [78ece9641d71450306ce2e1c702c9d6b]
               |    +----Entries.Log (20 B) [4f14cc3d785f0a5a449d1de35e0adc6c]
               |    +----GW1_Network (1.00 KB) [N/A]
               |         +----router.db,v (166 B) [c45e45e4cbd56481c0b7d8486648e80a]
               +----GW1_Network (1.00 KB) [N/A]
               |    +----.cvsignore (47 B) [90e6cc3a9eeff8d4217cf3fd20d222fb]
               |    +----CVS (1.00 KB) [N/A]
               |    |    +----Entries (56 B) [fc5dfec76adf047d95890f9cad4ad96a]
               |    |    +----Repository (12 B) [d0dae395b1afeaf0b3ddd7929bc2ec31]
               |    |    +----Root (16 B) [c96b9db0b52cea4b9fd04712d60c25f1]
               |    +----configs (1.00 KB) [N/A]
               |    |    +----CVS (1.00 KB) [N/A]
               |    |         +----Entries (2 B) [57b8d745384127342f95660d97e1c9c2]
               |    |         +----Repository (20 B) [3588ae0e50ce50bc63bde94c3cef70fe]
               |    |         +----Root (16 B) [c96b9db0b52cea4b9fd04712d60c25f1]
               |    +----router.db (0 B) [d41d8cd98f00b204e9800998ecf8427e]
               |    +----routers.all (0 B) [d41d8cd98f00b204e9800998ecf8427e]
               |    +----routers.down (0 B) [d41d8cd98f00b204e9800998ecf8427e]
               |    +----routers.up (0 B) [d41d8cd98f00b204e9800998ecf8427e]
               +----logs (97.00 KB) [N/A]

January 27, 2008

nixlogin_exp

Filed under: bigip, expect, f5, linux, nix, rancid — Tags: , , , , , — lancevermilion @ 11:52 pm

In order to get this to work you will need to add something like this to your .cloginrc

add userprompt linuxsvr* sshOnlyNoPrompt
add autoenable linuxsvr* 1
add user linuxsvr*

#! /usr/bin/expect --
##
## $Id: clogin.in,v 1.79 2004/05/27 21:57:52 heas Exp $
##
## Copyright (C) 1997-2004 by Terrapin Communications, Inc.
## All rights reserved.
##
## This software may be freely copied, modified and redistributed
## without fee for non-commerical purposes provided that this license
## remains intact and unmodified with any RANCID distribution.
##
## There is no warranty or other guarantee of fitness of this software.
## It is provided solely "as is".  The author(s) disclaim(s) all
## responsibility and liability with respect to this software's usage
## or its effect upon hardware, computer systems, other software, or
## anything else.
##
## Except where noted otherwise, rancid was written by and is maintained by
## Henry Kilmer, John Heasley, Andrew Partan, Pete Whiting, and Austin Schutz.
##
#
# The login expect scripts were based on Erik Sherk's gwtn, by permission.
#
# clogin - Cisco login
#
# Most options are intuitive for logging into a Cisco router.
# The default is to enable (thus -noenable).  Some folks have
# setup tacacs to have a user login at priv-lvl = 15 (enabled)
# so the -autoenable flag was added for this case (don't go through
# the process of enabling and the prompt will be the "#" prompt.
# The default username password is the same as the vty password.
#

# Usage line
set usage "Usage: $argv0 \[-autoenable\] \[-noenable\] \[-c command\] \
\[-Evar=x\] \[-e enable-password\] \[-f cloginrc-file\] \[-p user-password\] \
\[-s script-file\] \[-t timeout\] \[-u username\] \
\[-v vty-password\] \[-w enable-username\] \[-x command-file\] \
\[-y ssh_cypher_type\] router \[router...\]\n"

# env(CLOGIN) may contain:
#	x == do not set xterm banner or name

# Password file
set password_file $env(HOME)/.cloginrc
# Default is to login to the router
set do_command 0
set do_script 0
# The default is to automatically enable
set avenable 1
# The default is that you login non-enabled (tacacs can have you login already
# enabled)
set avautoenable 0
# The default is to look in the password file to find the passwords.  This
# tracks if we receive them on the command line.
set do_passwd 1
set do_enapasswd 1
# attempt at platform switching.
set platform ""

# Find the user in the ENV, or use the unix userid.
if {[ info exists env(CISCO_USER) ] } {
    set default_user $env(CISCO_USER)
} elseif {[ info exists env(USER) ]} {
    set default_user $env(USER)
} elseif {[ info exists env(LOGNAME) ]} {
    set default_user $env(LOGNAME)
} else {
    # This uses "id" which I think is portable.  At least it has existed
    # (without options) on all machines/OSes I've been on recently -
    # unlike whoami or id -nu.
    if [ catch {exec id} reason ] {
	send_error "\nError: could not exec id: $reason\n"
	exit 1
    }
    regexp {\(([^)]*)} "$reason" junk default_user
}

# Sometimes routers take awhile to answer (the default is 10 sec)
set timeout 45

# Process the command line
for {set i 0} {$i  (enable) " }
	-re "(denied|Sorry|Incorrect)"	{
			  # % Access denied - from local auth and poss. others
			  send_user "\nError: Check your Enable passwd\n";
			  return 1
			}
	"% Error in authentication" {
			  send_user "\nError: Check your Enable passwd\n"
			  return 1
			}
	"% Bad passwords" {
			  send_user "\nError: Check your Enable passwd\n"
			  return 1
			}
    }
    # We set the prompt variable (above) so script files don't need
    # to know what it is.
    set in_proc 0
    return 0
    puts "past expect enable prompt"
}

# Run commands given on the command line.
proc run_commands { prompt command } {
    global in_proc platform
    set in_proc 1

    # If the prompt is (enable), then we are on a switch and the
    # command is "set length 0"; otherwise its "term length 0".
    # skip if its an extreme (since the pager can not be disabled on a
    # per-vty basis).
    if { [ string compare "extreme" "$platform" ] } {
	if [ regexp -- ".*> .*enable" "$prompt" ] {
	    send "\r"
	    #send "set length 0\r"
	    # This is ugly, but reduces code duplication, allowing the
	    # subsequent expects to handle everything as normal.
	    set command "set logging session disable;$command"
	} else {
	    #send "term length 0\r"
	    send "\r"
	}
	# escape any parens in the prompt, such as "(enable)"
	regsub -all {[)(]} $prompt {\\&} reprompt
	# match cisco config mode prompts too, such as router(config-if)#,
	# but catalyst does not change in this fashion.
	regsub -all {^(.{1,14}).*([#>])$} $reprompt {\1([^#>\r\n]+)?[#>](\\([^)\\r\\n]+\\))?} reprompt
	expect {
	    -re $reprompt	{}
	    -re "\[\n\r]+"	{ exp_continue }
	}
    } else {
	regsub -all "\[)(]" $prompt {\\&} reprompt
    }

    # this is the only way i see to get rid of more prompts in o/p..grrrrr
    log_user 0
    # Is this a multi-command?
    if [ string match "*\;*" "$command" ] {
	set commands [split $command \;]
	set num_commands [llength $commands]
	# the pager can not be turned off on the PIX, so we have to look
	# for the "More" prompt.  the extreme is equally obnoxious, with a
	# global switch in the config.
	for {set i 0} {$i < $num_commands} { incr i} {
	    send "[subst -nocommands [lindex $commands $i]]\r"
	    expect {
		-re "\b+"			{ exp_continue }
		-re "^\[^\n\r *]*$reprompt"	{ send_user -- "$expect_out(buffer)"
						}
		-re "^\[^\n\r]*$reprompt."	{ send_user -- "$expect_out(buffer)"
						  exp_continue }
		-re "\[\n\r]+"			{ send_user -- "$expect_out(buffer)"
						  exp_continue }
		-re "\[^\r\n]*Press  to cont\[^\r\n]*"	{
						  send " "
						  # bloody ^[[2K after " "
						  expect {
							-re "^\[^\r\n]*\r" {}
							}
						  exp_continue
						}
		-re "^ *--More--\[^\n\r]*"	{
						  send " "
						  exp_continue }
		-re "^\[^\n\r]*"	{
						  send_user -- "$expect_out(buffer)"
						  send " "
						  exp_continue }
	    }
	}
    } else {
	# the pager can not be turned off on the PIX, so we have to look
	# for the "More" prompt.  the extreme is equally obnoxious, with a
	# global switch in the config.
	send "[subst -nocommands $command]\r"
	expect {
		-re "\b+"			{ exp_continue }
		-re "^\[^\n\r *]*$reprompt"	{ send_user -- "$expect_out(buffer)"
						}
		-re "^\[^\n\r]*$reprompt."	{ send_user -- "$expect_out(buffer)"
						  exp_continue }
		-re "\[\n\r]+"			{ send_user -- "$expect_out(buffer)"
						  exp_continue }
		-re "\[^\r\n]*Press  to cont\[^\r\n]*"	{
						  send " "
						  # bloody ^[[2K after " "
						  expect {
							-re "^\[^\r\n]*\r" {}
							}
						  exp_continue
						}
		-re "^ *--More--\[^\n\r]*"	{
						  send " "
						  exp_continue }
		-re "^\[^\n\r]*"	{
						  send_user -- "$expect_out(buffer)"
						  send " "
						  exp_continue }
	}
    }
    log_user 1

    if { [ string compare "extreme" "$platform" ] } {
	send "\rexit\r"
    } else {
	send "\rquit\r"
    }
    expect {
	-re "^\[^\n\r *]*$reprompt"		{
						  # the Cisco CE and Jnx ERX
						  # return to non-enabled mode
						  # on exit in enabled mode.
						  send "\rexit\r"
						  exp_continue;
						}
	"Do you wish to save your configuration changes" {
						  send "n\r"
						  exp_continue
						}
	-re "\[\n\r]+"				{ exp_continue }
	timeout					{ return 0 }
	eof					{ return 0 }
    }
    set in_proc 0
}

#
# For each router... (this is main loop)
#
source_password_file $password_file
set in_proc 0
foreach router [lrange $argv $i end] {
    set router [string tolower $router]
    send_user "$router\n"

    # Figure out prompt.
    # Since autoenable is off by default, if we have it defined, it
    # was done on the command line. If it is not specifically set on the
    # command line, check the password file.
    if $avautoenable {
	set autoenable 1
	set enable 0
	set prompt "(#| \\(enable\\))"
    } else {
	set ae [find autoenable $router]
	if { "$ae" == "1" } {
	    set autoenable 1
	    set enable 0
	    set prompt "(#| \\(enable\\))"
	} else {
	    set autoenable 0
	    set enable $avenable
	    set prompt "(\\$|\\>|\\:)"
	    #set prompt ">"
            puts "prompt: $prompt"
	}
    }

    # look for noenable option in .cloginrc
    if { [find noenable $router] != "" } {
	set enable 0
    }

    # Figure out passwords
    if { $do_passwd || $do_enapasswd } {
      set pswd [find password $router]
      if { [llength $pswd] == 0 } {
	send_user "\nError: no password for $router in $password_file.\n"
	continue
      }
      if { $enable && $do_enapasswd && $autoenable == 0 && [llength $pswd]  \\\(enable\\\)"	{
				  set junk $expect_out(0,string);
				  regsub -all "\[\]\[]" $junk {\\&} prompt;
				}
    }

    if { $do_command } {
	if {[run_commands $prompt $command]} {
	    continue
	}
    } elseif { $do_script } {
	# If the prompt is (enable), then we are on a switch and the
	# command is "set length 0"; otherwise its "term length 0".
	if [ regexp -- ".*> .*enable" "$prompt" ] {
	    #send "set length 0\r"
	    send "\r"
	    send "set logging session disable\r"
	} else {
	    #send "term length 0\r"
	    send "\r"
	}
	expect -re $prompt	{}
	source $sfile
	close
    } else {
	label $router
	log_user 1
	interact
    }

    # End of for each router
    wait
    sleep 0.3
}
exit 0

January 26, 2008

nixlogin_pl

Filed under: f5, linux, nix, perl, rancid — Tags: , , , , — lancevermilion @ 7:26 am
#! /usr/bin/perl
##
## $Id: nixrancid.in,v 1.4 2008/01/21 10:43:25 lvermilion Exp $
##
#
# TODO
# * Add to personal rancid distribution.
# * OS Detection (maybe use variable to support user defined OS)
# * Collection of more system critical files
# * Fix netstat to not include Use/Expire fields on BSD
#
# Changelog
#
# v1.4  lvermilion 1-21-08
# Added support so the original F5 script works on Linux/BSD/Unix
# Added support for the command "sudo su root"
# Added BSDConfFile, since these files are BSD specific
# and we don't want them ran unless we are on a BSD system
# Added a variety of linux/BSD commands
# Removed F5 commands at this point until OS detection is completed.
#
# v1.3 lvermilion
# Add banner formatting so it is more clear what command was ran
# Add support for more F5 commands (profile/monitor/regkey/license/etc).
#
# v1.2  mashcraft 9-18-07
# Fixed invalid TERM causing intermitent failures
# Works as non-root user
# Added Platform info and static routes
#
# v1.1  mashcraft 8-2-07
# Debugging problems with HF8.
#
#
## Copyright (C) 1997-2004 by Terrapin Communications, Inc.
## All rights reserved.
##
## Copyright (C) 2007 by Omniture, Inc. Ver 1.1-1.2
## All rights reserved.
##
## Copyright (C) 2008 by Gheek.net Ver 1.3-1.4
## All rights reserved.
##
## This software may be freely copied, modified and redistributed
## without fee for non-commerical purposes provided that this license
## remains intact and unmodified with any RANCID distribution.
##
## There is no warranty or other guarantee of fitness of this software.
## It is provided solely "as is".  The author(s) disclaim(s) all
## responsibility and liability with respect to this software's usage
## or its effect upon hardware, computer systems, other software, or
## anything else.
##
## Except where noted otherwise, rancid was written by and is maintained by
## Henry Kilmer, John Heasley, Andrew Partan, Pete Whiting, and Austin Schutz.
##
#
# Based on rancid, this script handles F5 BigIP/(Li|U)nix,BSD systems.
# Modifications by Mike Ashcraft and Lance Vermilion.
#
# This script has been tested with Big-IP Version 9.1.2, 9.2.x and 9.3.1
#
# This script uses nixlogin.  You will need to configure the
# host in .cloginrc as follows:
#
# add user hostname		username
# add userprompt hostname	sshOnlyNoPrompt # The default prompt regex matches the last login message and causes problems
# add method hostname		ssh
# add password hostname		password
# add autoenable hostname	1
#
#  RANCID - Really Awesome New Cisco confIg Differ
#
# usage: nixrancid [-d] [-l] [-f filename | $host]
#
use Getopt::Std;
getopts('dfl');
$log = $opt_l;
$debug = $opt_d;
$file = $opt_f;
$host = $ARGV[0];
$clean_run = 0;
$found_end = 0;
$found_version = 0;
$found_env = 0;
$found_diag = 0;
$timeo = 90;				# clogin timeout in seconds

my(@commandtable, %commands, @commands);# command lists
my(%filter_pwds);			# password filtering mode

# This routine is used to print out the router configuration
sub ProcessHistory {
    my($new_hist_tag,$new_command,$command_string,@string) = (@_);
    if ((($new_hist_tag ne $hist_tag) || ($new_command ne $command))
	&& defined %history) {
	print eval "$command \%history";
	undef %history;
    }
    if (($new_hist_tag) && ($new_command) && ($command_string)) {
	if ($history{$command_string}) {
	    $history{$command_string} = "$history{$command_string}@string";
	} else {
	    $history{$command_string} = "@string";
	}
    } elsif (($new_hist_tag) && ($new_command)) {
	$history{++$#history} = "@string";
    } else {
	print "@string";
    }
    $hist_tag = $new_hist_tag;
    $command = $new_command;
    1;
}

sub numerically { $a  $b; }

# This is a sort routine that will sort numerically on the
# keys of a hash as if it were a normal array.
sub keynsort {
    local(%lines) = @_;
    local($i) = 0;
    local(@sorted_lines);
    foreach $key (sort numerically keys(%lines)) {
	$sorted_lines[$i] = $lines{$key};
	$i++;
    }
    @sorted_lines;
}

# This is a sort routine that will sort on the
# keys of a hash as if it were a normal array.
sub keysort {
    local(%lines) = @_;
    local($i) = 0;
    local(@sorted_lines);
    foreach $key (sort keys(%lines)) {
	$sorted_lines[$i] = $lines{$key};
	$i++;
    }
    @sorted_lines;
}

# This is a sort routine that will sort on the
# values of a hash as if it were a normal array.
sub valsort{
    local(%lines) = @_;
    local($i) = 0;
    local(@sorted_lines);
    foreach $key (sort values %lines) {
	$sorted_lines[$i] = $key;
	$i++;
    }
    @sorted_lines;
}

# This is a numerical sort routine (ascending).
sub numsort {
    local(%lines) = @_;
    local($i) = 0;
    local(@sorted_lines);
    foreach $num (sort {$a  $b} keys %lines) {
	$sorted_lines[$i] = $lines{$num};
	$i++;
    }
    @sorted_lines;
}

# This is a sort routine that will sort on the
# ip address when the ip address is anywhere in
# the strings.
sub ipsort {
    local(%lines) = @_;
    local($i) = 0;
    local(@sorted_lines);
    foreach $addr (sort sortbyipaddr keys %lines) {
	$sorted_lines[$i] = $lines{$addr};
	$i++;
    }
    @sorted_lines;
}

# These two routines will sort based upon IP addresses
sub ipaddrval {
    my(@a) = ($_[0] =~ m#^(\d+)\.(\d+)\.(\d+)\.(\d+)$#);
    $a[3] + 256 * ($a[2] + 256 * ($a[1] +256 * $a[0]));
}
sub sortbyipaddr {
    &ipaddrval($a)  &ipaddrval($b);
}

# sub Platform  31-Jan-2007  Mike Ashcraft mashcraft@omniture.com
# This routine parses "cat filename"
sub Platform {
    print STDERR "    In Platform: $_" if ($debug);

    ProcessHistory("COMMENTS","","BO","\n!+----------------------------------------------+\n!|\n!| ####  Running $cmd\n!|\n!+----------------------------------------------+\n");

    while () {
        tr/15//d;
        last if (/^$prompt/);
        next if (/degC/);
        ProcessHistory("","","$cmd","$_");
    }
    $found_end = 1;
    return(0);
}

# sub BSDConfFile  21-Jan-2008  Lance Vermilion lvermilion@gheek.net
# This routine parses "cat filename"
sub BSDConfFile {
    print STDERR "    In BSDConfFile: $_" if ($debug);
        $uname=`uname`;

    ProcessHistory("COMMENTS","","BO","\n!+----------------------------------------------+\n!|\n!| ####  Running $cmd\n!|\n!+----------------------------------------------+\n") if ( $uname !~ /BSD/i );

    while () {
        last if ( $uname !~ /BSD/i );
        tr/15//d;
        last if (/^$prompt/);
        ProcessHistory("","","$cmd","$_") if ( $uname =~ /BSD/i );
    }
    $found_end = 1;
    return(0);
}

# sub ConfFile  31-Jan-2007  Mike Ashcraft mashcraft@omniture.com
# This routine parses "cat filename"
sub ConfFile {
    print STDERR "    In ConfFile: $_" if ($debug);

    ProcessHistory("COMMENTS","","BO","!+----------------------------------------------+\n!|\n!| ####  Running $cmd\n!|\n!+----------------------------------------------+\n");

    while () {
        tr/15//d;
        last if (/^$prompt/);
        if ( $cmd =~ /netstat/ )
        {
          next if /.*UHLW.*/
        }
        next if ($cmd =~ /config sync show/ && !/^\s+Status/);
        ProcessHistory("","","$cmd","$_");
    }
    $found_end = 1;
    return(0);
}

# sub DirList  31-Jan-2007  Mike Ashcraft mashcraft@omniture.com
# This routine parses directory listings.
#
# Disable color and make sure the date format is consistent
# For example: ll --time-style=long-iso --color=never
#
sub DirList {
    print STDERR "    In ConfFile: $_" if ($debug);

    ProcessHistory("COMMENTS","","BO","!+----------------------------------------------+\n!|\n!| ####  Running $cmd\n!|\n!+----------------------------------------------+\n");

    while () {
        tr/15//d;
        last if (/^$prompt/);
        ProcessHistory("","","$cmd","$_");
    }
    $found_end = 1;
    return(0);
}

# sub NoOutput  18-Sep-2007  Mike Ashcraft mashcraft@omniture.com
# This routine handles commands where you do not want any output
#
#
sub NoOutput {
    print STDERR "    In ConfFile: $_" if ($debug);

    while () {
        tr/15//d;
        last if (/^$prompt/);
    }
    $found_end = 1;
    return(0);
}

# dummy function
sub DoNothing {print STDOUT;}

# Main
@commandtable = (
	{'TERM=xterm'			=> 'NoOutput'},
	{'export TERM'			=> 'NoOutput'},
	{'uname -a'			=> 'Platform'},
	{'netstat -rn'			=> 'ConfFile'},
	{'cat /etc/passwd'		=> 'ConfFile'},
	{'cat /etc/group'		=> 'ConfFile'},
	{'cat /etc/hosts'		=> 'ConfFile'},
	{'cat /etc/resolv.conf'		=> 'ConfFile'},
	{'cat /etc/my.cnf'		=> 'ConfFile'},
	{'cat /etc/drbd.conf'		=> 'ConfFile'},
	{'cat /etc/fstab'		=> 'ConfFile'},
	{'cat /etc/rc.conf'		=> 'ConfFile'},
	{'cat /etc/iscsi/iscsid.conf' 	=> 'ConfFile'},
	{'cat /etc/modprobe.conf'	=> 'ConfFile'},
	{'cat /etc/sysconfig/network-scripts/ifcfg-bond0'		=> 'ConfFile'},
	{'cat /etc/sysconfig/network-scripts/ifcfg-bond1'		=> 'ConfFile'},
	{'cat /etc/sysconfig/network-scripts/ifcfg-eth0'		=> 'ConfFile'},
	{'cat /etc/sysconfig/network-scripts/ifcfg-eth1'		=> 'ConfFile'},
	{'cat /etc/sysconfig/network-scripts/ifcfg-eth2'		=> 'ConfFile'},
	{'cat /etc/sysconfig/network-scripts/ifcfg-eth3'		=> 'ConfFile'},
	{'cat /etc/sysconfig/network-scripts/ifcfg-eth4'		=> 'ConfFile'},
	{'cat /etc/sysconfig/network-scripts/ifcfg-eth5'		=> 'ConfFile'},
	{'alternatives --config java' 	=> 'ConfFile'},
	{'/sbin/chkconfig --list' 	=> 'ConfFile'},
	{'/usr/bin/yum list installed' 	=> 'ConfFile'},
#        {'bigpipe platform'             => 'Platform'},
#        {'bigpipe version'              => 'ConfFile'},
#        {'bigpipe list'                 => 'ConfFile'},
#        {'bigpipe base list'            => 'ConfFile'},
#        {'bigpipe profile list'         => 'ConfFile'},
#        {'bigpipe monitor list'         => 'ConfFile'},
#        {'bigpipe config sync show'             => 'ConfFile'},
#        {'cat /config/RegKey.license'           => 'ConfFile'},
#        {'bigpipe route static show'            => 'ConfFile'},
#        {'ls --full-time --color=never /config/ssl/ssl.crt'     => 'DirList'},
#        {'ls --full-time --color=never /config/ssl/ssl.key'     => 'DirList'},

);
# Use an array to preserve the order of the commands and a hash for mapping
# commands to the subroutine and track commands that have been completed.
@commands = map(keys(%$_), @commandtable);
%commands = map(%$_, @commandtable);

$cisco_cmds=join(";",@commands);
$cmds_regexp=join("|",@commands);

open(OUTPUT,">$host.new") || die "Can't open $host.new for writing: $!\n";
select(OUTPUT);
# make OUTPUT unbuffered if debugging
if ($debug) { $| = 1; }

if ($file) {
    print STDERR "opening file $host\n" if ($debug);
    print STDOUT "opening file $host\n" if ($log);
    open(INPUT,"<$host") || die "open failed for $host: $!\n";
} else {
    print STDERR "executing nixlogin -t $timeo -c\"$cisco_cmds\" $host\n" if ($debug);
    print STDOUT "executing nixlogin -t $timeo -c\"$cisco_cmds\" $host\n" if ($log);
    if (defined($ENV{NOPIPE})) {
	system "nixlogin -t $timeo -c \"$cisco_cmds\" $host  $host.raw 2>&1" || die "nixlogin failed for $host: $!\n";
	open(INPUT, "< $host.raw") || die "nixlogin failed for $host: $!\n";
    } else {
	open(INPUT,"nixlogin -t $timeo -c \"$cisco_cmds\" $host </dev/null |") || die "nixlogin failed for $host: $!\n";
    }
}

# determine password filtering mode
if ($ENV{"FILTER_PWDS"} =~ /no/i) {
    $filter_pwds = 0;
} elsif ($ENV{"FILTER_PWDS"} =~ /all/i) {
    $filter_pwds = 2;
} else {
    $filter_pwds = 1;
}

ProcessHistory("","","","!RANCID-CONTENT-TYPE: nix\n!\n");
ProcessHistory("COMMENTS","keysort","B0","!\n");
ProcessHistory("COMMENTS","keysort","D0","!\n");
ProcessHistory("COMMENTS","keysort","F0","!\n");
ProcessHistory("COMMENTS","keysort","G0","!\n");
TOP: while() {
    tr/15//d;
    if (/\s?logout$/) {
	$clean_run=1;
	last;
    }
    if (/^Error:/) {
	print STDOUT ("$host nixlogin error: $_");
	print STDERR ("$host nixlogin error: $_") if ($debug);
	$clean_run=0;
	last;
    }
    while (/#\s*($cmds_regexp)\s*$/) {
	$cmd = $1;
	if (!defined($prompt)) {
	    $prompt = ($_ =~ /^([^#]+#)/)[0];
	    $prompt =~ s/([][}{)(\\])/\\$1/g;
	    print STDERR ("PROMPT MATCH: $prompt\n") if ($debug);
	}
	print STDERR ("HIT COMMAND:$_") if ($debug);
	if (! defined($commands{$cmd})) {
	    print STDERR "$host: found unexpected command - \"$cmd\"\n";
	    $clean_run = 0;
	    last TOP;
	}
	$rval = };
	delete($commands{$cmd});
	if ($rval == -1) {
	    $clean_run = 0;
	    last TOP;
	}
    }
}
print STDOUT "Done $logincmd: $_\n" if ($log);
# Flush History
ProcessHistory("","","","");
# Cleanup
close(INPUT);
close(OUTPUT);

if (defined($ENV{NOPIPE})) {
    unlink("$host.raw") if (! $debug);
}

# check for completeness
if (scalar(%commands) || !$clean_run || !$found_end) {
    if (scalar(%commands)) {
	printf(STDOUT "$host: missed cmd(s): %s\n", join(',', keys(%commands)));
	printf(STDERR "$host: missed cmd(s): %s\n", join(',', keys(%commands))) if ($debug);
    }
    if (!$clean_run || !$found_end) {
	print STDOUT "$host: End of run not found\n";
	print STDERR "$host: End of run not found\n" if ($debug);
	system("/usr/bin/tail -1 $host.new");
    }
    unlink "$host.new" if (! $debug);
}

Blog at WordPress.com.