Automating input in bash [duplicate] - bash

This question already has answers here:
Passing arguments to an interactive program non-interactively
(5 answers)
How do I specify a password to 'psql' non-interactively?
(11 answers)
Closed 5 months ago.
I have a bash script that runs a few psql commands. Every time a command is ran it prompts for user password. I'd like to make the script input the password automatically so I'd only need to run the script and not need to input anything.
I am aware that I can do this for psql
PGPASSWORD=root psql -h localhost -d $db_name -U root -c
but I'd like to know how to automate input in general/

You can export the PGPASSWORD as below:
export PGPASSWORD=root
psql -h localhost -d $db_name -U root -c

Related

Read dotenv from bash script [duplicate]

This question already has answers here:
Set environment variables from file of key/value pairs
(50 answers)
How to set environment variables from .env file
(5 answers)
Closed last year.
I have a bash file setting up a postgres-database.
#configure_db.sh
source ./secrets/sqlpassword.sh
createdb -U myuser mydatabase
I have a simple bash file exporting the password
#secrets/sqlpassword.sh
export PGPASSWORD='mypassword'
The rest of the node server uses environment variables saved in a .env-file. For the sake of order and simplicity i would want my postgres password stored in the same file:
//.env
postgrespassword='mypassword'
How can you import a variable from a .env-file to as bash-file?
Is there another way of solving the above?
Why don't you follow the same pattern?
Change
#configure_db.sh
source ./secrets/sqlpassword.sh
createdb -U myuser mydatabase
to
#configure_db.sh
PGPASSWORD=$(grep postgrespassword /path/to/.env | cut -d "=" -f2)
createdb -U myuser mydatabase

How do I pass arguments containing special characters to a bash script? [duplicate]

This question already has answers here:
Running shell command that has nested quotes via ssh
(3 answers)
How can I escape an arbitrary string for use as a command line argument in Bash?
(8 answers)
Closed 1 year ago.
Normally I can run a command to run a sql script via the commandline in the following manner in my server:
[ec2-user#ip-XX-XX-XX-XXX ~]$ sudo mysql -h BLAHBLAHBLAH.us-east-1.rds.amazonaws.com -u user -p'aaaaa:b>c[d{e]ff=|ggggggggg^$*' adi_chf_db < ./test.sql
So I wanted to make this simple and have a bash script run it for me:
#!/bin/bash
sql_cmd_to_run="sudo mysql -h BLAHBLAHBLAH.us-east-1.rds.amazonaws.com -u user -p\'aaaaa:b>c[d{e]ff=|ggggggggg^$*\' test_database < ./test.sql"
ssh -t test_server "${sql_cmd_to_run}"
My result is the following:
bash: ggggggggg^': command not found
ERROR 1045 (28000): Access denied for user 'user'#'XX.XX.XX.XXX' (using password: YES)
Connection to XX.XX.XX.XXX closed.
I also understand that there are some special characters in bash so I tried the following as well (by putting \ before the special characters):
#!/bin/bash
sql_cmd_to_run="sudo mysql -h BLAHBLAHBLAH.us-east-1.rds.amazonaws.com -u user -p\'aaaaa:b\>c\[d\{e\]ff=\|ggggggggg\^$\*\' test_database < ./test.sql"
ssh -t test_server "${sql_cmd_to_run}"
Which the output is:
ERROR 1045 (28000): Access denied for user 'user'#'XX.XX.XX.XXX' (using password: YES)
Connection to XX.XX.XX.XXX closed.
(I've obscured some of the values, obviously for some security reasons.)
Don't try to do shell quoting by hand: Let the shell do it for you.
So, if you have a working local command:
sudo mysql -h BLAHBLAHBLAH.us-east-1.rds.amazonaws.com -u user -p'aaaaa:b>c[d{e]ff=|ggggggggg^$*' adi_chf_db < ./test.sql
...then encapsulate it in a function, by adding a mycmd() { line before and a } line after:
mycmd() {
sudo mysql -h BLAHBLAHBLAH.us-east-1.rds.amazonaws.com -u user -p'aaaaa:b>c[d{e]ff=|ggggggggg^$*' adi_chf_db < ./test.sql
}
...and tell the shell to serialize that function into your ssh session:
ssh test_server "$(declare -f mycmd); mycmd"

run a local script as root on remote server [duplicate]

This question already has answers here:
How to use SSH to run a local shell script on a remote machine?
(22 answers)
Closed 5 years ago.
I try to run a local script on multiple remote servers as root. I don't have su to root on those but just can run root commands using sudo. So far I tried:
for host in $(cat hosts_list); do ssh -tt $host "echo mypassword | sudo bash -s" < ./myscript.sh
And in myscript.sh there is something like:
echo "test test123" >> /etc/tests
exit 0
But it looks like not working and won't change the file. What is the proper way to run this script as root and without typing password separately for each host?
Ok, then why do you "echo mypassword" ?
Can't you add your SSH account to the sudoers file with NOPASSWD ?
From man sudoers:
authenticate If set, users must authenticate themselves via a password (or other means
of authentication) before they may run commands. This default may be
overridden via the PASSWD and NOPASSWD tags. This flag is on by default.

Execute a shell script in another unix machine using SSH [duplicate]

This question already has answers here:
SSH in shell script with password
(3 answers)
Closed 9 years ago.
I am trying to execute a script which is present in another unix machine from my unix box using below command:
HOST=myhostname
USER=myuser
ssh -o StrictHostKeyChecking=no -l $USER $HOST "/tmp/myscript.sh"
But the command prompts me to enter the password, but I don't want the command to prompt for password, instead of that I want to pass the password as parameter for my command. But I am not able to find an option to pass the password as parameter to SSH command.
Please help me on how to send password as part of command, instead of the command to prompt it. I am using BASH shell script.
Use EXPECT
Save this as mylogin.exp (or some other name you like), and change the names and password:
#!/usr/bin/expect -f
spawn ssh myname#some_server.com
expect {
password: {send "mypassword\n"}
}
interact
Then just run the command:
./mylogin.exp
That will just get you logged in. If you want to run a command instead, you can just put that at the end of the ssh command.

Automatically input a password when a bash script is run [duplicate]

This question already has answers here:
How to provide password to a command that prompts for one in bash?
(8 answers)
Closed 8 years ago.
For example, say if I have a script saying:
#!/bin/bash
sudo setpci -s 00:02.0 F4.B=00
How do I put the root password into the script so that it accepts it as the password when it reads and executes the sudo line (so I don't have to type it manually)?
Spawning an expect session within your bash script is typically how you automate interactive prompts.
e.g.
expect -c "
spawn sudo setpci -s 00:02.0 F4.B=00
expect -nocase \"password:\" {send \"$PASS\r\"; interact}
"
Note that this isn't the recommended approach in this case as it is a huge security hole to store your root password in a bash script.
The correct solution would be to edit your /etc/sudoers/ to not prompt you for a password for that binary.
#in /etc/sudoers
neohexane ALL=(ALL) NOPASSWD : /usr/bin/setpci

Resources