Posted to tcl by rahl at Mon Nov 14 13:15:09 GMT 2022view raw

  1. #!/bin/expect -f
  2.  
  3. if {[llength $argv] != 3} {
  4. exit 1
  5. }
  6.  
  7. set user [lindex $argv 0]
  8. set passwd [lindex $argv 1]
  9. set command [lindex $argv 2]
  10.  
  11. set fp [open "$passwd" r]
  12. set user_pass [lindex $data 0]
  13. close $fp
  14.  
  15. # The problem appears to be down to passing a potentially layered command
  16. # e.g. ssh -t <server> uname leads to tcl (expect) error:
  17. # "sudo: ssh -t <server> uname: command not found"
  18. spawn sudo -u $user $command
  19.  
  20. expect {
  21. # TODO: Currently specific to ssh, but should handle local sudo as well
  22. -re "Enter passphrase for key.*" {
  23. send "${user_pass}\r"
  24. }
  25. }
  26. expect eof
  27.  
  28. foreach {pid spawnid os_error_flag value} [wait] break
  29. #lassign [wait] pid spawnid os_error_flag value
  30.  
  31. if {$os_error_flag == 0} {
  32. puts "exit status: $value"
  33. } else {
  34. puts "errno: $value"
  35. }
  36.  
  37. exit $value