Contents of /alx-src/branches/alx-web-070/include/common-functions.php
Parent Directory | Revision Log
Revision 3343 -
(show annotations)
(download)
Wed Feb 1 10:48:54 2012 UTC (12 years, 7 months ago) by niro
Original Path: alx-src/branches/alx-web-060/include/common-functions.php
File size: 2043 byte(s)
Wed Feb 1 10:48:54 2012 UTC (12 years, 7 months ago) by niro
Original Path: alx-src/branches/alx-web-060/include/common-functions.php
File size: 2043 byte(s)
- sshdo(): first check if the sockert can be connected as ssh2_connect has a timeout of 60 seconds, which cannot be changed
1 | <? |
2 | include('config.inc.php'); |
3 | include('ppping.inc.php'); |
4 | |
5 | function sshdo($cmd, $ip, $user="root") |
6 | { |
7 | global $privkey_file; |
8 | global $pubkey_file; |
9 | global $socket_timeout; |
10 | |
11 | if (!function_exists("ssh2_connect")) die("sshdo(): function ssh2_connect doesn't exist. You must install the ssh2 module from pecl library!"); |
12 | |
13 | // check if port exists |
14 | $con = fsockopen( $ip, "22", $errono, $errstr, $socket_timeout); |
15 | if (!$con) |
16 | { |
17 | echo "sshdo(): socket failure,<br>could not connect to port 22<br>"; |
18 | } |
19 | else |
20 | { |
21 | // first off all close our open socket |
22 | fclose($con); |
23 | |
24 | if(!($con = ssh2_connect($ip, "22"))) |
25 | { |
26 | echo "sshdo(): failure, could not connect<br>"; |
27 | } |
28 | else |
29 | { |
30 | if(!ssh2_auth_pubkey_file($con, $user, $pubkey_file, $privkey_file)) |
31 | { |
32 | echo "sshdo(): failure, could not login<br>"; |
33 | } |
34 | else |
35 | { |
36 | //echo "logged in\n"; |
37 | |
38 | if(!($stream = ssh2_exec($con, "source /etc/profile; ".$cmd."; echo '__RETVAL__:'$?"))) |
39 | { |
40 | echo "sshdo(): failure, could not execute command '".$cmd."'<br>"; |
41 | } |
42 | else |
43 | { |
44 | // collect stream data |
45 | stream_set_blocking($stream, true); |
46 | $data = ""; |
47 | while ($buf = fread($stream,4096)) |
48 | { |
49 | $data .= $buf; |
50 | //echo "buf=" .$buf."\n"; |
51 | } |
52 | fclose($stream); |
53 | # get the last element of data as retval |
54 | $retval = explode('__RETVAL__:', trim($data)); |
55 | } |
56 | } |
57 | } |
58 | } |
59 | |
60 | // return an array -> 0=retval, 1=data |
61 | return array($retval[1], $retval[0]); |
62 | } |
63 | |
64 | function ssh_editvar($var, $value, $file) |
65 | { |
66 | global $ip; |
67 | |
68 | // sed cmd: ssh -l root 10.11.12.13 'sed -i -e "s|^\(VAR=\).*|\1\"VALUE\"| FILE' |
69 | sshdo('if [[ ! -z $(grep "^'.$var.'=" '.$file.') ]]; then sed -i -e "s|^\('.$var.'=\).*|\1\"'.$value.'\"|" '.$file.' ;else echo "'.$var.'=\"'.$value.'\"" >> '.$file.'; fi', $ip); |
70 | } |
71 | |
72 | function online($ip) |
73 | { |
74 | $ping = new PPPing(); |
75 | $ping->hostname = $ip; |
76 | $ping->timeout = "0.3"; |
77 | $result = $ping->Ping(); |
78 | |
79 | if($result<0) |
80 | { |
81 | echo "error: ".$ping->strError($result); |
82 | $retval=1; |
83 | } |
84 | else |
85 | { |
86 | echo $result." ms"; |
87 | $retval=0; |
88 | } |
89 | return $retval; |
90 | } |
91 | ?> |