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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
| <?php
class Telnet
{
var $sock = NULL;
function telnet($host, $port)
{
$this->sock = fsockopen($host, $port);
socket_set_timeout($this->sock,2,0);
}
function close()
{
if ($this->sock)
fclose($this->sock);
$this->sock = NULL;
}
function write($buffer)
{
$buffer = str_replace(chr(255),chr(255).chr(255), $buffer);
fwrite($this->sock, $buffer);
}
function getc()
{
return fgetc($this->sock);
}
function read_till($what)
{
$what2 = "---- More ----";
$bufs = '';
while (true) {
$buf = $this->read_till_do($what);
$bufs .= $buf;
if ($what == (substr($buf, strlen($buf) - strlen($what)))) {
$bufs = substr($bufs, 0, -strlen($what));
break;
}
if (strpos($buf, $what2) === false)
break;
if ($what2 == (substr($buf, strlen($buf) - strlen($what2))))
$bufs = substr($bufs, 0, -strlen($what2));
$this->write(" ");
}
return $bufs;
}
function read_till_do($what)
{
$IAC = chr(255);
$DONT = chr(254);
$DO = chr(253);
$WONT = chr(252);
$WILL = chr(251);
$theNULL = chr(0);
$buf = '';
while (1)
{
$c = $this->getc();
if ($c === false)
return $buf;
if ($c == $theNULL)
continue;
// if ($c == "1")
// continue;
if ($c != $IAC) {
$buf .= $c;
if ($what == (substr($buf, strlen($buf) - strlen($what)))) {
return $buf;
} else {
continue;
}
}
$c = $this->getc();
if ($c == $IAC) {
$buf .= $c;
} elseif ($c == $DO) {
$opt = $this->getc();
// echo "we wont " . ord($opt) . "\n";
fwrite($this->sock, $IAC.$WONT.$opt);
} elseif ($c == $DONT) {
$opt = $this->getc();
// echo "we wont " . ord($opt) . "\n";
fwrite($this->sock, $IAC.$WONT.$opt);
} elseif ($c == $WILL) {
$opt = $this->getc();
// echo "we dont " . ord($opt) . "\n";
fwrite($this->sock, $IAC.$DONT.$opt);
} elseif ($c == $WONT) {
$opt = $this->getc();
// echo "we dont " . ord($opt) . "\n";
fwrite($this->sock, $IAC.$DONT.$opt);
} else {
// echo "where are we? c=" . ord($c) . "\n";
}
}
}
}
$telnet = new telnet($ip, 23);
$msg = $telnet->read_till("Password:");
$telnet->write("abc123\r\n");
$msg = $telnet->read_till("<H3C>");
$telnet->write("display cu\r\n");
$msg = $telnet->read_till("<H3C>");
$telnet->write("display vlan all\r\n");
$msg = $telnet->read_till("<H3C>");
$telnet->write("display interface Vlan-interface\r\n");
$msg = $telnet->read_till("<H3C>");
$telnet->write("display version\r\n");
$msg = $telnet->read_till("<H3C>");
$telnet->write("display device\r\n");
$msg = $telnet->read_till("<H3C>");
$telnet->write("display device manuinfo\r\n");
$msg = $telnet->read_till("<H3C>");
$telnet->write("display ip routing-table\r\n");
$msg = $telnet->read_till("<H3C>");
$telnet->write("display arp\r\n");
$msg = $telnet->read_till("<H3C>");
$telnet->write("display mac-address\r\n");
$msg = $telnet->read_till("<H3C>");
|