Secure rexec… Why did I not go to bed 2 hours ago?

It is 1 AM on July 6th, 2006 and I think that I may have reached the pinnacle of techie stupidity. I am sure you are asking why? Well the answer is that I should have gone to bed hours ago because my 1 year old daughter is going to get me up in 3 or if I am lucky 4 hours from now. The reality is by the time I am done writing this post it will probably be more like 2 hours.

Anyway, I was sitting at the computer tonight and out of pure boredom decided to finish up some code I started a while ago at a techie conference session (I was probably suffering from an even more sever case of boredom then). This was a perl script that would function in the same way as rexec but utilizing the more secure ssh protocol. Again you may ask why? I often find myself asking the same question. No, really the concept was to include this in a much larger project that is still on the drawing board and may or may not actually happen depending on if I can find the cycles.

I have decided to post to code to my blog for anyone else who would like to improve, modify or use the code. I also have a windows binary version compiled for users who may want to use it but do not know how to install ActivePerl or the required perl modules, if you leave a comment I will provide the ftp site where you can grab it from.

Right now the connection speed is a bit slow and I am not sure why, I think there may be a problem with the Net::SSH::W32Perl module and password authentication but I can not verify.

You will also need to create a hosts.txt file in the same directory where the srexec.pl script is.? This file should contain the host(s) you would like the passed command to be run on.

The same code will work on UNIX/Linux just use the Net::SSH perl module as opposed to the NET::SSH::W32Perl module. Both modules can be acquired from CPAN (http://www.cpan.org).

Enjoy!

-RJB

#————begin————

#!/usr/local/bin/perl
### srexec.exe – Secure Remote Execution
### Rich Bocchinfuso
### Revision History
### 06-06-2006 – rev 0.02a

### 04-25-2006 – rev 0.01a

use strict;
use Net::SSH::W32Perl;

my $f=’hosts.txt’;

if ($ARGV[0] eq “-e”) {
&execute;
}
elsif ($ARGV[0] eq “-h”) {
&error;
}
elsif ($ARGV[0] eq “-about”) {
&about;
}

sub execute {
( my $command = $ARGV[1] || &error);
#chop $command;
( my $user = $ARGV[2] || &error);
#chop $user;
( my $pass = $ARGV[3] || &error);
#chop $pass;

my ($ssh, $out, $err, $exit);

my %args;
$args{debug} = 1;
$args{protocol} = 2;

open F, “< $f" or die "Can't open $f : $!"; while () { chop $_; $ssh = new Net::SSH::W32Perl($_, %args); $ssh->login($user, $pass);
($out, $err, $exit) = $ssh->cmd($command,”\n”);
}
close (F);
exit;
}

sub error {
print “\nsrexec.exe\n\nUsage: srexec.exe [option] [command string] [username] [password]\n”;
print “i.e. – srexec.exe -e \”ps -ef | grep getty > test.out\” user password\n”;
print “srexec.exe -h => This Help\n”;
print “srexec.exe -about => Detailed srexec info\n”;
exit;
}

sub about {
print “\nSecure Remote Execution\n”;
print “Rev 0.1a build date: 04-25-2006 \n”;
print “Author: Rich Bocchinfuso \n”;
print “Licence: GPL\n”;
exit;
}

#————end————

One thought on “Secure rexec… Why did I not go to bed 2 hours ago?

Leave a Reply

Your email address will not be published. Required fields are marked *