2011-08-03 76 views
0

我創建了一個連接到主機並執行一些命令的Perl腳本,它工作正常!我kinf感到驕傲,因爲我是用Perl真正福利局^^ ...調用通過SSH連接到主機的PERL腳本

perl腳本的概述:

use Expect;    
$|=0;       
$Expect::Debug=0;  
$Expect::Exp_Internal=0; 
$Expect::Log_Stdout=1; 

my $ip = $ARGV[0]; 
my $file = $ARGV[1]; 
my $username = $ARGV[2]; 
my $password = $ARGV[3]; 
open(CONF,$file) || die "File not found"; 

while(<CONF>){ 
    $con .= $_; 

} 
my @conf = split("#",$con); 
my $ssh = Expect->spawn("ssh -q -l $username $ip") || die "Spawn ssh failed, $!"; 
    if($ssh->expect(5,"yes")) { 
     print $ssh "yes\r"; 
    if($ssh->expect(10,"assword")) { 
    print $ssh "$password\r"; 
    } 
    else { 
      warn $ssh->exp_error()."\n"; 
      next; 
    } 
    } 
    elsif($ssh->expect(10,"assword")) { 
    print $ssh "$password\r"; 
    } 
    else { 
     warn $ssh->exp_error()."\n"; 
      next; 
    } 

    #Variables Globales 
    my $rcmd;   
    my @lcmd;   
    my $lrcmd;  

    $regExpCmd = "\#"; 
    $regExpCmd2 = "^(A|B).*(\$|\#)"; 
    $regExp = "\n"; 

    $ssh->expect(10,$regExpCmd); 

    my $cmd0 = "environment no more\r"; 
    my $cmdExit = "logout\r"; 

    $ssh->send($cmd0); 
    $ssh->expect(5,$regExpCmd); 
    foreach my $step (@conf) { 

     my @lines = split("\n",$step); 

     foreach my $val (@lines){ 
     $val =~ s/^\s+//; 
     $val =~ s/\r//; 
     $ssh->send("$val\r"); 
     $i *= 1; 

     if(!$ssh->expect(2,$regExpCmd2)){ 
      $i *= 0;     
     # if($ssh->expect(1,"MINOR")){ 
     #  die "Erreur mineur: $val";} 
      if($ssh->expect(2,"Error")){ 
       die "Erreur majeur: $val"; 
      } 
     } 

     } 
     $ssh->expect(1,$regExpCmd2); 

    } 
    $ssh->send($cmdExit); 

    print $i; 

現在,我想從PHP調用它...

我曾嘗試不同的方式:

一樣叫我的perl腳本與EXEC()函數

<?php 
$arg1 = "MY.ADD.IP"; 
$arg2 = "MY/FILE"; 
$arg3 = "USERNAME"; 
$arg4 = "PASSWORD"; 
    $result = exec("perl /path/of/perl/script.pl $arg1 $arg2 $arg3 $arg4"); 
    if($result == 1) { 
     return true: } 
    else { 
     return false; 
    } ?> 

,但它沒有做任何事情(經過遠程主機等SSH Connexion公司在所有的)...

我也嘗試使用PECL Perl interpreter for PHP,打電話給我的劇本那樣:

<?php 
$perl = new Perl(); 
$perl->require('myperl.pl'); ?> 

但我沒有計算出如何發送一些arg到我的腳本中。

事實是,我需要用jQuery $ .ajax請求調用它,我需要等待腳本結束才發送回任何「回答」jQuery。

一切我試過沒有工作,因爲PHP腳本結束Perl腳本「之前」 ......

PS:我也試過在Perl創建了一個叫做用PHP,象下面這樣:

package Connect; 
sub new{ 
#Init some var... } 
sub connect { 
#Something like the script above..... 
} 


<?php 
$perl = new Perl(); 
$perl->require('myscript.pl'); 
$perl->call('connect',$args); 
?> 

你有沒有成功過這樣的事情?我真的不知道該怎麼做:(

+2

你可以用'system()'而不是'exec()'來試試腳本嗎?這應該將命令行輸出打印到屏幕上。 – Legolas

+0

檢查你的php.ini中的php安全模式選項,也許它已啓用。 – Zyava

+0

@Zyava:我查過了,它已被禁用。感謝 – Franquis

回答

0

爲什麼你不使用ssh從php?它看起來像ssh部分會比你在perl中做的更容易,你仍然可以獲得perl 。正則表達式使用參看preg_功能

PHP.net ssh2 manual page PHP.net preg_match manual page

+0

我嘗試使用PHP和PHPSECLIB :: SSH2函數的SSH ......事實是perl中的'expect'函數是唯一的工作解決方案,因爲我不知道主機對我的命令的答案是什麼在我發送它們之前... – Franquis

0

phpseclib, a pure PHP SSH implementation,具有非常類似的期待

示例如下:

<?php 
include('Net/SSH2.php'); 

$sftp = new Net_SSH2('www.domain.tld'); 
$sftp->login('username', 'password'); 

echo $sftp->read('[email protected]:~$'); 
$sftp->write("sudo ls -la\n"); 
$output = $sftp->read('#Password:|[email protected]:~\$#', NET_SSH2_READ_REGEX); 
echo $output; 
if (preg_match('#Password:#', $lines)) { 
    $ssh->write("password\n"); 
    echo $sftp->read('[email protected]:~$'); 
} 
?> 

它會執行「sudo ls -la」並等待「Password:」或「username @ username:〜」。

相關問題