ssh-command-stats 951 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. our %sshs;
  5. open(HISTORY, "$ENV{HOME}/.histfile") || die "Cannot open history file: $!\n";
  6. sub hashValueDescendingNum {
  7. $sshs{$b} <=> $sshs{$a};
  8. }
  9. while (<HISTORY>) {
  10. my $command = $_;
  11. if ($command =~ m/^$/) {
  12. next;
  13. }
  14. my @comm = split(' ', $command);
  15. if ($comm[0] eq "sudo") {
  16. shift @comm;
  17. }
  18. if (($comm[0] eq "ssh-add") or ($comm[0] eq "add-ssh-keys")) {
  19. if (not exists($sshs{"KEY-MANAGEMENT"})) {
  20. $sshs{"KEY-MANAGEMENT"} = 0;
  21. }
  22. $sshs{"KEY-MANAGEMENT"}++;
  23. next;
  24. }
  25. if (not $comm[0] eq "ssh") {
  26. next;
  27. }
  28. if (exists $comm[1]) {
  29. if (not exists($sshs{$comm[1]})) {
  30. $sshs{$comm[1]} = 0;
  31. }
  32. $sshs{$comm[1]}++;
  33. }
  34. }
  35. foreach my $key (sort hashValueDescendingNum (keys(%sshs))) {
  36. my $value = sprintf "%8s", $sshs{$key};
  37. print "$value\t$key\n";
  38. }