Annotation of /alx-src/branches/alx-web-070/include/common-functions.php
Parent Directory | Revision Log
Revision 7070 -
(hide annotations)
(download)
Mon Sep 7 10:50:48 2015 UTC (9 years ago) by niro
File size: 2043 byte(s)
Mon Sep 7 10:50:48 2015 UTC (9 years ago) by niro
File size: 2043 byte(s)
created branch 'alx-web-070'
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 | niro | 3343 | global $socket_timeout; |
10 | niro | 2194 | |
11 | niro | 2417 | if (!function_exists("ssh2_connect")) die("sshdo(): function ssh2_connect doesn't exist. You must install the ssh2 module from pecl library!"); |
12 | niro | 2194 | |
13 | niro | 3343 | // check if port exists |
14 | $con = fsockopen( $ip, "22", $errono, $errstr, $socket_timeout); | ||
15 | if (!$con) | ||
16 | niro | 2021 | { |
17 | niro | 3343 | echo "sshdo(): socket failure,<br>could not connect to port 22<br>"; |
18 | niro | 2194 | } |
19 | else | ||
20 | { | ||
21 | niro | 3343 | // first off all close our open socket |
22 | fclose($con); | ||
23 | |||
24 | if(!($con = ssh2_connect($ip, "22"))) | ||
25 | niro | 2021 | { |
26 | niro | 3343 | echo "sshdo(): failure, could not connect<br>"; |
27 | niro | 2194 | } |
28 | else | ||
29 | { | ||
30 | niro | 3343 | if(!ssh2_auth_pubkey_file($con, $user, $pubkey_file, $privkey_file)) |
31 | niro | 2022 | { |
32 | niro | 3343 | echo "sshdo(): failure, could not login<br>"; |
33 | niro | 2194 | } |
34 | else | ||
35 | { | ||
36 | niro | 3343 | //echo "logged in\n"; |
37 | |||
38 | if(!($stream = ssh2_exec($con, "source /etc/profile; ".$cmd."; echo '__RETVAL__:'$?"))) | ||
39 | niro | 2021 | { |
40 | niro | 3343 | echo "sshdo(): failure, could not execute command '".$cmd."'<br>"; |
41 | niro | 2021 | } |
42 | niro | 3343 | 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 | niro | 2021 | } |
57 | niro | 2022 | } |
58 | niro | 2194 | } |
59 | niro | 2021 | |
60 | niro | 2194 | // 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 | niro | 2594 | sshdo('if [[ ! -z $(grep "^'.$var.'=" '.$file.') ]]; then sed -i -e "s|^\('.$var.'=\).*|\1\"'.$value.'\"|" '.$file.' ;else echo "'.$var.'=\"'.$value.'\"" >> '.$file.'; fi', $ip); |
70 | niro | 2194 | } |
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 | niro | 2021 | } |
84 | niro | 2194 | else |
85 | { | ||
86 | echo $result." ms"; | ||
87 | $retval=0; | ||
88 | } | ||
89 | return $retval; | ||
90 | } | ||
91 | niro | 2021 | ?> |