#!/usr/bin/perl -- # -*-Perl-*- # # rexec.pl # # a simple rexec program # # library for sockets use Socket; # constants and defaults $| = 1; $user = getlogin || (getpwuid($<))[0] || die "Unable to get username +!"; $remote = 'machine.domain'; $port = getservbyname('exec','tcp'); $usage = " Usage: rexec.pl [options] command Options: -? print this help screen and exit -v verbose output mode -u <username> specify username (default: $user) -h <hostname> specify host (default: $remote) -p <password> specify a password to use Notes: It is a good idea to enclose your command in single quotes to avoid shell interpretation. "; # parse options while ($_ = shift @ARGV) { if (/^-\?$/) { print $usage; exit; } if (/^-v$/) { $verbose = 1; next; } if (/^-u$/) { $user = shift or die "Error: Missing username"; n +ext; } if (/^-h$/) { $remote = shift or die "Error: Missing hostname"; n +ext; } if (/^-p$/) { $passwd = shift or die "Error: Missing password"; n +ext; } $cmd = join ' ',($_,@ARGV); last; } # if we don't have a command, we must have not gotten the right option +s if (! $cmd) { print "Error: Missing command\n"; print $usage; exit; } # get password (without echoing it back to the screen) if (! $passwd ) { print "Password:"; system("stty -echo"); chomp($passwd = <STDIN>); system("stty echo"); print "\n"; } die "Error: No password" unless $passwd; # die if we don't have a port die "Error: No port" unless $port; # get some info $iaddr = inet_aton($remote) or die "inet_aton: $!"; # host ip (packed) $paddr = sockaddr_in($port, $iaddr); # port address (pa +cked) $proto = getprotobyname('tcp'); # protocol number # open the socket socket(SOCK, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; connect(SOCK, $paddr) or die "connect: $!"; # If we're verbose, print out what we're going to rexec print "CMD: $cmd\n" if ($verbose); # build our command and send it $string = "$port\0$user\0$passwd\0$cmd\0"; send(SOCK,$string,0,$remote) or die "send: $!"; # get any info back print "Listening for response...\n"; while ($line = <SOCK>) { print $line; } # put up our toy and exit nicely print "\nClosing connection...\n"; sleep 2; shutdown(SOCK,2); close(SOCK); exit 0;

In reply to rexec.pl by Shendal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.