Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

su pass password to script

Tags:

sudo

sudoers

I am trying to write a script that will run the following commands:

sudo su
runmqsc_result=`su -c "runmqsc QMGR < /home/rob/query_queue.txt" -m "mqm"`

My issue however, is that these commands are run as part of a shell script, by user that is in the sudoers file. However, obviously sudo su asks for the password of the user running it.

What I need to do is to pass the password to sudo su so that the script will run automatically. How can I do this?

p.s: I can't change the permissions for running "runmqsc"...it HAS to be run as user mqm which needs to be switched to from the root user.

like image 653
RobM Avatar asked Oct 03 '13 16:10

RobM


People also ask

How do you put a password on a Linux script?

#!/bin/bash password="" echo "Enter Username : " # it will read username read username pass_var="Enter Password :" # this will take password letter by letter while IFS= read -p "$pass_var" -r -s -n 1 letter do # if you press enter then the condition # is true and it exit the loop if [[ $letter == $'\0' ]] then break fi ...

How do you put a username and password in a script?

The useradd command/adduser command used to create a new user on Linux and passwd command to set or change password for users.

Can I sudo with password?

By default, sudo needs that a user authenticates using a password before running a command. Some times you may need to run a command with root privileges, but you do not want to type a password using sudo command. This is useful for scripting or any other purpose.


2 Answers

From man sudo:

-S    The -S (stdin) option causes sudo to read the password from the standard
      input instead of the terminal device.  The password must be followed by a
      newline character.

So, while it defies all security principles, echo 'password' | sudo -S su [...] should work.


Alternatively, you could make your script writeable only by root and add the following to /etc/sudoers to allow the user johndoe to run it with root priviledges without having to enter his password:

johndoe ALL = NOPASSWD: /full/path/to/your/script

The part writeable only by root is important to prevent johndoe from modifying the script and executing arbitrary commands as root.

like image 120
n.st Avatar answered Oct 04 '22 13:10

n.st


This solution work by using 'script' command from the 'bsdutiles' package that setup a pty (a terminal). The 'sleep' command is there to prevent sending the password before the 'su' command is ready to read it. The 'tail' command remove the "Password:" input line issued by 'su'.

 { sleep 1; echo rootpassword } | script -qc 'su -c "runmqsc QMGR < /home/rob/query_queue.txt" -m "mqm"' /dev/null | tail -n +2

Beware that the rootpassword could be see in many ways (history, ps, /proc/, etc...). Start the command with a space to at least avoid history recording.

like image 23
jcamdr Avatar answered Oct 04 '22 14:10

jcamdr