Annotation of /alx-src/branches/alx-web-070/include/common-functions.php
Parent Directory | Revision Log
Revision 2592 -
(hide annotations)
(download)
Tue Jul 5 15:24:46 2011 UTC (13 years, 2 months ago) by niro
Original Path: alx-src/branches/alx-web-060/include/common-functions.php
File size: 1720 byte(s)
Tue Jul 5 15:24:46 2011 UTC (13 years, 2 months ago) by niro
Original Path: alx-src/branches/alx-web-060/include/common-functions.php
File size: 1720 byte(s)
-fixed a typo (missing $ in variable name)
1 | niro | 2021 | <? |
2 | niro | 2194 | include('config.inc.php'); |
3 | include('ppping.inc.php'); | ||
4 | niro | 2021 | |
5 | niro | 2194 | function sshdo($cmd, $ip, $user="root") |
6 | { | ||
7 | global $privkey_file; | ||
8 | global $pubkey_file; | ||
9 | |||
10 | niro | 2417 | if (!function_exists("ssh2_connect")) die("sshdo(): function ssh2_connect doesn't exist. You must install the ssh2 module from pecl library!"); |
11 | niro | 2194 | |
12 | if(!($con = ssh2_connect($ip, "22"))) | ||
13 | niro | 2021 | { |
14 | niro | 2417 | echo "sshdo(): failure, could not connect\n"; |
15 | niro | 2194 | } |
16 | else | ||
17 | { | ||
18 | if(!ssh2_auth_pubkey_file($con, $user, $pubkey_file, $privkey_file)) | ||
19 | niro | 2021 | { |
20 | niro | 2417 | echo "sshdo(): failure, could not login\n"; |
21 | niro | 2194 | } |
22 | else | ||
23 | { | ||
24 | niro | 2417 | //echo "logged in\n"; |
25 | niro | 2021 | |
26 | niro | 2194 | if(!($stream = ssh2_exec($con, "source /etc/profile; ".$cmd."; echo '__RETVAL__:'$?"))) |
27 | niro | 2022 | { |
28 | niro | 2417 | echo "sshdo(): failure, could not execute command '".$cmd."'\n"; |
29 | niro | 2194 | } |
30 | else | ||
31 | { | ||
32 | // collect stream data | ||
33 | stream_set_blocking($stream, true); | ||
34 | $data = ""; | ||
35 | while ($buf = fread($stream,4096)) | ||
36 | niro | 2021 | { |
37 | niro | 2194 | $data .= $buf; |
38 | niro | 2416 | //echo "buf=" .$buf."\n"; |
39 | niro | 2021 | } |
40 | niro | 2194 | fclose($stream); |
41 | # get the last element of data as retval | ||
42 | $retval = explode('__RETVAL__:', trim($data)); | ||
43 | niro | 2021 | } |
44 | niro | 2022 | } |
45 | niro | 2194 | } |
46 | niro | 2021 | |
47 | niro | 2194 | // return an array -> 0=retval, 1=data |
48 | return array($retval[1], $retval[0]); | ||
49 | } | ||
50 | |||
51 | function ssh_editvar($var, $value, $file) | ||
52 | { | ||
53 | global $ip; | ||
54 | |||
55 | // sed cmd: ssh -l root 10.11.12.13 'sed -i -e "s|^\(VAR=\).*|\1\"VALUE\"| FILE' | ||
56 | niro | 2592 | sshdo('if [[ ! -z $(grep "^'.$var.'=" '.$file.') ]]; then sed -i -e "s|^\('.$var.'=\).*|\1\"'.$value.'\"|" '.$file. 'else echo "'.$var.'=\"'.$value.'\"" >> '.file, $ip); |
57 | niro | 2194 | } |
58 | |||
59 | function online($ip) | ||
60 | { | ||
61 | $ping = new PPPing(); | ||
62 | $ping->hostname = $ip; | ||
63 | $ping->timeout = "0.3"; | ||
64 | $result = $ping->Ping(); | ||
65 | |||
66 | if($result<0) | ||
67 | { | ||
68 | echo "error: ".$ping->strError($result); | ||
69 | $retval=1; | ||
70 | niro | 2021 | } |
71 | niro | 2194 | else |
72 | { | ||
73 | echo $result." ms"; | ||
74 | $retval=0; | ||
75 | } | ||
76 | return $retval; | ||
77 | } | ||
78 | niro | 2021 | ?> |