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

Hi,

I'm trying to write a script which involves logging into another machine before doing stuff. Basically what I'm trying to do is this:
log into another machine
cmd 1
cmd 2
.
.
.
cmd n
logout
First of all, is it possible to do this inside a script? If yes, then please advice on the best way to do it.

Thanks much,
Srinivas

  • Comment on Executing after logging into a remote machine

Replies are listed 'Best First'.
Re: Executing after logging into a remote machine
by blindluke (Hermit) on Oct 08, 2014 at 06:45 UTC

    It is possible. There are a few things missing in your question, however: do you want to use Perl, or will a shell script suffice? are you logging onto the host using FTP, SSH, or Telnet?

    Assuming that at this point you want only a proof that it can be done, here is a very, very basic example in shell.

    #!/bin/bash HOST=$1 ssh root@$HOST 'bash -s' <<'ENDSSH' # commands to run on remote host echo Installing httpd yum install -y httpd echo Yum exited with status $? ENDSSH

    ...and in Perl.

    #!/usr/bin/perl use v5.14; use Net::SSH::Perl; my ($host, $user, $pass) = @ARGV; my $ssh = Net::SSH::Perl->new($host); $ssh->login($user, $pass); my($stdout, $stderr, $exit) = $ssh->cmd('ls -ltr'); say $stdout;

    regards,
    Luke Jefferson

Re: Executing after logging into a remote machine
by QM (Parson) on Oct 08, 2014 at 08:07 UTC
    You can do it yourself in your own code, or I would recommend Net::OpenSSH.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re: Executing after logging into a remote machine
by thanos1983 (Parson) on Oct 08, 2014 at 10:18 UTC

    Hello Srinivas,

    Well your question has many gaps, and it can not be answered straight. You mention remote machine what do you mean by that? Is it a Windows OS, Linux, is it a modem, what is it? Do you need to have administration access to execute the commands?

    Second gap, you are working on Windows or Linux? Sample of code? If you do not have sample of code sample of commands? There is a huge difference on the solution.

    Do not take this wrong but please read How (Not) To Ask A Question, it will help you to create a good question and get more effective answers to your problem as fast as possible.

    As blindluke suggested you can use ssh to connect to a remote host. You can use But this solution will not work in all cases. If you are planning to use Bash you need to be working on Linux OS or using Cygwin on Windows OS.

    Provide us with more data, there are many cases that people had the same problem with you and got multiple answers through the community.

    Help us so we can help you.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Executing after logging into a remote machine
by srinigs (Novice) on Oct 08, 2014 at 10:47 UTC

    Hi All

    I'm extremely sorry as the question was not well formed. I was in a bit of a hurry so did not think through it completely (all though I agree that cannot be an excuse for all the gaps in the question :)). But thanks a lot for the replies despite that.

    To fill some of the gaps, both the machines are running Red Hat linux and I can use either ssh or rsh to login. I could have used shell script but for the fact that I need to use DBI module for a couple of db operations before I execute the commands on the remote machine. Net::SSH::Perl modules seems to work fine for my requirement

    Again, thanks for the replies

      Hello again srinigs,

      There is no need to apologize, by defining a well formatted question you help people to understand your problem and provide you with feedback faster. Any way enough said, let's code.

      Now that you provide us with more information, there was a recent question that I would suggest you read all threads and answers from it Simple SSH based chat client.

      I strongly suggest to read all threads. The question includes a variety of working answers with and without root access to the remote machine(s).

      You also mentioned db I assume you mean database (e.g. SQL or MySQL) to get information for multiple device(s).

      As an alternative I have posted a solution with conf.ini files where you can use to store your data instead of a db. In case you want to interact with multiple devices.

      Any problems you have do not hesitate to ask, this is the reason that the community exists. For all of us there are difficult tasks and people who might have solve this tasks before ;), so feel free to ask.

      Best of luck with your coding task.

      Seeking for Perl wisdom...on the process of learning...not there...yet!