How to execute bash script that requires multiply answers? [duplicate] - linux

This question already has answers here:
Pipe multiple verbatim lines into command in bash
(2 answers)
Closed 2 months ago.
Here is the case. I have bash script that requires a couple of command to be executed. First of all, it requires sudo, then answer (y/n) and then password one more time. What I want to do is I want to execute it in one command.
Let's say I have my bash script - myscript.sh. This script requires sudo to be executed. So, to execute it in one line I can write:
echo 'mypassword' | sudo -S bash myscript.sh
And this will work. But after script is executed I need to answer y and type password one more time. How can I do that?
Here is what I have tried:
printf '%s\n mypassword y mypassword' | sudo -S bash myscript.sh
echo 'y\nmypassword\n' | echo 'mypassword' | sudo -S bash myscript.sh
And there were a couple more of what I have tried, but it didn't work.

You can:
printf '%s\n' mypassword y mypassword | ...
( echo mypassword; echo y; echo mypassword ) | ...
This script requires sudo to
Note that typing that on the command line or in a script will publish your password. Instead, configure sudoers to allow the user to login without a password. Consider configuring credential caching in sudoers, so you don't have to type password twice.

Related

Run a command in the middle of a script as root [duplicate]

This question already has answers here:
sudo echo "something" >> /etc/privilegedFile doesn't work [duplicate]
(15 answers)
Closed 4 years ago.
I need that in the middle of a script in bash a line be executed as root, I have this code:
#!/bin/bash
...
EOF
if [[ $x = true ]] || [[ $y= "On" ]]; then
echo -e "0 8 * * * root " >> /etc/crontab
I need the echo written in the crontab by the root user, who has a password.
Currently it does not write as it is executed by the ubuntu user.
Thank you.
I propose this:
echo -e "0 8 * * * root " | sudo tee -a /etc/crontab
When your script encounters the sudo for the tee, it will ask for your password or, if you already gave your password for sudo just recently, it will just execute the task. (Of course, if this user doesn't need to type a password for sudo, it will never ask for it.)
Using tee -a instead of shell redirection works around the fact that redirections are performed by the current shell, so adding sudo to the command will not affect the redirect (and then it will fail due to missing permissions).

Perform SSH remote cmd exec on multiple local servers from input (sshpass?) [duplicate]

This question already has answers here:
Loop through a file with colon-separated strings
(2 answers)
Using a variable's value as password for scp, ssh etc. instead of prompting for user input every time
(10 answers)
How to automate password entry?
(5 answers)
Closed 4 years ago.
I am currently looking for a solution for executing remote commands on multiple local servers from an input file containing the 'user : password' in the following format:
jboss5:manager:192.168.1.101
database1:db01:192.168.20.6
server8:localnet:192.168.31.83
x:z:192.168.1.151
test:mynet:192.168.35.44
.... and others
Some commands I wish to execute remotely:
cd $HOME; ./start_script.sh; wget 192.168.1.110/monitor.sh; chmod +x monitor.sh; ./monitor.sh
I know there is a utility called "sshpass" but not sure how I could apply this utility for my needs.
I am open to any ideas in order to fulfill my need, any help would be very appreciated!
Thanks
Did you think about using ssh-keys (check man ssh-keygen)? You will be able to connect without password input ...
However if you cant, just try:
for i in $(< your_file); do
user=$(echo $i | cut -d: -f1)
pass=$(echo $i | cut -d: -f2)
ip=$(echo $i | cut -d: -f3)
sshpass -p $pass ssh $user#$ip bash -c "you commands &"
done
Instead of using cd $HOME use the full path with yours scripts names.And dont forget the & for send the process in background...

ssh and execute several commands as another user through a heredoc [duplicate]

This question already has answers here:
Usage of expect command within a heredoc
(1 answer)
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Closed 4 years ago.
I have a script that I need to execute through ssh as another use, is there a way to pass whole script like this:
ssh -t user#server.com sudo -u user2 sh -c << EOF
cd /home
ls
dir=$(pwd)
echo "$dir"
echo "hello"
....
EOF
Returns: sh: -c: option requires an argument
ssh'ing and sudo'ing separately is not an option and putting .sh file directly on the machine is not possible.
sh -c requires a command string as the argument. Since you are reading the commands from standard input (through heredoc), you need to use sh -s option:
ssh -t user#server.com sudo -u user2 sh -s << 'EOF'
cd /home
ls
dir=$(pwd)
echo "$dir"
echo "hello"
...
EOF
From man sh:
-c
string If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.
-s
If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell.
Need to quote the heredoc marker to prevent the parent shell from interpreting the content.

How can I automatically respond to a password prompt via the command line?

I'm looking to respond to a password prompt in the linux terminal. I know how to do this with echo and a non-password prompt. For example, let's say whatsyourname.sh prompted me for a string with my name after being ran, but didn't allow my name to be passed as an argument in the inital command. I would do the following:
echo -e "dan" | ./whatsyourname.sh
However, if I ran a command that asked me for a password after being ran, the following does not work:
echo -e "supersecurepassword" | sudo apt-get update
I'm guessing this has something to do with the fact that the characters are hidden while a password is being input in the command line. How would I respond to a password prompt within the inital command?
You're looking for sudo -S
Explaining -S - man sudo
-S, --stdin
Write the prompt to the standard error and read the password from the standard input instead of
using the terminal device. The password must be followed by a newline character.
Simple,
#!/bin/bash
echo "notsecure" | sudo -S apt-get update
Variable,
#!/bin/bash
pass="notsecure"
echo $pass | sudo -S apt-get update
Lets still type it,
#!/bin/bash
read -s -p "[sudo] sudo password for $(whoami): " pass
echo $pass | sudo -S apt-get update
Explaining -s and -p - help read
-r do not allow backslashes to escape any characters
-s do not echo input coming from a terminal
Handy if you make a script that logs into multiple servers to view route -n for example.

How to execute multiple commands with sudo in script [duplicate]

This question already has answers here:
How to run two commands with sudo?
(11 answers)
Closed 7 years ago.
Can we use heredocs to run multiple commands using sudo?
I am facing an issue (need to pass password for every command) while running
multiple commands:
echo 'password'| sudo -S ls
echo 'password'| sudo -S cat /var/log/abc.log
Can anyone help me how to automate this in a script? Like:
echo 'password' | sudo -S << ENDBLOCK
ls
cat
ENDBLOCK
you can run sh -c ..., but remember to quote properly.
sudo sh -c 'id; echo another command ; id'
sudo must see this as a single argument for the sh command.
Of course you can use new line instead of semicolon:
sudo sh -c '
echo "I am root"
id
echo "another command"
id
'
One way is to write a script with all the things you need to do with sudo and then run the script with sudo.
you could put all your commands in a script. Then
sudo ./script.sh
put permissions for script.sh in /etc/sudoers.d; that way you'll never need to type your password again (for that script)

Resources