cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1977
Views
0
Helpful
3
Replies

Perl script to get uptime from Cisco ASA

kamil.swedrak
Level 1
Level 1

Hello everyone,

I would like to create a script which will return device uptime.

I thinking to use SNMP and OID (I wanted use 1.3.6.1.2.1.1.3. sysUpTime) but I don't know how to use this in code.

Please help me - I appreciate any help.

Best regards,

Kamil

3 Replies 3

UWE STEINHAU
Level 1
Level 1

The exact code would depend on whether your ASA is configured for SNMPv2 or SNMPv3. Which SNMP version is configured on this device?

For a PERL script you would need some PERL modules or have the SNMP utilities installed, which you could then call from within the PERL script or simply call the SNMP client from a SHELL script. The exact syntax of the SNMP query depends on the installed SNMP package on the systems issuing the query. You could always read the man page (man snmpget) or run a query with the -h switch (snmpget -h) to find out how to build your query. I would need more info as to how the SNMPv3 agent on the ASA is configured to build a proper example of the SNMPv3 query.

This simple SNMPv2 example is for NetSNMP package version 5.6.2.1 with 'public' being the default read-only community string for SNMP queries:

snmpget -v2c -c public yourhostname  .1.3.6.1.2.1.1.3   or   snmpget -v2c -c public yourhostname  .1.3.6.1.2.1.1.3.0

and should return the ASA's uptime since the last restart.

Inside the PERL script you would write the query to a string and then print the string to the screen:

$uptime = `snmpget -v2c -c public yourhostname  .1.3.6.1.2.1.1.3.0`;

print $uptime;

Hope this gets you started.

= Uwe =

Thank you Uwe for your response - I appreciate this.

ASA using SNMPv1 & v2 version (checked that by command sh snmp-server group & sh runn | inc snmp). Security model: v2c.

I checked few websites and it looks like that Net::SNMP will be useful for this purpose.

I created sample code based on some information in the internet and please take a look:
#!/usr/bin/perl
use Net::SNMP;
my $sysUptime = '1.3.6.1.2.1.1.3;
($session, $error) = Net::SNMP->session(
                   -hostname => "ASA_IP",
                   -community => "public",
                   -timeout  => "30",
                   -port => "161");

if (!defined($session)) {
      printf("ERROR: %s.\n",  $error);
      exit 1;
}
my $response = $session->get_request($sysUptime);
my %pdesc = %{$response};

my $err = $session->error;
if ($err){
   return 1;
}
print %pdesc;
exit 0;

It will be good? Please advise.

Regards,

Kamil

Kamil,

Looks pretty good but it probably won't run as is. You'll need to change:

my $sysUptime = '1.3.6.1.2.1.1.3;

to

my $sysUptime = '1.3.6.1.2.1.1.3.0';

and it should run fine.

= Uwe =

Review Cisco Networking for a $25 gift card