Group: comp.lang.tcl


Subject: Need some help on this script
From: Glenn Jackman
Date: 12/14/2007 7:53:40 PM
At 2007-12-14 01:07PM, "hphinizy" wrote: > I am relatively new to expect and I am trying to create a script to > add user accounts to some dis. I am utilizing sudo to invoke useradd > and the like... I am having a problem with sudo is once you auth for > one command sudo will not prompt you for you password the next. So, I > am attempting to build a script that would account for that. The > script seems to timeout at the "Password:" prompt for the useradd > statement however, the user account gets created... Problem is the > pasword does not get changed to the generic password with the passwd > command later in the script. > > #!/usr/bin/expect -f [...] > set admin xxxxxxxx > set adminpw xxxxxxxx > set rootpw xxxxxxxx [...] > set timeout 1 > spawn $env(SHELL) > match_max 100000 > set list [exec cat add_users_servers] That's shell-ish but not tcl-ish. I'd write: set filename $add_users_servers if {[catch {open $filename r} fid] != 0} { error "can't open $filename: $fid" } set servers [split [read -nonewline $fid] \n] close $fid > foreach server $list { > > send -- "$server\r" What is in $server? Is it a shell command? What do you expect to see after issuing this command? Recall that your timeout is 1 second, so does "$server" happend quickly enough? > send "sudo /usr/sbin/useradd <some_user>\r" > expect { > "Password:" {send "$adminpw\r"} here, you probably want "Password:" { send "$adminpw\r" exp_continue } > "]$" > } Do the characters ']' and '$' appear in your prompt? If you intended that to be a regular expression, write: -re "\]$" > send -- "sudo /usr/bin/passwd <some_user>\r" > expect { > "password:" {send "generic\r"} > } > expect { > "password:" {send "generic\r"} > } > expect { > "successfully." {send "exit\r"} > } I'd write the above as send -- "sudo /usr/bin/passwd <some_user>\r" expect { -re {password: *$} { send "generic\r" exp_continue } "successfully." } send "exit\r" > expect "closed" > puts "done with '$server'." > > > I know I am missing something here that is simple... Moreover, I am > also sure there are pre-canned scripts out there that do what I want > here? Am I reinventing the wheel? > > Thanks, > > H -- Glenn Jackman "You can only be young once. But you can always be immature." -- Dave Barry