Auto Switch User Script

Install expect first by:

sudo apt-get install expect

Save script in file:

#!/usr/bin/expect -f
#!/bin/bash

set USER tomcat
set PASS tomcat

spawn su - $USER
expect "*assword:"
send $PASS
send "\n"
interact

Run script by giving permission and ./script_name.sh.
N.B. bash script_name.sh or sh script_name.sh will not work because #!/usr/bin/expect will be ignored it's not a bash script, it's an expect script.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License