Showing results for 
Search instead for 
Did you mean: 

Perl script to get uptime from Cisco ASA


Hello everyone,

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

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

Please help me - I appreciate any help.

Best regards,


3 Replies 3


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 with 'public' being the default read-only community string for SNMP queries:

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

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  .`;

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:
use Net::SNMP;
my $sysUptime = ';
($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.




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

my $sysUptime = ';


my $sysUptime = '';

and it should run fine.

= Uwe =

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Recognize Your Peers