Postgres createuser.exe silent execution from batch script - windows

I am finding myself with the issue of needing to execute the postgres createuser.exe from a batch script and cannot get it to stop prompting me with the following:
Enter name of role to add:
my batch script looks like this:
echo calling createuser!
createuser username %super_user% -s -U Super_Postgres s -q
Where %super_user% is a command line argument. Any help would be greatly appreciated, this is the documentation that I am referring too: postgres

"username" should go at the end, after the options. You have it as the first parameter.

from the documentation http://www.postgresql.org/docs/8.4/static/app-createuser.html
The following is from the documentation listed above
To create the same user joe using the server on host eden, port 5000, avoiding the prompts and taking a look at the underlying command:
$ createuser -h eden -p 5000 -S -D -R -e joe
CREATE ROLE joe NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN;
To create the user joe as a superuser, and assign a password immediately:
$ createuser -P -s -e joe
Enter password for new role: xyzzy
Enter it again: xyzzy
CREATE ROLE joe PASSWORD 'md5b5f5ba1a423792b526f799ae4eb3d59e' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;

Related

How do I execute sudo commands on an ssh'ed machine? [duplicate]

This question already has answers here:
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Closed 1 year ago.
I wrote a short script that would ssh to a bunch of machines on a file called config that would iterate through the machines, ssh through them and create a new user on them. problem is - these commands require sudo privileges, and when I'm trying to execute sudo on them, I get a wrong password error, probably because sudo is not allowed over ssh? I'm not quite sure.
The code is as follows:
#!/bin/bash
read -p "enter remote admin username " adminuser
read -p "choose new username " newuser
read -p "choose new pass " newpass
while read -u10 HOST ; do ssh ${HOST} "uname -a" ;
sudo -S adduser --disabled-password --gecos "" $newuser
sudo -S chpasswd <<<"$newuser:$newpass"
sudo -S chown $newuser /home/$newuser
#sudo -S groupadd group
echo; echo "New user ${newuser} has been created on ${HOST}"
done 10< config.txt
It's worth to note I have set /etc/ssh/sshd_config PermitRootLogin to yes.
While we're at it, is there a way to minimize the amount of times i have to input my admin password? Right now I have to use it when I first ssh into the machine and when I execute a sudo command - so if I have 17 machines that's a minimum of 17 machines. I'd like to minimize that if possible.
Please do not set /etc/ssh/sshd_config PermitRootLogin to yes. No reason to play with fire unless necessary.
On the remote machine, use visudo to define a group like admin that never needs to enter a password in order to use sudo. Here are two lines from my /etc/sudoers file:
# Members of the admin group may gain root privileges
%admin ALL=(ALL) NOPASSWD: ALL
Then add the user id to that Linux user group and the script will run as root without prompts for sudo passwords:
$ usermod -a -G admin my_user_name

How to add username and password at command prompt using shell script

I have logged in to a host i.e 10.54.***.*** . Also I have executed a command as bdrun sa -u root but it asks for password to enter. How can I perform it using shell script ?
you can use redirect option to provide a password in run time in a shell script like below ( You might need to add your user account in sodo user config, check with your Unix system admin for detail ):-
echo "yourpassword" | sudo -S ls
So in your case you can use like below:-
echo "yourpassword" | sudo -S bdrun sa
Or if your password is in a file for example pass.txt
cat pass.txt | sudo -S bdrun sa
Hope this will help your. Good Luck!!

user created by shell script is not executing proceeding commands

I have created a shell script as follows.
username='root'
sudo -H -u "$username" bash 2<&0 << 'END_COMMAND'
useradd -m -s /bin/bash suhail
passwd suhail
mkdir ~/test
END_COMMAND
But i am getting the out put as follows when i trying to run this script file
user#uvais-desktop:~/ssp$ ./test
sudo: unable to resolve host uvais-desktop
Enter new UNIX password: Retype new UNIX password: Sorry, passwords do not match
passwd: Authentication token manipulation error
passwd: password unchanged
It is not prompting for password for the new user.Everything happened in a second. !!
Please help me if anyone could.
One problem is that you can't run passwd. It asks to write password twice which you can't do in the script. So you enter password once then for the second time you enter mkdir ~test, hence the message "Sorry, passwords do not match"
Instead of passwd suhail try:
echo suhail| passwd $username --stdin

Switch user without prompting for a password in shell script

I need to switch between two users using a shell script.
I used su and sudo for switching between users. The bottom line is that, it's prompting for user password every time, and I do not want that to happen.
I know the password; is there a way I can hard code it in the script itself, so that it will not prompt the user for a password?
Wouldn't a NOPASSWD clause in sudoers work? For example:
user1 ALL=(ALL:ALL) NOPASSWD: /bin/su user2
Allows user1 to su to user2 without password. If you only need to run a certain command as user2, add that to sudoers (through visudo) explicitly:
user1 ALL=(user2) NOPASSWD: /path/to/command
Then as user1 run:
sudo -k user2 /path/to/command
With the -S parameter sudo accepts the password from Standard Input. See: How to pass the password to su/sudo/ssh without overriding the TTY?

sudo as another user with password from stdin

I am attempting to use sudo check to see if the password for the user is set to a standard password.
I have read that you can use the -S option to receive input from stdin
-S The -S (stdin) option causes sudo to read the password from
the standard input instead of the terminal device.
but when i run echo 'password' | sudo -S -u user command
it returns:
Password:
Sorry, try again.
Password:
sudo: 1 incorrect password attempt
Could somebody tell me what is off with this script?
Note: I have tried other methods to check the password but I am having a hard time finding a solution because I do not have: root access, a c compiler, or the availability to install programs like expect
Does your password end in a newline? Try:
printf password
or
echo -n password
Sudo is not expecting the password of the specified user, rather the password of the user calling "sudo":
http://en.wikipedia.org/wiki/Sudo

Resources