2016-05-04 4 views
-1

Terminalを使用して/ etc/config/uhttpdファイルのSSHポートを変更しました。しかし、私はPHPから動的にそれを行う方法を見つけることができないようです。説明するために、YunのLinuxシステム上のポートを自動的に設定するためにサーバが必要です。ですから、基本的にuhttpdファイルのポート番号を自動的に変更する必要があります。前もって感謝します。PHPを使用してarduino yun上のhttpd SSHポートを動的に変更します。

+0

ポートを自動的に変更してポートを動的に変更するとはどういう意味ですか? PHPスクリプトで設定ファイルをリモートで変更したいですか?このようなもの: './myscript.php --host = somehost --port = 8788'? –

+0

はい、Linuxチップがデフォルト80から別のポートにリモートで使用するリスニングポートを変更するには、PHPスクリプトが必要です。 – user3381715

答えて

0

Setup SSH keysリモートサーバーへのパスワードなしアクセス。

ユーザがrootでない場合は、sudoコマンドを使用してパスワードなしコマンドを実行するには、リモート/etc/sudoersを設定します。私たちは、簡単にするためにALLコマンドを許さ

%uhttpd ALL=(ALL) NOPASSWD: ALL 

パスワードなしで実行が許可

その後、
sudo gpasswd -a user uhttpd 

listコマンド:たとえば、あなたはuhttpdグループ(リモートで)にリモートユーザーを追加することができます。代わりに特定のコマンドをリストすることができます。 man sudoersを参照してください。

次のようなスクリプト書く:chport.phpに保存

#!/usr/bin/env php 
<?php 
namespace Tools\Uhttpd\ChangePort; 

$ssh_user = 'user'; // Change this 
$ssh_host = 'remote.host'; // Change this 
$remote_config = '/etc/config/uhttpd'; 

////////////////////////////////////////////////////// 

if (false === ($o = getopt('p:', ['port:']))) { 
    fprintf(STDERR, "Failed to parse CLI options\n"); 
    exit(1); 
} 

// Using PHP7 Null coalescing operator 
$port = $o['p'] ?? $o['port'] ?? 0; 
$port = intval($port); 
if ($port <= 0 || $port > 65535) { 
    fprintf(STDERR, "Invalid port\n"); 
    exit(1); 
} 

$sudo = $ssh_user == 'root' ? '' : 'sudo'; 

$sed = <<<EOS 
"s/option\s*'listen_http'\s*'[0-9]+'/option 'listen_http' '$port'/" 
EOS; 

// Replace port in remote config file 
execute(sprintf("ssh %s -- $sudo sed -i -r %s %s", 
    "{$ssh_user}@{$ssh_host}", $sed, 
    escapeshellarg($remote_config))); 

// Restart remote daemon 
execute("$sudo /etc/init.d/uhttpd restart"); 

////////////////////////////////////////////////////// 

/** 
* @param string $cmd Command 
* @return int Commands exit code 
*/ 
function execute($cmd) { 
    echo ">> Running $cmd\n"; 

    $desc = [ 
    1 => ['pipe', 'w'], 
    2 => ['pipe', 'w'], 
    ]; 

    $proc = proc_open($cmd, $desc, $pipes); 
    if (! is_resource($proc)) { 
    fprintf(STDERR, "Failed to open process for cmd: $cmd\n"); 
    exit(1); 
    } 

    if ($output = stream_get_contents($pipes[1])) { 
    echo $output, PHP_EOL; 
    } 

    if ($error = stream_get_contents($pipes[2])) { 
    fprintf(STDERR, "Error: %s\n", $error); 
    } 

    fclose($pipes[1]); 
    fclose($pipes[2]); 

    if (0 != proc_close($proc)) { 
    fprintf(STDERR, "Command failed(%d): %s\n", $exit_code, $cmd); 
    exit(1); 
    } 
} 

をし、それを実行可能にします。

chmod +x chport.php 

その後、あなたはこのようにそれを使用することができます:

./chport.php --port=10000 

スクリプトで使用されているコマンドをシェルスクリプトでラップし、にリストすることができます。

関連する問題