Browse Source

Run some basic statistics on ssh commands

Samuel W. Flint 8 years ago
parent
commit
a3e4c44d4d
1 changed files with 51 additions and 0 deletions
  1. 51 0
      ssh-command-stats

+ 51 - 0
ssh-command-stats

@@ -0,0 +1,51 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+our %sshs;
+
+open(HISTORY, "$ENV{HOME}/.histfile") || die "Cannot open history file: $!\n";
+
+
+sub hashValueDescendingNum {
+    $sshs{$b} <=> $sshs{$a};
+}
+
+while (<HISTORY>) {
+    my $command = $_;
+
+    if ($command =~ m/^$/) {
+        next;
+    }
+
+    my @comm = split(' ', $command);
+
+    if ($comm[0] eq "sudo") {
+        shift @comm;
+    }
+
+    if (($comm[0] eq "ssh-add") or ($comm[0] eq "add-ssh-keys")) {
+        if (not exists($sshs{"KEY-MANAGEMENT"})) {
+            $sshs{"KEY-MANAGEMENT"} = 0;
+        }
+        $sshs{"KEY-MANAGEMENT"}++;
+        next;
+    }
+
+    if (not $comm[0] eq "ssh") {
+        next;
+    }
+
+    if (exists $comm[1]) {
+        if (not exists($sshs{$comm[1]})) {
+            $sshs{$comm[1]} = 0;
+        }
+        $sshs{$comm[1]}++;
+    }
+}
+
+foreach my $key (sort hashValueDescendingNum (keys(%sshs))) {
+    my $value = sprintf "%8s", $sshs{$key};
+    print "$value\t$key\n";
+}