| 1 | # | ||
| 2 | # $Id: telnet.pm,v 1.8 2003/03/02 11:12:10 dsw Exp $ | ||
| 3 | # | ||
| 4 | # COPYRIGHT AND LICENSE | ||
| 5 | # Copyright (c) 2001-2003, Juniper Networks, Inc. | ||
| 6 | # All rights reserved. | ||
| 7 | # Redistribution and use in source and binary forms, with or without | ||
| 8 | # modification, are permitted provided that the following conditions are | ||
| 9 | # met: | ||
| 10 | # 1. Redistributions of source code must retain the above | ||
| 11 | # copyright notice, this list of conditions and the following | ||
| 12 | # disclaimer. | ||
| 13 | # 2. Redistributions in binary form must reproduce the above | ||
| 14 | # copyright notice, this list of conditions and the following disclaimer | ||
| 15 | # in the documentation and/or other materials provided with the | ||
| 16 | # distribution. | ||
| 17 | # 3. The name of the copyright owner may not be used to | ||
| 18 | # endorse or promote products derived from this software without specific | ||
| 19 | # prior written permission. | ||
| 20 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
| 21 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| 22 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| 23 | # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | ||
| 24 | # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
| 25 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 26 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 27 | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 28 | # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | ||
| 29 | # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 30 | # POSSIBILITY OF SUCH DAMAGE. | ||
| 31 | # | ||
| 32 | |||
| 33 | package JUNOS::Access::telnet; | ||
| 34 | |||
| 35 | use vars qw( @ISA $shell_bad_command $state_none $state_shellsh | ||
| 36 | $login_incorrect $prompt_login | ||
| 37 | $prompt_password $prompt_shell $prompt_cli $state_failed $state_login | ||
| 38 | $state_password $state_done $state_cli $state_junoscript | ||
| 39 | $cli_bad_command $unknown_host $command_not_found | ||
| 40 | $command_not_found2 ); | ||
| 41 | use JUNOS::Trace; | ||
| 42 | use JUNOS::Access; | ||
| 43 | @ISA = qw(JUNOS::Access); | ||
| 44 | |||
| 45 | # First one is FreeBSD second on is Solaris | ||
| 46 | $unknown_host = "No address associated with hostname|Unknown host"; | ||
| 47 | $command_not_found = "junoscript: .*not found"; | ||
| 48 | $command_not_found2 = "No such file or directory"; | ||
| 49 | $cli_bad_command = "syntax error, expecting <command>"; | ||
| 50 | $cli_unknown_command = "unknown command"; | ||
| 51 | $shell_bad_command = ": .*not found"; | ||
| 52 | $login_incorrect = "Login incorrect"; | ||
| 53 | |||
| 54 | $state_shellsh = 'sh shell'; | ||
| 55 | $state_xmlmode = 'xml mode'; | ||
| 56 | $state_failed = 'failed'; | ||
| 57 | $state_login = 'login'; | ||
| 58 | $state_password = 'password'; | ||
| 59 | $state_cli = 'cli'; | ||
| 60 | $state_clitest = 'cli test'; | ||
| 61 | $state_junoscript = 'junoscript'; | ||
| 62 | $state_done = 'done'; | ||
| 63 | $state_none = 'none'; | ||
| 64 | |||
| 65 | $prompt_login = 'login:'; | ||
| 66 | $prompt_password = 'Password:'; | ||
| 67 | $prompt_shell = '[$#]$'; | ||
| 68 | $prompt_cli = '>.*start shell sh'; | ||
| 69 | |||
| 70 | sub start | ||
| 71 | { | ||
| 72 | my($self) = @_; | ||
| 73 | $self->{Leader} = ''; | ||
| 74 | |||
| 75 | # state maintenance for login process | ||
| 76 | $self->{'login_state'} = $state_login; | ||
| 77 | $self->{'login_last_state'} = $state_none; | ||
| 78 | |||
| 79 | # properties specific to telnet | ||
| 80 | $self->{'password'} = $self->{'password'} || ""; | ||
| 81 | |||
| 82 | $self->start_command( "telnet -l " . $self->{login} . " " . | ||
| 83 | $self->{hostname}); | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | ### partial diagram of state machine implemented in incoming | ||
| 88 | # | ||
| 89 | # [state_login] | ||
| 90 | # | | ||
| 91 | # | | ||
| 92 | # |<---(bad command|bad host)---------+ | ||
| 93 | # | | | ||
| 94 | # | | | ||
| 95 | # | (wait for 'login:', send login) | ||
| 96 | # | | | ||
| 97 | # | | | ||
| 98 | # | V | ||
| 99 | # | [state_password] | ||
| 100 | # | | | ||
| 101 | # | | | ||
| 102 | # | (wait for 'Password:', send password) | ||
| 103 | # | | | ||
| 104 | # | | | ||
| 105 | # | V | ||
| 106 | # | [state_cli] | ||
| 107 | # | | | ||
| 108 | # | | | ||
| 109 | # |<---(got bad login)----------------+ | ||
| 110 | # | | | ||
| 111 | # | | | ||
| 112 | # | (send 'start') | ||
| 113 | # | | | ||
| 114 | # | | | ||
| 115 | # | [state_clitest] | ||
| 116 | # | | | ||
| 117 | # | | | ||
| 118 | # | V---(bad command from cli)------+-----(bad command from shell)-----V | ||
| 119 | # | | | | ||
| 120 | # | | | | ||
| 121 | # | (send 'xml-mode no-echo\n') [state_shell] | ||
| 122 | # | | | | ||
| 123 | # | | | | ||
| 124 | # | +-------------------------------+ | | ||
| 125 | # | | (send 'sh; stty -echo; | ||
| 126 | # | [state_xmlmode] junoscript) | ||
| 127 | # | | | | ||
| 128 | # | | | | ||
| 129 | # | | | | ||
| 130 | # | V----(bad command from cli)---+----(success)---------------------+ | ||
| 131 | # | | | | ||
| 132 | # | | | | ||
| 133 | # | +-----------------------------+ | | ||
| 134 | # | | | | ||
| 135 | # | (send 'start shell sh\n') | | ||
| 136 | # | | | | ||
| 137 | # | | | | ||
| 138 | # | V | | ||
| 139 | # | [state_shellsh]<--------------+ | | ||
| 140 | # | | | | | ||
| 141 | # | | | | | ||
| 142 | # |<---(bad command from cli)---------+---------(bad command from unix, | | ||
| 143 | # | | send 'sh') | | ||
| 144 | # | | | | ||
| 145 | # | (wait for '$' or '#', | | ||
| 146 | # | send 'stty -echo; junoscript') | | ||
| 147 | # | | | | ||
| 148 | # | |<---------------------------------+ | ||
| 149 | # | V | ||
| 150 | # | [state_junoscript]<-------------+ | ||
| 151 | # | | | | ||
| 152 | # | | | | ||
| 153 | # |<---(bad command)------------------+--------(cli prompt followed by | ||
| 154 | # | | 'start shell sh', | ||
| 155 | # | | send 'stty -echo; junoscript') | ||
| 156 | # V V | ||
| 157 | #{{failed}} {{ in xml, JUNOS::Device takes over from now on) }} | ||
| 158 | # | ||
| 159 | ### | ||
| 160 | %state_table = ( | ||
| 161 | $state_login => \&login_action, | ||
| 162 | $state_password => \&password_action, | ||
| 163 | $state_cli => \&cli_action, | ||
| 164 | $state_clitest => \&clitest_action, | ||
| 165 | $state_xmlmode => \&xmlmode_action, | ||
| 166 | $state_shellsh => \&shellsh_action, | ||
| 167 | $state_junoscript => \&junoscript_action, | ||
| 168 | ); | ||
| 169 | |||
| 170 | sub login_action | ||
| 171 | { | ||
| 172 | ### STATE: logging in -- waiting for 'login:' | ||
| 173 | my($self, $original, $incoming) = @_; | ||
| 174 | # FAIL: bad host | ||
| 175 | if ($incoming =~ m/$unknown_host/s) { | ||
| 176 | $self->{JUNOS_Device}->report_error("Host [" . $self->{'hostname'} . "] not found : " . $incoming ); | ||
| 177 | return $state_failed; | ||
| 178 | } | ||
| 179 | # FAIL: bad command | ||
| 180 | if ($incoming =~ m/$command_not_found/s or | ||
| 181 | $incoming =~ m/$command_not_found2/s | ||
| 182 | ) { | ||
| 183 | $self->{JUNOS_Device}->report_error("Command not found " . $incoming ); | ||
| 184 | return($state_failed); | ||
| 185 | } | ||
| 186 | if ($incoming =~ m/$prompt_password/sm ) { | ||
| 187 | return (password_action($self, $original, $incoming)); | ||
| 188 | } | ||
| 189 | # login | ||
| 190 | if ($incoming =~ m/$prompt_login/ ) { | ||
| 191 | trace("IO", ref $self, "::incoming: sending login to ", $self->{hostname}); | ||
| 192 | $self->send($self->{'login'} . "\n"); | ||
| 193 | return ($state_password); | ||
| 194 | } | ||
| 195 | return; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub password_action | ||
| 199 | { | ||
| 200 | ### STATE: waiting for password prompt | ||
| 201 | my($self, $original, $incoming) = @_; | ||
| 202 | # Password: | ||
| 203 | if ($incoming =~ m/$prompt_password/ ) { | ||
| 204 | trace("IO", ref $self, "::incoming: sending password to ", $self->{hostname}); | ||
| 205 | $self->send( $self->{'password'} . "\n" ); | ||
| 206 | return($state_cli); | ||
| 207 | } | ||
| 208 | return; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub cli_action | ||
| 212 | { | ||
| 213 | ### STATE: waiting for cli prompt | ||
| 214 | my($self, $original, $incoming) = @_; | ||
| 215 | # FAIL: BAD LOGIN (from password) | ||
| 216 | if ($incoming =~ m/$login_incorrect/) { | ||
| 217 | $self->{JUNOS_Device}->report_error("Failed to login user " . $self->{'login'} . " : " . $incoming); | ||
| 218 | return($state_failed); | ||
| 219 | } | ||
| 220 | if ($original =~ /\n$/) { | ||
| 221 | return; | ||
| 222 | } | ||
| 223 | # Got a prompt | ||
| 224 | # Figure out if we are in shell or cli | ||
| 225 | trace("IO", ref $self, "::incoming: sending start command to cli"); | ||
| 226 | # Send the 'start' command just to see if we are in the cli or if we are | ||
| 227 | # the shell prompt | ||
| 228 | $self->send( "start\n" ); | ||
| 229 | return($state_clitest); | ||
| 230 | } | ||
| 231 | |||
| 232 | sub clitest_action | ||
| 233 | { | ||
| 234 | ### STATE: waiting for cli error | ||
| 235 | my($self, $original, $incoming) = @_; | ||
| 236 | ### If we get a cli bad command, we know we are in the cli - send a | ||
| 237 | # 'xml-mode no-echo' | ||
| 238 | if ($incoming =~ m/$cli_bad_command/ or $incoming =~ m/$cli_unknown_command/) { | ||
| 239 | trace("IO", ref $self, "::incoming: sending 'xml-mode no-echo' to the cli"); | ||
| 240 | $self->send( "xml-mode no-echo\n" ); | ||
| 241 | return($state_xmlmode); | ||
| 242 | } | ||
| 243 | # It was an UNIX shell | ||
| 244 | if ($incoming =~ m/$shell_bad_command/) { | ||
| 245 | trace("IO", ref $self, "::incoming: sending junoscript command to sh shell"); | ||
| 246 | # assume we got a bash or sh prompt, probably not in deploy config | ||
| 247 | $self->send( "sh\nstty -echo; junoscript\n" ); | ||
| 248 | return($state_junoscript); | ||
| 249 | } | ||
| 250 | return; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub xmlmode_action | ||
| 254 | { | ||
| 255 | ### STATE: xml mode action | ||
| 256 | my($self, $original, $incoming) = @_; | ||
| 257 | # FAIL: bad command (from 'xml-mode no-echo' at cli prompt) | ||
| 258 | # let's try old 'start shell' method | ||
| 259 | if ($incoming =~ m/$cli_bad_command/ or $incoming =~ m/$cli_unknown_command/) { | ||
| 260 | $self->send( "start shell sh\n" ); | ||
| 261 | return($state_shellsh); | ||
| 262 | } | ||
| 263 | return; | ||
| 264 | } | ||
| 265 | |||
| 266 | sub shellsh_action | ||
| 267 | { | ||
| 268 | ### STATE: shell action | ||
| 269 | my($self, $original, $incoming) = @_; | ||
| 270 | # FAIL: bad command (from 'start shell sh' at cli prompt) | ||
| 271 | if ($incoming =~ m/$cli_bad_command/ or $incoming =~ m/$cli_unknown_command/) { | ||
| 272 | $self->{JUNOS_Device}->report_error("CLI could not start sh shell (does user have shell permissions?): " . $incoming ); | ||
| 273 | return($state_failed); | ||
| 274 | } | ||
| 275 | # It was an UNIX shell | ||
| 276 | if ($incoming =~ m/$shell_bad_command/) { | ||
| 277 | trace("IO", ref $self, "::incoming: sending junoscript command to sh shell"); | ||
| 278 | # assume we got a bash or sh prompt, probably not in deploy config | ||
| 279 | $self->send( "sh\nstty -echo; junoscript\n" ); | ||
| 280 | return($state_junoscript); | ||
| 281 | } | ||
| 282 | if ($original =~ /\n$/) { | ||
| 283 | return; | ||
| 284 | } | ||
| 285 | if ($incoming =~ m/$prompt_shell/) { | ||
| 286 | # got sh shell prompt | ||
| 287 | trace("IO", ref $self, "::incoming: sending junoscript command to sh shell"); | ||
| 288 | # echo from telnet seems to confuse the bindings, use stty to hide the echo | ||
| 289 | $self->send( "stty -echo; junoscript\n" ); | ||
| 290 | return($state_junoscript); | ||
| 291 | } | ||
| 292 | return; | ||
| 293 | } | ||
| 294 | |||
| 295 | sub junoscript_action | ||
| 296 | { | ||
| 297 | ### STATE: handle junoscript failure to start | ||
| 298 | my($self, $original, $incoming) = @_; | ||
| 299 | # FAIL: bad command (from 'junoscript' at cli prompt) | ||
| 300 | if ($incoming =~ m/$cli_bad_command/ or $incoming =~ m/$cli_unknown_command/) { | ||
| 301 | $self->{JUNOS_Device}->report_error("CLI could not start junoscript at cli prompt: " . $incoming); | ||
| 302 | return($state_failed); | ||
| 303 | } | ||
| 304 | # FAIL: bad command | ||
| 305 | if ($incoming =~ m/$command_not_found/s or | ||
| 306 | $incoming =~ m/$command_not_found2/s | ||
| 307 | ) { | ||
| 308 | $self->{JUNOS_Device}->report_error("Command not found " . $incoming ); | ||
| 309 | return($state_failed); | ||
| 310 | } | ||
| 311 | # Very unusual. This means the motd is big and cli only got the | ||
| 312 | # start shell sh command. | ||
| 313 | if ($incoming =~ m/$prompt_cli/) { | ||
| 314 | trace("IO", ref $self, "::incoming: sending junoscript command to sh shell"); | ||
| 315 | # echo from telnet seems to confuse the bindings, use stty to hide the echo | ||
| 316 | $self->send( "stty -echo; junoscript\n" ); | ||
| 317 | return; | ||
| 318 | } | ||
| 319 | return; | ||
| 320 | } | ||
| 321 | |||
| 322 | sub incoming | ||
| 323 | { | ||
| 324 | tracept("IO"); | ||
| 325 | my($self, $incoming) = @_; | ||
| 326 | |||
| 327 | # 6 state machine for login | ||
| 328 | my $state = $self->{'login_state'}; | ||
| 329 | my $state_last = $self->{'login_last_state'}; | ||
| 330 | my $incoming_stripped = $incoming; | ||
| 331 | $incoming_stripped =~ s/\n/ /g; # delete newline | ||
| 332 | $incoming_stripped =~ s/\r//g; # delete carriage return | ||
| 333 | $incoming_stripped =~ s/^\s+//; # delete leading whitespaces | ||
| 334 | $incoming_stripped =~ s/\s+$//; # delete trailing whitespaces | ||
| 335 | |||
| 336 | trace("IO", "state = $state"); | ||
| 337 | trace("IO", "last state = $state_last"); | ||
| 338 | trace("IO", "new chunk = <<$incoming>>"); | ||
| 339 | |||
| 340 | my $ret = $state_table{$state}->($self, $incoming, $incoming_stripped); | ||
| 341 | $state = $ret if $ret; | ||
| 342 | |||
| 343 | # keep a record of the input.. | ||
| 344 | $self->{Leader} .= $incoming; | ||
| 345 | |||
| 346 | # store state | ||
| 347 | $self->{'login_last_state'} = $self->{'login_state'}; | ||
| 348 | $self->{'login_state'} = $state; | ||
| 349 | |||
| 350 | # if we failed, return undef | ||
| 351 | if( $state eq $state_failed ) { $self = undef; } | ||
| 352 | $self; | ||
| 353 | } | ||
| 354 | |||
| 355 | 1; | ||
| 356 | |||
| 357 | __END__ |