I have to write a script and it requires root privileges to execute some linux commnads and to stop & start some services. I'm asked to disable sudo access totally for all the users (as the normal users can gain access to root with the command 'sudo -s') and I did it. So I'm looking for the script to automate the switch user password(su) which does not need user intervention.
On further research I got following script what I'm in need of:
#!/usr/bin/python import pexpect import os passwd = "my-root-password-here" child = pexpect.spawn('su root') child.expect('Password:') child.sendline(passwd) child.expect('$') child.interact()
I have checked this script executing manually logging in as normal user and it switched to root without any problem. But when I type
exit/ctrl+d to switch back to the normal user, I'm getting following error:
Traceback (most recent call last): File "./su3.py", line 9, in <module> child.interact() File "/usr/lib/python2.6/dist-packages/pexpect.py", line 1492, in interact self.__interact_copy(escape_character, input_filter, output_filter) File "/usr/lib/python2.6/dist-packages/pexpect.py", line 1520, in __interact_copy data = self.__interact_read(self.child_fd) File "/usr/lib/python2.6/dist-packages/pexpect.py", line 1510, in __interact_read return os.read(fd, 1000) OSError: [Errno 5] Input/output error