how can i switch user using
su command and adding password from file ? i want to make a script which will automaticly switch the user, having the user and the password ,instead of manually typing the password ?
P.S. I don't have super user rights.
Typically programs like
su require passwords to be entered on the terminal and can't be bypassed. The
root user doesn't need to enter a password to switch to another, so it's common to see solutions like
sudo su user.
However we might be able to cheat. If you have the
expect command on your system then we can fake typing the password
#!/usr/bin/expect -f set user [lindex $argv 0] set password [lindex $argv 1] spawn /bin/su $user expect "Password:" send "$password\r"; interact
With this program you can do
./autosu username password
% autosu root imnottellingyou spawn su root Password: [root@server /]#
It's not normally a good idea to do this exact thing, though, 'cos the password will appear in your shell history when you type it on the command line. But if you have the password inside a script or data file then it doesn't matter too much! You should be able to modify this simple example to meet your needs.
If you don't have
expect installed then it gets harder; ask your admin to install it, or compile it from source and run it from your directory!
The best option is use Sudo rulles
Safe passwords in text file is in top 10 wrong pratice IT. but if you want use you must rememebr parwd is not read from stdin but ftom tty so you can use
expect(*1) but this is still wrong way .
Beter option is use sudo or sshkey you can login to ssh account using public key to localhost address .
do not try
su - username <<< password
su username << - _EOF_ you_password whoami _EOF_
this only can run command in another sheell and give you output.