OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [expect/] [example/] [passmass] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
#!../expect --
2
# passmass: change password on many machines
3
# Synopsis: passmass host1 host2 host3 ....
4
# Don Libes - March 11, 1991
5
 
6
# Description: Change passwords on the named machines.
7
#
8
# You are prompted for old/new passwords.  (If you are changing root
9
# passwords and have equivalencing, the old password is not used and may be
10
# omitted.)
11
#
12
# Additional arguments may be used for fine tuning.  They affect all hosts
13
# which follow until another argument overrides.
14
#
15
#   -user       User whose password will be changed.  By default, the current
16
#               user is used.
17
#   -rlogin     Use rlogin to access host.  (default)
18
#   -telnet     Use telnet to access host.
19
#   -program    Next argument is taken as program to run to set password.
20
#               Default is "passwd".  Other common choices are "yppasswd" and
21
#               "set passwd" (e.g., VMS hosts).
22
#   -prompt     Next argument is taken as a prompt suffix pattern.  This allows
23
#               the script to know when the shell is prompting.  The default is
24
#               "# " for root and "% " for non-root accounts.
25
#   -timeout    Next argument is number of seconds to wait for responses.
26
#               Default is 30 but some systems can be much slower logging in.
27
 
28
# The best way to run this is to put the command in a one-line shell script
29
# or alias. (Presumably, the set of hosts and parameters will rarely change.)
30
# Then run it whenever you want to change your passwords on all the hosts.
31
 
32
exp_version -exit 5.0
33
 
34
if $argc==0 {
35
        send_user "usage: $argv0 host1 host2 host3 . . .\n"
36
        exit
37
}
38
 
39
expect_before -i $user_spawn_id \003 exit
40
 
41
proc badhost {host emsg} {
42
        global badhosts
43
 
44
        send_user "\r\n\007password not changed on $host - $emsg\n\n"
45
        if 0==[llength $badhosts] {
46
                set badhosts $host
47
        } else {
48
                set badhosts [concat $badhosts $host]
49
        }
50
}
51
 
52
# set defaults
53
set login "rlogin"
54
set program "passwd"
55
set user [exec whoami]
56
 
57
set timeout 1000000
58
stty -echo
59
send_user "old password: "
60
expect_user -re "(.*)\n"
61
send_user "\n"
62
set oldpassword $expect_out(1,string)
63
send_user "new password: "
64
expect_user -re "(.*)\n"
65
send_user "\n"
66
set newpassword $expect_out(1,string)
67
send_user "retype new password: "
68
expect_user -re "(.*)\n"
69
set newpassword2 $expect_out(1,string)
70
send_user "\n"
71
stty echo
72
trap exit SIGINT
73
 
74
if ![string match $newpassword $newpassword2] {
75
        send_user "mismatch - password unchanged\n"
76
        exit
77
}
78
 
79
 
80
#send_user "want to see new password you just typed? (y|n) "
81
#expect_user "*\n"
82
#
83
#if [string match "y" [lindex $expect_match 0 c]] {
84
#       send_user "password is <$newpassword>\nproceed? (y|n) "
85
#       expect_user "*\n"
86
#       if ![string match "y" [lindex $expect_match 0 c]] exit
87
#}
88
 
89
set timeout 30
90
set badhosts {}
91
for {set i 0} {$i<$argc} {incr i} {
92
 
93
        set arg [lindex $argv $i]
94
        switch -- $arg \
95
                "-user" {
96
                        incr i
97
                        set user [lindex $argv $i]
98
                        continue
99
                } "-prompt" {
100
                        incr i
101
                        set prompt [lindex $argv $i]
102
                        continue
103
                } "-rlogin" {
104
                        set login "rlogin"
105
                        continue
106
                } "-telnet" {
107
                        set login "telnet"
108
                        continue
109
                } "-program" {
110
                        incr i
111
                        set program [lindex $argv $i]
112
                        continue
113
                } "-timeout" {
114
                        incr i
115
                        set timeout [lindex $argv $i]
116
                        continue
117
                }
118
 
119
        set host $arg
120
        if [string match $login "rlogin"] {
121
                set pid [spawn rlogin $host -l $user]
122
        } else {
123
                set pid [spawn telnet $host]
124
                expect -re "(login|Username):.*" {
125
                        send "$user\r"
126
                }
127
        }
128
 
129
        if ![info exists prompt] {
130
                if [string match $user "root"] {
131
                        set prompt "# "
132
                } else {
133
                        set prompt "(%|\\\$) "
134
                }
135
        }
136
 
137
        set logged_in 0
138
        for {} 1 {} {
139
                expect "Password*" {
140
                        send "$oldpassword\r"
141
                } eof {
142
                        badhost $host "spawn failed"
143
                        break
144
                } timeout {
145
                        badhost $host "could not log in (or unrecognized prompt)"
146
                        exec kill $pid
147
                        expect eof
148
                        break
149
                } -re "incorrect|invalid" {
150
                        badhost $host "bad password or login"
151
                        exec kill $pid
152
                        expect eof
153
                        break
154
                } -re $prompt {
155
                        set logged_in 1
156
                        break
157
                }
158
        }
159
 
160
        if (!$logged_in) {
161
                wait
162
                continue
163
        }
164
 
165
        send "$program\r"
166
        expect "Old password*" {
167
                send "$oldpassword\r"
168
                expect "Sorry*" {
169
                        badhost $host "old password is bad?"
170
                        continue
171
                } "password:"
172
        } -re "(n|N)ew password:"
173
        send "$newpassword\r"
174
        expect -re "not changed|unchanged" {
175
                badhost $host "new password is bad?"
176
                continue
177
        } -re "(password|Verification|Verify|again):.*"
178
        send "$newpassword\r"
179
        expect -re "(not changed|incorrect|choose new).*" {
180
                badhost $host "password is bad?"
181
                continue
182
        } -re "$prompt"
183
        send_user "\n"
184
 
185
        close
186
        wait
187
}
188
 
189
if [llength $badhosts] {send_user "\nfailed to set password on $badhosts\n"}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.