Posted to tcl by rahl at Mon Nov 14 13:15:09 GMT 2022view raw
- #!/bin/expect -f
- if {[llength $argv] != 3} {
- exit 1
- }
- set user [lindex $argv 0]
- set passwd [lindex $argv 1]
- set command [lindex $argv 2]
- set fp [open "$passwd" r]
- set user_pass [lindex $data 0]
- close $fp
- # The problem appears to be down to passing a potentially layered command
- # e.g. ssh -t <server> uname leads to tcl (expect) error:
- # "sudo: ssh -t <server> uname: command not found"
- spawn sudo -u $user $command
- expect {
- # TODO: Currently specific to ssh, but should handle local sudo as well
- -re "Enter passphrase for key.*" {
- send "${user_pass}\r"
- }
- }
- expect eof
- foreach {pid spawnid os_error_flag value} [wait] break
- #lassign [wait] pid spawnid os_error_flag value
- if {$os_error_flag == 0} {
- puts "exit status: $value"
- } else {
- puts "errno: $value"
- }
- exit $value