Question: I want to write a script that allows me to pass a password to the `su` command. The reason for this is to automate the process of logging into root from nonroot account. From my basic understanding of security, I know the dangers of this. I still need this to try to crack the root user password by reading passwords from a file.

I have heard about using the `expect` command to automate interactions but I do not know how it works, or if it is applicable in this situation. Any information about using `expect` would be greatly appreciated.

Please note that `sudo` is not an option as my user is not on the sudoer list.

Things I've tried
- Passing the password in using pipes
 Didn't work because of `"su must be used from terminal"` type error
- Passing the password using `echo`
 Whatever was echoed would simply show up after the password was entered

Answer: 
Doing this "oneline style"

```
expect -c 'spawn su - ;expect Password:;send "your-password\n";interact'

```

Take a look at the output of `su -` to guarantee if this is the right phrase that appears to capture a password. I'm using a translated version of Linux here so, the word would be "Senha:" instead of "Password:".

* `expect -c ''`: Run the following actions to this command
* `spawn su -`: Execute this command
* `expect Password:` Expect the word password to do something
* `send "your-password\n"`: Type in your password, followed by an Enter
* `interact`: Keep interaction ready to the command.
You can use `expect` to do that job for you. Save the following to a file, make it executable, and run it:

```
#!/usr/bin/expect
spawn su
expect "Password:"
send "YOUR_PASS\n"
interact

```

(Obviously, you need to specify the correct path to `expect`. Here I used `/usr/bin/expect` as an example.)

If `YOUR_PASS` is the correct root password, you should now have a root shell. If not, you get an authentication failure.

Note that the `interact` command will probably not be needed in the context of a fully automated script, as it gives control of the process to the user, i.e., keystrokes are sent to the current process and stderr and stderr are returned.

See `man expect` for a more detailed description of the commands `spawn`, `expect`, `send` and `interact`, and many other commands.