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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using "passwd" in a perl script

Status
Not open for further replies.

m1bzm

ISP
Mar 6, 2001
31
0
0
GB
Hi all.

Im writing an ISP automation package that controls all aspect of being an ISP. Ive just hit a small problem that someone might be able to help me with :)

Im looking for some examples of perl scripts that use the "passwd" function to change a users password. I want to run a command line script something like:

./passchange.pl username password

If you look at the man pages the passwd theres no obvious way of building it into a script.

Any ideas anyone? Thanks

Darren
 
An easy way to do this is with expect.

#!/usr/bin/expect(or wherever)
set timeout -1
log_user 0
set user [lindex $argv 0]
set pass [lindex $argv 1]

proc new_pass {} {
global user pass

if {![catch {spawn -noecho passwd $user} err]} {
set id $spawn_id

expect {
-re "New (P|p)assword.*" {
send "$pass\r"
#fast machines need to sleep 1s
expect -re "(.*) (P|p)assword.*" {
send "$pass\r"
}
}
}
} else {
puts "$err at $id."
return
}
}

catch [list new_pass] err2
if {[info exists $err2]} {
puts "$err2\n"
exit
}

I know this is not perl, BUT it is very simple to use
expect for automating interactive processes.
It was what expect was written for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top