Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Run a program remotely

by bblustein (Beadle)
on Mar 06, 2003 at 20:47 UTC ( [id://240995]=perlquestion: print w/replies, xml ) Need Help??

bblustein has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks. I am a perl newbie, and I have a question: What module and what format will allow me to execute commands remotely through perl. I have many machines that I would like to be able to have run a batch file just as if I were actually in front of that machine typing at a command prompt. Any ideas? Follow up - Any idea how I could get current drive mappings for remote machines? I'd like to know exactly which servers and machines each of the target machines is mapped to at a given moment. Any help is greatly appreciated.

Replies are listed 'Best First'.
Re: Run a program remotely
by phydeauxarff (Priest) on Mar 06, 2003 at 22:57 UTC
    if the target is a Unix box, you could use Net::Telnet

    We used this as the basis for remotely provisioning and controlling about 4000 Netscreen appliances before Net::Telnet::Netscreen came out.

    It's pretty easy to use as you can see from the example provided.
    use Net::Telnet ();
        $t = new Net::Telnet (Timeout => 10,
                              Prompt => '/bash\$ $/');
        $t->open("sparky");
        $t->login($username, $passwd);
        @lines = $t->cmd("who");
        print @lines;
    

    You might have to make some adjustments for the prompt returned, but the documention provides examples for this as well.
    Of course, if you aren't connecting to something that will accept a Telnet session, such as a winblows box....all bets are off for this example.
Re: Run a program remotely
by pg (Canon) on Mar 06, 2003 at 22:49 UTC
Re: Run a program remotely
by zentara (Archbishop) on Mar 07, 2003 at 13:16 UTC
    If you are going to be doing things on a remote machine, you may as well start out right, and use ssh instead of telnet. Why use telnet, and show your password to the world?
    #!/usr/bin/perl use strict; use warnings; use Net::SSH::Perl; my %hostdata = ( 'localhost' => { user => "zz", password => "ztest", cmdtorun => "ls -la", misc_data => [], }, 'zentara.zentara.net' => { user => "zz", password => "ztest", cmdtorun => "/usr/bin/uptime", misc_data => [], }, ); foreach my $host (keys %hostdata) { my $ssh = Net::SSH::Perl->new($host, port => 22); #, debug => 1 +); $ssh->login($hostdata{$host}{user},$hostdata{$host}{password} ); my ($out) = $ssh->cmd($hostdata{$host}{cmdtorun}); print "$out\n"; }
      Thank you for your advice, I will start playing around with this stuff today. thank you.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://240995]
Approved by thelenm
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (8)
As of 2024-04-23 17:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found