Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

expect passwd (password change script using expect)

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I am looking for a script that can automate password changes in aix. I am new to expect. As of this writing I have not yet compiled it. If there is an expect expert out there here's what I want to do.

I want to be able to read a config file in as arguments to passwd. Such as

#Config file
userid current_passwd new_passwd

#Script
passwd $userid
password: $current_passwd
new password: $new_passwd
new password: $new_passwd

You get the idea? If I could write it, I wouldn't be here.

Thanx for any help.
hwaynejones@yahoo.com
 
I picked this up, however can't verify it.
Good luck anyway!



#!/usr/local/expect/bin/expect

if { $argc != 3 } {
puts "Usage:"
puts "$argv0 username oldpwd newpwd"
exit
}

#set machine [lindex $argv 0]
set username [lindex $argv 0]
set oldpwd [lindex $argv 1]
set newpwd [lindex $argv 2]
set completed_list " "

#-----------------------------------------
# All machines where set password on
#-----------------------------------------
set machine_list {
machine1
machine2
machine3
etc
etc
}


#----------------------------------------------------
# Ping machines to see which ones are available
# ok machines in run_list
#----------------------------------------------------
foreach machine $machine_list {
if { [ catch { exec ping -c 1 $machine } ] == 0 } {
lappend run_list $machine
}
}


proc exclude_list {} {
set count 0
global machine_list run_list
foreach machine $machine_list {
incr count
if { [ lsearch $run_list $machine ] < 0 } {
puts &quot;\t$count)\t$machine&quot;
}
}
puts &quot;&quot;
}

proc include_list {} {
puts &quot;Will now try to change password on following machines:&quot;
set count 0
global machine_list run_list
foreach machine $run_list {
incr count
puts &quot;\t$count)\t$machine&quot;
}
puts &quot;&quot;
}

proc continue_chk {} {
global run_list
while {1} {
include_list
puts -nonewline &quot;\nDo you want to continue or (m)odify list? (y/n/m): &quot;
gets stdin ans
if { 1 == [regexp {^y|Y$} $ans] } {
puts &quot;yes&quot;
break
} elseif { 1 == [regexp {^m|M$} $ans] } {
puts -nonewline &quot;\n Enter number to remove: &quot;
gets stdin ans
set r_num [expr {$ans - 1}]
puts $run_list
set run_list [ lreplace $run_list $r_num $r_num ]
} else {
puts &quot;no&quot;
exit
}
}
}

puts &quot;Failed to ping following machines:&quot;
exclude_list

#puts &quot;Will now try to change password on following machines:&quot;
#include_list

continue_chk

foreach machine $run_list {
set change 0
spawn telnet $machine
while {1} {
expect {
timeout break
&quot;\rlogin:&quot; { sleep 1
send &quot;${username}\r&quot; }
&quot;New password&quot; { send &quot;${newpwd}\r&quot;
lappend completed_list $machine
set change 1 }
&quot;new password&quot; { send &quot;${newpwd}\r&quot;
set change 1 }
&quot;Old password:&quot; { send &quot;${oldpwd}\r&quot; }
&quot;Password:&quot; { send &quot;${oldpwd}\r&quot; }
&quot;\\\$&quot; { if {$change == 0} {
send &quot;passwd\r&quot;
set change 1
} else {
send &quot;exit\r&quot; }
}
&quot;changed&quot; { send &quot;exit\r&quot; }
&quot;closed&quot; { break }
}
sleep 1
}
}

puts &quot; &quot;
puts &quot;Password changed on following machines:&quot;
foreach machine $completed_list {
puts $machine
}

puts &quot; &quot;
puts &quot;Password NOT changed on following machines:&quot;
foreach machine $machine_list {
if { [ lsearch $completed_list $machine ] < 0 } {
puts $machine
}
}
exit
 
Winston,

Well found. If this script works you are an official Tek-Tips god! This had got to be one of the most frequently asked questions on TT. If you can test it (or if anyone else reading can) you should make it into a FAQ.

Regards, Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top