2015-09-30 28 views
-1

方案啓動一個Perl腳本,遠程通過網絡或WiFi

筆記本答: 我有一個自動FTP /上傳下載一個Perl腳本。讓我叫它ftp_script.pl。我只是運行這個腳本,剩下的就完成了。

系統B: 現在,說我在一個不同的位置。我想從筆記本電腦B(來自我家)遠程觸發此perl腳本(ftp_script.pl),僅使用perl腳本。我想知道使用另一個perl腳本觸發perl腳本的可能方法是什麼

要求:

  1. 我要開始一個Perl腳本(比如ftp_script.pl)從我家坐到遠程系統(位於辦公室),其公網IP,我知道。
  2. 我想通過集線器從一臺筆記本電腦啓動一個Perl腳本(比如ftp_script.pl)到其他連接的LAN。

這是可能實現的嗎? PS:我不太瞭解Perl腳本。

+0

在您標記此複本之前,我已經查看過 - http://stackoverflow.com/questions/29645053/trigger-perl-scripts-windows-remote,但這個不符合我的要求。 –

+2

這與perl幾乎沒有任何關係,但可以簡化爲如何觸發在遠程系統上運行程序的問題。有很多顯而易見的方法,例如使用SSH登錄,在Web服務器等中使用CGI腳本,但您的真實需求未知。 –

+0

Steffen - 我想觸發一個perl腳本,觸發另一個perl腳本,這是在遠程系統中自動完成FTP上傳/下載的操作。 –

回答

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,除非你運行的是 root (不是一個好主意)對machine.you可以從遠程計算機上使用PERLDB_OPTS

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

IP地址:端口號

0

確定 - 這可能已在其他許多地方被提及,但會給一些可能有幫助的提示。

所以你知道你可以在遠程服務器上使用ssh執行命令,例如ssh [email protected] '/usr/bin/perl /home/of/perlscript.pl',並假設你的密鑰安裝正確,這可以作爲一些自動化工作流程的一部分編寫腳本。

從perl腳本中,您可以在本地shell中執行ssh命令行,通過在調用服務器的perl腳本中使用類似system("ssh [email protected] '/usr/bin/perl /home/of/perlscript.pl'");的內容來啓動遠程腳本。

有可能是更好的方法,但對於一個快速和骯髒的方法,這應該讓你啓動和運行。

..提及局域網或無線網絡確實表明可能存在安全限制,因爲這並不是不受阻礙的開放式Internet。您可能還需要擒拿以識別目標機器,檢查是否存在路徑,確保它已打開等。

相關問題