how to provide an input for whiptail using commhe and line - linux

So, I have to setup several server that requires mysql-server on each and every one of them. I decided to write a shell script to enter a input to whiptail which require the user to input password. I wrote the bash script but it just does not response when it reach the whiptail page, can anyone help me with this? Thanks
#!/bin/bash
sudo apt-get install mysql-server <<< "yes
mypassword
"
First when the sudo apt-get ask about whether to install or not it supplies yes, but then when it came to whiptail it does not provide any input as it should be which is mypassword

Rather than providing input to whiptail, you can run apt-get install like this to skip prompts during installation:
DEBIAN_FRONTEND="noninteractive" apt-get install -y mysql-server
If you want to provide a root password as well, you can do this:
export DEBIAN_FRONTEND="noninteractive"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password myrootpw"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password myrootpw"
sudo apt-get install -y mysql-server
Take care to not accidentally expose your root password (i.e. ensure it doesn't end up in your history, securely delete any script files containing it, I'm sure there are others).
I found this information here.

Related

How do I enable passwordless sudo for all options for a command?

I'd like to enable passwordless sudo for sudo apt update, and also have passwordless sudo work for the apt update command when I run it with options like sudo apt update -q and sudo apt update -qq.
I have the file /etc/sudoers.d/apt with these contents:
%sudo ALL=(root) NOPASSWD: /usr/bin/apt update
This gives me passwordless sudo for sudo apt update, but passwordless sudo fails when I call the command with an option such as sudo apt update -q.
I know I can account for options by listing out each one in /etc/sudoers.d/apt, but I'd prefer something which will work for all available options without me needing to foresee which ones I might want to use in the future.
I can not test it right now, but according to the link, you could use wildcards:
%sudo ALL=(root) NOPASSWD: /usr/bin/apt update *
Again, I could no test it - Be careful as always, when you edit the suders file.
Source: https://www.sudo.ws/docs/man/1.7.10/sudoers.man/#Wildcards
The most robust way that I can see to do this is:
%sudo ALL=(root) NOPASSWD: /usr/bin/apt update, /usr/bin/apt * update, /usr/bin/apt update *
The /usr/bin/apt update makes the basic command sudo apt update passwordless.
The /usr/bin/apt * update makes adding an option first like sudo apt -q update passwordless.
The /usr/bin/apt update * makes adding an option last like sudo apt update -q passwordless.
All of these are needed to handle the different ways the command might be called, and the different possible placements of options in the command.
Credit to Tobias' answer for pointing me in the right direction on this.

How to copy bash commands from snippet without $ sign?

In most of the websites, I can see snippet with a bunch of bash commands ( With a Click to Copy Button), The problem with that button is this copy $ sign too.
Just like the image about, if I click copy, it will copy complete command INCLUDING $ Sign
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install gnome-shell
$ sudo apt-get install ubuntu-gnome-desktop
$ sudo apt-get install autocutsel
$ sudo apt-get install gnome-core
$ sudo apt-get install gnome-panel
$ sudo apt-get install gnome-themes-standard
We, programmers, are lazy in terms of doing this one by one, If I copy and paste this directly on bash it will show error since it will NOT recognize what is "$" as the command is starting with $ sign, is there a simple hack to let bash skip the first $ sign if command starts with it? If not any solution? I am tired of getting a response from the server and then pasting next commands
P.S This question is asked before I guess but that isn't EXACTLY what I have asked so moderators please understand my point.
Just cut it
while read -r line; do
$(echo "$line"|cut -d' ' -s -f2-)
done
After that, you'll just have to copy it to stdin(and end it witg Contr+D
As requested, here is a function that trims it:
function trimDollar(){
echo "$*"cut -d' ' -s -f2-
}

Explanation of this docker file?

I am trying to understand this docker file
FROM ubuntu:trusty
MAINTAINER Wurstmeister
RUN apt-get update; apt-get install -y unzip openjdk-7-jre-headless wget supervisor docker.io openssh-server
ENV JAVA_HOME /usr/lib/jvm/java-7-openjdk-amd64/
RUN echo 'root:wurstmeister' | chpasswd
RUN mkdir /var/run/sshd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
EXPOSE 22
I understood most of the lines but I don't understand what these below lines means?
apt-get install -y unzip openjdk-7-jre-headless wget supervisor docker.io openssh-server
echo 'root:wurstmeister' | chpasswd
sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
Can anyone help me understand? I am not sure what is the purpose of wget supervisor docker.io openssh-server in that line. Also what does echo mean there? And then also last sed line.
apt-get install -y unzip openjdk-7-jre-headless wget supervisor docker.io openssh-server
Installs a bunch of packages using the ubuntu package manager (the base image is Ubuntu Trusty 14.04).
The -y option is used to prevent apt from asking user confirmation about installing the packages and their dependencies: the installation just proceeds without need for any input. This is needed to avoid hanging the process of building the docker image. Packages installed:
openssh-server: so that the container can act as an ssh server and process requests from ssh clients
wget, unzip: utilities
java: eventually this container is used to allow users to access it via ssh, maybe those users need java
supervisor, docker.io: I don't see a direct usage of it in the Dockerfile itself
then
echo 'root:wurstmeister' | chpasswd
Changes password of user root to wurstmeister
sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
Replaces the text PermitRootLogin without-password with PermitRootLogin yes in file /etc/ssh/sshd_config to allow root user to login with password or without (e.g., with public key).
An important general note: If you run SSHD in your Docker containers, you're doing it wrong!
wget supervisor docker.io openssh-server are parameters to the apt-get call. Those are the names of the packages to be installed.
echo is used in combination with | to send the new password to the chpasswd command.
sed is used to update some text in a configuration file (here to allow passwordless login). The s/text/newText/ part does text substitution.

Windows Bash (WSL) - sudo: no tty present and no askpass program specified

After following this tutroial I get the following error when trying to run the commands as user or even sudo:
sudo: no tty present and no askpass program specified
The comments from Lurdan in this article state that you need to run
sudo -S <YOUR_COMMAND>
chmod 0666 /dev/tty
chmod doesn't work but sudo -S does, but surely there's another fix?
So silly, after looking further down I see a solution from Beorat:
To avoid the sudo tty issue and others, run these commands just before running do-release-upgrade:
sudo -S apt-mark hold sudo
sudo -S apt-mark hold procps
sudo -S apt-mark hold strace
If you've already upgraded, run the above commands, then manually downgrade to the Trusty packages:
sudo -S wget http://mirrors.kernel.org/ubuntu/pool/main/s/sudo/sudo_1.8.9p5-1ubuntu1.1_amd64.deb
sudo -S wget http://mirrors.kernel.org/ubuntu/pool/main/p/procps/procps_3.3.9-1ubuntu2_amd64.deb
sudo -S wget http://mirrors.kernel.org/ubuntu/pool/main/s/strace/strace_4.8-1ubuntu5_amd64.deb
sudo -S dpkg -i sudo_1.8.9p5-1ubuntu1.1_amd64.deb
sudo -S dpkg -i procps_3.3.9-1ubuntu2_amd64.deb
sudo -S dpkg -i strace_4.8-1ubuntu5_amd64.deb
More info here: https://github.com/Microsoft/BashOnWindows/issues/482
WSL uses the lxrun executable for management from Windows:
lxrun -h
Usage:
/install - Installs the subsystem
Optional arguments:
/y - Do not prompt user to accept
/uninstall - Uninstalls the subsystem
Optional arguments:
/full - Perform a full uninstall
/y - Do not prompt user to accept
/setdefaultuser - Configures the subsystem user that bash will be launched as. If the user does not exist it will be created.
Optional arguments:
username - Supply the username
/y - If username is supplied, do not prompt to create a password
/update - Updates the subsystem's package index
Given that, you can use lxrun /setdefaultuser root. Just thought I'd point out this side of it since it was required when I ran into the same issue as you after trying to upgrade to Xenial. I can confirm that running this command, then the wget / dpkg commands my issues were resolved.
The commands I used:
wget http://mirrors.kernel.org/ubuntu/pool/main/s/sudo/sudo_1.8.9p5-1ubuntu1.4_amd64.deb
wget http://mirrors.kernel.org/ubuntu/pool/main/p/procps/procps_3.3.9-1ubuntu2_amd64.deb
wget http://mirrors.kernel.org/ubuntu/pool/main/s/strace/strace_4.8-1ubuntu5_amd64.deb
dpkg -i sudo_1.8.9p5-1ubuntu1.4_amd64.deb
dpkg -i procps_3.3.9-1ubuntu2_amd64.deb
dpkg -i strace_4.8-1ubuntu5_amd64.deb
Finally, you might need to run sudo apt-get install -f in case you get The following packages have unmet dependencies [xxx] but it is not going to be installed
I got rid of the error by moving /etc/hosts to /etc/hosts.bu. After closing the shell en opening again, /etc/hosts is recreated and your computer name is added. The error is gone (for me.)

Is there a way to automate the return key after a linux script command?

I am using the line below in my script
add-apt-repository ppa:webupd8team/java
When it is run from the script I get a prompt to press [return] to confirm adding the source to the repositories
Can I automate that return?
Secondly, I am installing oracle-java7-installer and there is a license agreement that prompts for the user to:
1.. OK the License agreement
2.. select YES to Accept License Terms
Can I automate the OK and automate the keypad left & OK to Accept License Terms?
This script is for testing locally and I do want the script to pause for these user inputs each time.
I have seen this method of piping YES to a command:
yes | <command here>
I am hoping there is a similar method to automate these steps...
For add-apt-repository you can use the -y flag to skip the yes/no prompt.
The Oracle Java one is a little more complicated, but this will do what you want:
echo debconf shared/accepted-oracle-license-v1-1 select true | sudo debconf-set-selections
echo debconf shared/accepted-oracle-license-v1-1 seen true | sudo debconf-set-selections
# Install required packages
sudo apt-get install oracle-java7-installer -y
The command echo will produce a new line!

Resources