-1

Scenario

Laptop A: I have a perl script that automates FTP/Upload download. Let me call it ftp_script.pl. I just run this script and it does the rest.

System B: Now, say I am at a different location. I want to trigger this perl script(ftp_script.pl) remotely from Laptop B(from my home) only using perl script. I want to know what are possible ways to trigger a perl script using another perl script?

Requirements:

  1. I want to start a Perl script (say ftp_script.pl) sitting from my home to a remote system (located in office) whose public IP I know.
  2. I want to start a Perl script (say ftp_script.pl) from one laptop to other connected over LAN via hub.

Is this possible to achieve?

PS: I don't know much about Perl scripting.

arka.b
  • 204
  • 2
  • 13
  • Before you mark this duplicate, I have already viewed - http://stackoverflow.com/questions/29645053/trigger-perl-scripts-remotely-on-windows but this one does not meet my requirement. – arka.b Sep 30 '15 at 06:33
  • 2
    This has nearly nothing to do with perl but can simply be reduced to the question of how to trigger running a program on a remote system. There are obvious ways like logging in with SSH, CGI-Scripts in a web server etc but your real requirements are unknown. – Steffen Ullrich Sep 30 '15 at 06:46
  • Steffen - I want to trigger a perl script that triggers another perl script which is done to automate FTP upload/download in remote system. – arka.b Sep 30 '15 at 07:05
  • One possibility: simply log in via SSH and start the script. If this does not meet your requirement than explain and use this as a way to actually define your setup and requirements more clearly. There are in theory zillions of ways to do what you want and don't expect somebody to show every possibility to you so that you can complain that it does not meet your poorly specified requirements. – Steffen Ullrich Sep 30 '15 at 09:09
  • why don't you want to use SSH? You can have your local Perl script log into SSH and start the remote script. Is this not acceptable? If not, why not? –  Sep 30 '15 at 14:39
  • 1
    You cannot directly cause another machine to execute a program; you need to contact a service running on the machine and ask it to execute the program. `ssh` is the most apt service for doing this, but you ruled it out without saying why. That leaves us with no way of evaluating other options, so to answer your question, *Is this possible to achieve?* We can't tell, because it is known that your system has restrictions that may prevent this, but we don't know what those restrictions are. – ikegami Sep 30 '15 at 15:02
  • Thanks ikegami, dan111 for your explanation. Before you guys scream at me the only reason of not using `SSH` was *my manager did not wanted to, I explained him with the reasons you gave. He accepted it.* And with little search I now know how to trigger a perl script over SSH. http://stackoverflow.com/questions/18236988/execute-perl-script-on-remote-server-from-local-machine, http://stackoverflow.com/questions/12616671/run-a-perl-script-on-remote-machine-from-local-machine-using-telnet-or-ssh-with – arka.b Oct 01 '15 at 04:27

2 Answers2

0

we need to have a mechanism at the remote host that can pick up the requested connection on the specified port, and also handle the debugger interaction from the command line You can use netcat but to reduce dependencies on external programs, and to give you a better idea as to what is happening behind the scenes, you can use following program

#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use IO::Socket;
use Term::ReadLine;
use constant BIGNUM => 65536;
our $previous_input;
# Set host and port.
my $host = shift || 'localhost';
my $port = shift || 12345; # over 1024 please
die("Usage: $0 hostname portno") unless ($host =~ /\w+/ && $port =~
^\d+$/);
print "listening on $host:$port\n";
my $term = new Term::ReadLine 'local prompter';
my $OUT;
{
# strict subs complains about STDOUT, so turn it off for the moment.
no strict 'subs';
$OUT = $term->OUT || STDOUT;
}
$OUT->autoflush(1);
# Open the socket the debugger will connect to.
my $sock = new IO::Socket::INET(
LocalHost => $host,
LocalPort => $port,
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1);
$sock or die "no socket :$!";
my $new_sock = $sock->accept();
# Try to pick up the remote hostname for the prompt.
my $remote_host = gethostbyaddr($sock->sockaddr(), AF_INET) || 'remote';
my $prompt = "($remote_host)> ";
my ($buf, $input

# Read output from the debugger, then read debugger input.
while (1) {
# Drop out if the remote debugger went away.
exit 0 unless sysread($new_sock, $buf, BIGNUM);
print $OUT $buf;
# Drop out if we got end-of-file locally (warning: this
# causes the remote Perl to drop dead because the socket goes away).
exit 0 unless defined($input = $term->readline($prompt));
print { $new_sock } munge_input($input);
# Add the line to the terminal history.
$term->addhistory($input) if $input =~ /\S/;
}
# The debugger interaction can get all confused if the string it gets
# passed is just a null. We clean this up here.
sub munge_input {
my $actual_input = shift;
$actual_input = "\n" unless defined $actual_input;

Note also that whichever program you choose as the listener, you will need to select a port number higher than 1024 unless you’re running as root (not a good idea) on that machine.you can debug perl program from remote machine using PERLDB_OPTS

PERLDB_OPTS="RemotePort=192.168.0.7:12345" perl  helloworld
Hello World
process id(2798)

IP address:port number

0

OK - this has probably been mentioned in many other places but will give a couple of tips that might help.

So you know that you can run a command on the remote server using ssh by doing something like ssh systemsboy@rhost.systemsboy.edu '/usr/bin/perl /home/of/perlscript.pl' and assuming that your keys are setup correctly this can be scripted as part of some automated workflow.

From a perl script you can execute the ssh command line in a local shell to launch the remote script by using something like system("ssh systemsboy@rhost.systemsboy.edu '/usr/bin/perl /home/of/perlscript.pl'"); within your perl script on the calling server.

There are probably better approaches but for a quick and dirty approach this should get you up and running.

.. the mention of over LAN or Wifi does suggest that there may be security constraints as this isn't over unimpeded open internet .. you may need to grapple also with identifying the target machine, checking there is a path, ensuring it is turned on etc.

Peter Scott
  • 1,318
  • 12
  • 19