I have the script below working to read through the list of IPs and
login to each one. I have it passing the password to sudo properly.
However, I'm getting some weirdness with the output. It should be
passing something like:
sh -c 'echo \"HISTORY=5\" >> /etc/default/passwd'
However it keeps inserting ? characters, so what I get is more like:
sh -c 'echo \"HISTORY=5\" >> /etc/default/p?asswd'
I suspect my issue has to do with some fundamental misunderstanding on
my part about how to deal with spawning processes and subprocesses in
TCL/Expect. Please help if you can...what I want SEEMS like it should
be so trivial, yet I'm completely hung up here.
#!/bin/sh
# \
exec tclsh "$0" ${1+"$@"}
package require Expect
# Turn off echo logging to the user by default
exp_log_user 1
exp_internal 1
# This is the magic timeout variable used by Expect (time in seconds).
set timeout 10
# We want to operate very simply - force a dumb terminal mode
set env(TERM) dumb
set ::exp::winnt_debug 1
set timestamp [clock format [clock seconds]]
set ip [open ips.txt]
set exp_log_file login.log
# The default option settings
array set OPTS {
user "MYUSER"
password "MYPASS"
prompt "(%|#|>|\\$) $"
command "sudo chmod 777 /etc/default/passwd"
}
while {[gets $ip line] >=0 } {
set pid [spawn Plink.exe $line -l $OPTS(user) -pw $OPTS(password)]
set id $spawn_id
set logged_in 0
while {!$logged_in} {
# Handle SSH host key dialog
expect -re "(\[Yy\]es\/\[Nn\]o).*" {
exp_send "yes\r"
}
expect -re "(\[Yy\]\/\[Nn\]).*" {
exp_send "y\r"
}
expect -re $OPTS(prompt) {
set logged_in 1
}
}
if {$logged_in} {
# expect -re $OPTS(prompt)
# Run command with sudo
exp_send "$OPTS(command)\r"
expect -re "\[Pp\]assword:\r"
exp_send "$OPTS(password)\r"
exp_send "sh -c 'echo \"HISTORY=5\" >> /etc/default/passwd'\r"
exp_send "sudo chmod 444 /etc/default/passwd\r"
expect -re "\[Pp\]assword:\r"
exp_send "$OPTS(password)\r"
exp_send "tail /etc/default/passwd\r"
}
exp_close -i $id
exp_wait -i $id
}