here is the script sample.expect :
#!/usr/bin/expect
spawn ssh -t USER@HOST "sudo su "
expect "USER@HOST's password:"
send "password\r"
expect "$ "
expect "[sudo] password for USER:"
send "password\r"
expect "$ "
puts "USER [exec whoami]"
interact
-bash-4.1$ expect sample.expect`enter code here`
spawn ssh -t USER@HOST sudo su root
FIPS integrity verification test failed.
USER@HOST's password:
[sudo] password for USER: invalid command name "sudo"
while executing
"sudo"
invoked from within
"expect "[sudo] password for USER:""
(file "sample.expect" line 6)
-bash-4.1$
expecting to log into the host as sudo user. but getting issue [sudo] password for USER: invalid command name "sudo" while executing "sudo" invoked from within "expect "[sudo] password for USER:"" (file "sample.expect" line 6).
expect "[sudo] password for USER:"
That uses square brackets, which are Tcl's mechanism for command substitition. You have to either escape the leading bracket, or use Tcl's non-interpolating quotes: one of
expect "\[sudo] password for USER:"
# or
expect {[sudo] password for USER:}