通过LAN或WiFi远程启动Perl脚本

时间:2015-09-30 06:32:46

标签: perl ssh automation remote-access remote-server

方案

笔记本电脑A: 我有一个自动执行FTP /上载下载的perl脚本。我称之为 ftp_script.pl 。我只是运行这个脚本,剩下的就完成了。

系统B: 现在,说我在不同的位置。我想从Laptop B(从我家)远程触发这个perl脚本(ftp_script.pl)仅使用perl脚本。我想知道使用另一个perl脚本触发perl脚本的可能方法

要求:

  1. 我想从我家开始一个Perl脚本(比如ftp_script.pl)到我知道的公共IP的远程系统(位于办公室)。
  2. 我想从一台笔记本电脑启动一个Perl脚本(例如ftp_script.pl)到通过集线器局域网连接的其他笔记本电脑
  3. 这有可能实现吗?

    PS:我对Perl脚本的了解不多。

2 个答案:

答案 0 :(得分:0)

我们需要在远程主机上有一个可以接收的机制 在指定端口上请求连接,并从命令行处理调试器交互  您可以使用netcat但减少对外部程序的依赖性,并给予 如果您对幕后发生的事情有了更好的了解,可以使用以下程序

#!/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;

另请注意,您选择哪个程序作为 监听器,除非你正在运行,否则你需要选择一个高于1024的端口号 根 在那台机器上(不是一个好主意)。你可以使用PERLDB_OPTS从远程机器调试perl程序

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

IP地址:端口号

答案 1 :(得分:0)

好的 - 这可能已经在许多其他地方提到过,但会提供一些可能有帮助的提示。

所以你知道你可以通过做ssh systemsboy@rhost.systemsboy.edu '/usr/bin/perl /home/of/perlscript.pl'这样的事情来使用ssh在远程服务器上运行一个命令,并假设你的密钥设置正确,这可以作为一些自动化工作流程的一部分编写脚本。

从perl脚本中,您可以在本地shell中执行ssh命令行,通过在调用服务器上的perl脚本中使用system("ssh systemsboy@rhost.systemsboy.edu '/usr/bin/perl /home/of/perlscript.pl'");之类的内容来启动远程脚本。

可能有更好的方法,但对于快速而肮脏的方法,这应该可以帮助您启动并运行。

..提到LAN或Wifi确实表明可能存在安全限制,因为这不是无阻碍的开放互联网..您可能还需要抓住识别目标机器,检查有路径,确保它打开等等。