2 |
include('config.inc.php'); |
include('config.inc.php'); |
3 |
include('ppping.inc.php'); |
include('ppping.inc.php'); |
4 |
|
|
|
function sshdo($cmd, $ip, $user="root") |
|
|
{ |
|
|
global $privkey_file; |
|
|
global $pubkey_file; |
|
|
global $socket_timeout; |
|
|
|
|
|
if (!function_exists("ssh2_connect")) die("sshdo(): function ssh2_connect doesn't exist. You must install the ssh2 module from pecl library!"); |
|
|
|
|
|
// check if port exists |
|
|
$con = fsockopen( $ip, "22", $errono, $errstr, $socket_timeout); |
|
|
if (!$con) |
|
|
{ |
|
|
echo "sshdo(): socket failure,<br>could not connect to port 22<br>"; |
|
|
} |
|
|
else |
|
|
{ |
|
|
// first off all close our open socket |
|
|
fclose($con); |
|
|
|
|
|
if(!($con = ssh2_connect($ip, "22"))) |
|
|
{ |
|
|
echo "sshdo(): failure, could not connect<br>"; |
|
|
} |
|
|
else |
|
|
{ |
|
|
if(!ssh2_auth_pubkey_file($con, $user, $pubkey_file, $privkey_file)) |
|
|
{ |
|
|
echo "sshdo(): failure, could not login<br>"; |
|
|
} |
|
|
else |
|
|
{ |
|
|
//echo "logged in\n"; |
|
|
|
|
|
if(!($stream = ssh2_exec($con, "source /etc/profile; ".$cmd."; echo '__RETVAL__:'$?"))) |
|
|
{ |
|
|
echo "sshdo(): failure, could not execute command '".$cmd."'<br>"; |
|
|
} |
|
|
else |
|
|
{ |
|
|
// collect stream data |
|
|
stream_set_blocking($stream, true); |
|
|
$data = ""; |
|
|
while ($buf = fread($stream,4096)) |
|
|
{ |
|
|
$data .= $buf; |
|
|
//echo "buf=" .$buf."\n"; |
|
|
} |
|
|
fclose($stream); |
|
|
# get the last element of data as retval |
|
|
$retval = explode('__RETVAL__:', trim($data)); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
// return an array -> 0=retval, 1=data |
|
|
return array($retval[1], $retval[0]); |
|
|
} |
|
|
|
|
|
function ssh_editvar($var, $value, $file) |
|
|
{ |
|
|
global $ip; |
|
|
|
|
|
// sed cmd: ssh -l root 10.11.12.13 'sed -i -e "s|^\(VAR=\).*|\1\"VALUE\"| FILE' |
|
|
sshdo('if [[ ! -z $(grep "^'.$var.'=" '.$file.') ]]; then sed -i -e "s|^\('.$var.'=\).*|\1\"'.$value.'\"|" '.$file.' ;else echo "'.$var.'=\"'.$value.'\"" >> '.$file.'; fi', $ip); |
|
|
} |
|
|
|
|
5 |
function online($ip) |
function online($ip) |
6 |
{ |
{ |
7 |
$ping = new PPPing(); |
$ping = new PPPing(); |