/[drupal]/contributions/modules/autopilot/ssh_cli.php
ViewVC logotype

Contents of /contributions/modules/autopilot/ssh_cli.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Thu Oct 9 21:35:48 2008 UTC (13 months, 2 weeks ago) by souvent22
Branch: MAIN
CVS Tags: DRUPAL-5--2-0-BETA4, DRUPAL-5--2-0-BETA3, DRUPAL-5--1-4, DRUPAL-5--1-3, HEAD
Changes since 1.1: +0 -1 lines
File MIME type: text/x-php
Patching WSOD issue.
1 <?php
2 /**
3 * Usage:
4 * -u username
5 * -p password
6 * --port port
7 * --local local path
8 * --remote remote path
9 * -c Command which can be one of:
10 * * scp
11 * * scp_pull
12 * CLI Version to get around the error in the APACHE currently
13 * that will not allow this extension to be loaded.
14 */
15
16 # Setup and Input parsing
17 $args = $argv;
18 unset($args[0]);
19 $input = parseArguments($args);
20 if(isset($input['command'])) {
21 $input['command'] = base64_decode($input['command']);
22 }
23
24
25 # Create Object
26 $shell = new SSH2($input['host'], $input['port']);
27 $shell->authPassword($input['username'], $input['password']);
28
29 # Look up the command
30 $lookup_cmd = $input['action'];
31 if($lookup_cmd == 'scp') {
32 $shell->pushToRemote($input['local'], $input['remote']);
33 }
34 else if($lookup_cmd == 'scp_pull') {
35 $output = $shell->pullFromRemote($input['remote'], $input['local']);
36 print $output;
37 }
38 else {
39 $output = $shell->cmdExec($input['command']);
40 print $output;
41 unset($shell);
42 }
43 exit();
44
45 // ssh protocols
46 // note: once openShell method is used, cmdExec does not work
47
48 class SSH2 {
49
50 private $host = 'host';
51 private $user = 'user';
52 private $port = '22';
53 private $password = 'password';
54 private $con = null;
55 private $shell_type = 'bash';
56 private $shell = null;
57 private $logs = array();
58 private $connection_open = false;
59
60 function __construct($host='', $port='' ) {
61 if (!extension_loaded('ssh2')) {
62 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
63 dl('php_ssh2.dll');
64 } else {
65 dl('ssh2.so');
66 }
67 }
68 if( $host!='' ) $this->host = $host;
69 if( $port!='' ) $this->port = $port;
70
71 $this->con = ssh2_connect($this->host, $this->port);
72 if( !$this->con ) {
73 $this->connection_open = false;
74 $this->throwFatalError("Connection failed to $this->host on $this->port");
75 }
76
77 }
78 function logger($msg) {
79 $this->logs[] = $msg;
80 }
81 function getLogs() {
82 return $this->logs;
83 }
84
85 function pushToRemote($local_path, $remote_path) {
86 if(AUTOPILOT_DEBUG_MODE === true) {
87 $logger =& LoggerManager::getLogger('pushToRemote');
88 $logger->debug("Pushing $local_path to $remote_path");
89 }
90
91 umask(0);
92
93 $try = ssh2_scp_send($this->con, $local_path, $remote_path, 0777);
94 if(!$try) {
95 $this->throwFatalError("Channel SCP: Unable to SCP Copy: [local] $local_path, [remote] $remote_path");
96 }
97 else {
98 #$logger->info("Copy sucessful...");
99 }
100 return;
101 }
102
103 function pullFromRemote($remote_path, $local_path) {
104 if(AUTOPILOT_DEBUG_MODE === true) {
105 $logger =& LoggerManager::getLogger('pullFromRemote');
106 $logger->debug("Pushing $local_path to $remote_path");
107 }
108 umask(0);
109 $try = ssh2_scp_recv($this->con, $remote_path, $local_path);
110 if(!$try) {
111 $this->throwFatalError("Channel SCP: Unable to SCP Pull to $local_path from $remote_path");
112 }
113 else if(AUTOPILOT_DEBUG_MODE === true) {
114 $logger->info("Pull sucessful");
115 }
116 return;
117 }
118
119 function isConnectionOpen() {
120 return $this->connection_open;
121 }
122
123 function authPassword( $user = '', $password = '' ) {
124
125 if( $user!='' ) $this->user = $user;
126 if( $password!='' ) $this->password = $password;
127
128 if( !ssh2_auth_password( $this->con, $this->user, $this->password ) ) {
129 $this->logger("Authorization failed for $this->user");
130 $this->throwFatalError("Authorization failed for $this->user");
131 }
132
133 }
134
135 function openShell( $shell_type = '' ) {
136 if ( $shell_type != '' ) {
137 $this->shell_type = $shell_type;
138 }
139
140 $this->shell = ssh2_shell( $this->con, $this->shell_type );
141
142 if( !$this->shell ) {
143 $this->throwFatalError("Shell connection failed");
144 }
145 return;
146
147 }
148
149 function writeShell( $command = '' ) {
150 fwrite($this->shell, $command."\n")
151 or
152 $this->throwFatalError("Error while writing to shell");
153 }
154
155 function cmdExec($cmd) {
156 $cmd = "echo '[start]';$cmd;echo '[end]'";
157 $stream = ssh2_exec( $this->con, $cmd, false );
158 stream_set_blocking( $stream, true );
159 $contents = stream_get_contents($stream);
160 //fclose($stream);
161 return $contents;
162
163 }
164
165 public function getLog() {
166 return implode("\n", $this->getLogs());
167 }
168
169 /**
170 * The CLIFatalError echos the
171 * error message and then exits
172 * with an exit code other than 0.
173 * TODO: Perhaps match up error code
174 * types?
175 */
176 public function CLIFatalError($msg = null) {
177 if($msg !== null) {
178 $msg = implode("\n", $this->getLogs());
179 }
180 ob_end_clean();
181 print $msg;
182 ob_flush();
183 exit(1);
184 }
185
186 public function throwFatalError($msg) {
187 $this->CLIFatalError($msg);
188 }
189
190 }
191
192 function parseArguments($argv) {
193 $parsed = array();
194 foreach ($argv as $arg) {
195 if (preg_match('#^-{1,2}([a-zA-Z0-9]*)=?(.*)$#', $arg, $matches)) {
196 $key = $matches[1];
197 switch ($matches[2]) {
198 case '':
199 case 'true':
200 $arg = true;
201 break;
202 case 'false':
203 $arg = false;
204 break;
205 default:
206 $arg = $matches[2];
207 }
208
209 /* make unix like -afd == -a -f -d */
210 if(preg_match("/^-([a-zA-Z0-9]+)/", $matches[0], $match)) {
211 $string = $match[1];
212 for($i=0; strlen($string) > $i; $i++) {
213 $parsed[$string[$i]] = true;
214 }
215 } else {
216 $parsed[$key] = $arg;
217 }
218 } else {
219 $parsed['input'][] = $arg;
220 }
221 }
222 return $parsed;
223 }
224

  ViewVC Help
Powered by ViewVC 1.1.2