-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh2.php
More file actions
126 lines (107 loc) · 4.68 KB
/
ssh2.php
File metadata and controls
126 lines (107 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2007-2017 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDTool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
function create_ssh($deviceid) {
$dbquery = db_fetch_row_prepared("SELECT description, hostname, login, password FROM host WHERE id=?", array($deviceid));
if( $dbquery === false ){
return false; // no host to connect to
}
// look for the login/password on the device, or take the default one
$account=array();
if(empty($dbquery['login'])) {
$account['login'] = read_config_option('ciscotools_default_login');
$account['password'] = read_config_option('ciscotools_default_password');
} else {
$account['login'] = $dbquery['login'];
$account['password'] = $dbquery['password'];
}
extdb_log( 'Login prompt: '. $account['login']);
// open the ssh stream to the device
$stream = open_ssh($dbquery['hostname'], $account['login'], $account['password']);
if($stream !== false){
$data = ssh_read_stream($stream );
if( $data === false ){
extdb_log( 'Erreur can\'t read login prompt');
return false;
}
}
return $stream;
}
function open_ssh( $hostname, $username, $password ) {
$connection = @ssh2_connect($hostname, 22);
if($connection === false ) {
cacti_log( "can't open SSH session to ".$hostname, false, 'CISCOTOOLS');
return false;
}
if( !@ssh2_auth_password($connection, $username, $password) ) {
cacti_log( "can't login to host ".$hostname." via SSH session, log: ".$username, false, 'CISCOTOOLS');
return false;
}
$stream = @ssh2_shell($connection, 'vt100', null, 80, 24, SSH2_TERM_UNIT_CHARS );
stream_set_timeout($stream, 210);
stream_set_blocking($stream, true);
return $stream;
}
function close_ssh($connection) {
@ssh2_disconnect ($connection);
}
function ssh_read_stream($stream, $term='#', $timeout=210 ) {
$oldtimeout = 210;
stream_set_timeout($stream, $timeout);
$output = '';
if( $stream == null ) return false;
do {
$stream_out = @fread ($stream, 1);
extdb_log('stream read: >'.$stream_out.'<('.strlen($stream_out).')'.' hex:'.bin2hex($stream_out));
// Timeout occured
if( $stream_out === false ){
extdb_log('Timeout on ssh fread');
break;
}
$output .= $stream_out;
// if the terminal is waiting to go for the next screen, just issue a space to go one
if( strpos($output, "--More--" ) !== false ) {
ssh_write_stream($stream, ' ' );
// then remove it from stream
$output = str_replace( "--More--", "", $output );
}
} while ( !feof($stream) && $stream_out !== false && $stream_out != $term);
extdb_log('stream read out: '.print_r($output, true));
stream_set_timeout($stream, $oldtimeout);
if(strlen($output)!=0) {
return $output;
}
extdb_log('ssh_read_stream - Error - No output');
return false;
}
function ssh_write_stream( $stream, $cmd, $timeout=210){
if( $stream == null ) return;
$oldtimeout = 210;
stream_set_timeout($stream, $timeout);
do {
$write = fwrite( $stream, $cmd."\r\n" );
} while( $write < strlen($cmd) );
extdb_log('ssh_write_stream: '.$cmd .' ('.strlen($cmd).')');
stream_set_timeout($stream, $oldtimeout);
}
?>