include('config.inc.php');
include('ppping.inc.php');
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,
could not connect to port 22
";
}
else
{
// first off all close our open socket
fclose($con);
if(!($con = ssh2_connect($ip, "22")))
{
echo "sshdo(): failure, could not connect
";
}
else
{
if(!ssh2_auth_pubkey_file($con, $user, $pubkey_file, $privkey_file))
{
echo "sshdo(): failure, could not login
";
}
else
{
//echo "logged in\n";
if(!($stream = ssh2_exec($con, "source /etc/profile; ".$cmd."; echo '__RETVAL__:'$?")))
{
echo "sshdo(): failure, could not execute command '".$cmd."'
";
}
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);
}
function online($ip)
{
$ping = new PPPing();
$ping->hostname = $ip;
$ping->timeout = "0.3";
$result = $ping->Ping();
if($result<0)
{
echo "error: ".$ping->strError($result);
$retval=1;
}
else
{
echo $result." ms";
$retval=0;
}
return $retval;
}
?>