#!/usr/bin/perl use warnings; use Net::SSH::Expect; my $conn; my @output; my $date; my $host = ""; ## IP/Host Address/Name of the controller to connect to my $username = ""; ## SSH username of controller my $password = ""; ## SSH password of controller ## magic to get the date formatting correct to get the previous days logs $date = `date --date="-1 day" +%b`; $dom = `date --date="-1 day" +%Oe`; $date = $date." ".$dom; $date =~ s/\n//g; my @CmdList = ("no paging","show audit-trail login | include \"$date\""); ## array of commands to run on the controller $conn = arubaConnect($host,$username,$password); ## command to open connection on the controller $output = execArubaCmds($conn,\@CmdList); print "Logins for " . $date . " listed below:\n\n". $output. "\n\n"; sub arubaConnect { ## Obtain arguments for the connection my $host = $_[0]; my $user = $_[1]; my $pass = $_[2]; ## ## Call constructor and define our init variables ## "log_stdout" should be set to "1" to enable debug ## of the connection. "ssh_option" can be found ## in ssh_config man pages. It is necessary because the ## current environment being virtual and VPN use. ## my $conn = Net::SSH::Expect->new ( host => "$host",password => "$pass", user => "$user",log_stdout => 0, ssh_option => "-o ControlMaster=auto", # timeout => 5, # used to increase timeout when connecting to the controller raw_pty => 0); return $conn; } sub arubaDisconnect { my $conn = $_[0]; $conn->close(); } sub execArubaCmds { ## Obtain our connection instance and commands to be run my $conn = $_[0]; my $cmdList = $_[1]; my $results; ## Login to Controller, obtain any errors eval {$conn->login()}; my $error = $@; ## Execute each command passed in, and return the output ## of each command as seperate elements within an array if ($error !~ m/.*SSHAuthenticationError Login timed out.*/){ foreach (@$cmdList) { $results .= $conn->exec($_,1); # 1 is the delay between commands for some situations this needs to be increased or it can be removed completly } } else { $results = "Login timed out"; } # Done running commands close connection arubaDisconnect($conn); return $results; }