Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / bin / pon
1 #!/usr/bin/expect --
2 ################################################################################
3 #
4 # File:         pon
5 # Description:  Connect to a Salira PON
6 # Author:       Andrew@DeFaria.com
7 # Created:      Fri Jun  6 15:37:15 PDT 2003
8 # Language:     Tcl
9 #
10 # (c) Copyright 2003, Andrew@DeFaria.com, all rights reserved
11 #
12 ################################################################################
13 global argv
14 global spawn_id
15
16 # Reporting procedures
17 proc Error {args} {
18   send_user "ERROR: [join $args]\n"
19   
20   if {[info exist spawn_id]} {
21     close $spawn_id
22   }
23
24   exit 1;
25 } ;# Error
26
27 proc Display {args} {
28   send_user "[join $args]\n"
29 } ;# Display
30
31 proc Login {to_machine {username root} {password root}} {
32   # Establish connection
33   spawn "telnet" $to_machine
34
35   # Look for Login prompt
36   expect {
37     "Login:" {
38     }
39     "login:" {
40       Error "Host does not appear to be a Salira PON"
41     }
42     "Unknown host" {
43       Error "Machine $to_machine does not exist!"
44     }
45     timeout {
46       Error "$to_machine is not responding"
47     }
48   }
49
50   send $username\r
51   expect {
52     "Password:" {
53       send $password\r
54     }
55     timeout {
56       Error "Password prompt not issued"
57     }
58   }
59
60   expect {
61     "\# " {
62       puts -nonewline "Logged into $to_machine"
63       puts -nonewline $expect_out(buffer)
64     }
65     timeout {
66       Error "$to_machine appears to be dead"
67     }
68   }
69
70   return $spawn_id
71 }
72
73 # Main
74 log_user 0
75
76 set machine [lindex $argv 0]
77
78 if {![info exist machine] || $machine == ""} {
79   Error "You must specify a machine to log into"
80 }
81
82 set spawn_id [Login $machine]
83
84 log_user 1
85
86 interact