Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.77 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. #
  3. # logssh
  4. # John Simpson <jms1@jms1.net> 2008-07-15
  5. #
  6. ###############################################################################
  7. #
  8. # Copyright (C) 2008 John Simpson.
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License, version 3, as
  12. # published by the Free Software Foundation.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. ###############################################################################
  23.  
  24. require 5.003 ;
  25. use strict ;
  26.  
  27. ###############################################################################
  28. #
  29. # configuration
  30.  
  31. # the location of your normal "ssh" executable
  32. my $ssh = "/usr/bin/ssh" ;
  33.  
  34. # the location of your "tee" executable
  35. my $tee = "/usr/bin/tee" ;
  36.  
  37. # the directory where you want the logs to be stored
  38. # this directory must exist before running the program
  39. my $logdir = $ENV{"HOME"} . "/.ssh/logs" ;
  40.  
  41. # do you want this script to show the real command before running it?
  42. my $show_cmd = 0 ;
  43.  
  44. ###############################################################################
  45. ###############################################################################
  46. ###############################################################################
  47. #
  48. # examine the command line arguments to find a "userid@server" string
  49.  
  50. my $target = "" ;
  51. for my $k ( @ARGV )
  52. {
  53.     if ( $k =~ /\@/ )
  54.     {
  55.         $target = $k ;
  56.         last ;
  57.     }
  58. }
  59.  
  60. die "Can't recognize user\@host target\n"
  61.     unless ( $target ) ;
  62.  
  63. ###############################################################################
  64. #
  65. # build the name of the log file
  66.  
  67. my @d = localtime ;
  68. my $now = sprintf ( "%04d-%02d-%02d.%02d%02d%02d" ,
  69.     $d[5]+1900 , $d[4]+1 , $d[3] , $d[2] , $d[1] , $d[0] ) ;
  70.  
  71. my $logfile = $logdir . "/$now.$target" ;
  72.  
  73. ###############################################################################
  74. #
  75. # build the final command that we will be running
  76.  
  77. my $cmd = "$ssh " . join ( " " , @ARGV ) . " | $tee -a $logfile" ;
  78.  
  79. ###############################################################################
  80. #
  81. # start the log file with that command
  82.  
  83. open ( L , ">$logfile" )
  84.     or die "Can't create logfile \"$logfile\": $!\n" ;
  85. print L "CMD: $cmd\n" , "=" x 78 , "\n" ;
  86. close L ;
  87.  
  88. ###############################################################################
  89. #
  90. # do the deed
  91.  
  92. $show_cmd && print "% $cmd\n" ;
  93. exec $cmd or die "exec() failed: $!\n" ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement