I want to su to another user, using expect, and then run a graphical program as that user. I am using kcalc to test with:

set timeout 20
spawn su dummy
expect "Password:"
send "PASS\r";
send "kcalc\r"

I also tried eliminating the last line and changing the su to 'su dummy -c kcalc', but that didn't work either. I appreciate any help.

  • This could be StackOverflow's territory... or could be Unix.se... or could be SuperUser. Nevertheless, probably not ServerFault. Mar 6, 2012 at 11:53

3 Answers 3


Instead of leaving account passwords sitting around the file system, why not just create a sudo command to do specifically this, and grant it with the NOPASSWD option?

  • While this might be helpful, it seems like a comment rather than an answer. Jan 20, 2018 at 3:42

I was just doing this today using spawn SSH sessions here are some bits of the code I wrote not sure if it will help:

echo "Type the account you wish to use to issue the command, followed by [ENTER]: "
read account
read -s -p "Password: " password
echo "Type the command you wish to execute, followed by [ENTER]: "
read command
echo "Type the hostname you want to connect to, followed by [ENTER]: "
read hostname
switchToSudo="sudo su"
shortenedHost=`echo $hostname|cut -d. -f 1`

 VAR=$(expect -c "
        expect \"*~$\"
        send \"$switchToSudo\r\"
        expect \"*?assword*\"
        send \"$password\r\"
        spawn ssh $account@$hostname
        expect "*?assword:*"
        send \"$password\r\"
        expect \"*~$\"
        send \"$switchToSudo\r\"
        expect ""
        send \"$command\r\n\"
        send "logoff"
echo "=============="
echo "$VAR"

You have a couple of options here.

1) As MrTuttle stated, adding your user to sudoers with NOPASSWD is preffered. This will prevent needing to store a clear-text root password /etc/sudoers

yourUser ALL=(OtherUser) NOPASSWD:/path/to/kcalc 

will allow you to

$ sudo -u OtherUser kcalc

with out being prompted for a password.

2) Your current script.

Your current script should work, keep in mind, that not every system and prompt capitalizes "Password:" or leaves a space at the end. I find that "assword" is a better string to test with. The other thing to note is that su is asking for the ROOT password, not user dummy. If you don't have the root password, you will need to login as dummy instead (usualy ssh -l dummy localhost)

for some debugging steps, you can try multiple statements during the password phase of your expect. I find it useful to test and report for timeout and eof while developing scripts.

You might want to try this to help see where your script is ending

expect {
  -re "(.*)assword:"  { sleep 1; send -- "password\r" }
  timeout     { puts "un-able to login: timeout\n"; return }
  eof         { puts "Closed\n" ; return }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.