Find and replace text using a bash script - bash

I have the following script which changes a password using a password file that I have piped the password to in the correct format. However, the next command needs a password file with a revised format. At the moment i just delete and re-create the file, but it would be nice to just edit it instead, im just not sure how:
sudo touch /tmp/password.txt
sudo chmod 600 /tmp/password.txt
sudo echo "AS_ADMIN_PASSWORD=" > /tmp/password.txt
sudo echo "AS_ADMIN_NEWPASSWORD=Pa55w0rd2" >> /tmp/password.txt
sudo /opt/glassfish3/bin/asadmin --user admin --passwordfile /tmp/password.txt change-admin-password \
--domain_name domain1
sudo rm /tmp/password.txt
sudo touch /tmp/password.txt
sudo chmod 600 /tmp/password.txt
sudo echo "AS_ADMIN_PASSWORD=Pa55w0rd2" > /tmp/password.txt
sudo /opt/glassfish3/bin/asadmin --user admin --passwordfile /tmp/password.txt enable-secure-admin

Perfect job for sed:
sed -i '/^AS_ADMIN_PASSWORD=/d' /tmp/password.txt
This deletes the line matching the given regex, in-place.

Related

Executing sudo command in bash script without displaying it

I'm executing a command with sudo from bash script, and I'm wondering how to prevent sudo from displaying anything on the screen
echo "mypassword" | sudo -S cp -u /scripts/.bashrc ~/ > /dev/null 2>&1
The result will be an output displaying: [sudo] password for username:
I want to hide that output..
now, before the first comment;
This isn't the safest way, since you're entering your password into the script, but this is strictly internal servers.
Run sudo --help, we can get answer from the parameter list:
-p, --prompt=prompt use the specified password prompt
Then,
echo "mypassword" | sudo -S --prompt="" cp -u /scripts/.bashrc ~/ > /dev/null 2>&1
may do the trick.

Is there a way to automatically pass a password to sudo without having to type it in?

I have a script like this:
echo [password]| sudo -S
rm /Library/Application\ Support/GarageBand
ln -s /Volumes/WD\ Passport/Projects/Other\ Files/GarageBand /Library/Application\ Support/GarageBand
The
sudo -S
never gets my input. Is there a way to fill the sudo password without having to type it in manually from the console?
Let’s say your password was in the file called password
Pipe it into sudo -S
Example:
echo password | sudo -S rm /path/to/item

How do I write a Dockerfile which changes a protected text file in the docker image?

I have a Dockerfile that follows this pattern:
RUN echo "[DOCKER BUILD] Installing image dependencies..." && \
apt-get update -y && \
apt-get install -y sudo package_names ...
RUN useradd -ms /bin/bash builder
# tried this too, same error
# RUN useradd -m builder && echo "builder:builder" | chpasswd && adduser builder sudo
RUN mkdir -p /home/builder && chown -R builder:builder /home/builder
USER builder
RUN sudo sed -i '/hosts:/c\hosts: files dns' /etc/nsswitch.conf
The part that doesn't work is editing /etc/nsswitch.conf ... Why can't I configure my image to edit this file?
I've tried tweaking the useradd several different ways but the current error is:
Step 8/10 : RUN sudo sed -i '/hosts:/c\hosts: files dns' /etc/nsswitch.conf
---> Running in 97cd39584950
sudo: no tty present and no askpass program specified
The command '/bin/sh -c sudo sed -i '/hosts:/c\hosts: files dns' /etc/nsswitch.conf' returned a non-zero code: 1
How do I achieve editing this file inside the image?
A comment here suggests that all operations in dockerfile should be being run as root, which leads me to believe sudo is not needed. Why then do I see this?
RUN sed -i '/hosts:/c\hosts: files dns' /etc/nsswitch.conf
Step 8/10 : RUN sed -i '/hosts:/c\hosts: files dns' /etc/nsswitch.conf
---> Running in ad56ca17944c
sed: couldn't open temporary file /etc/sed8KGQzP: Permission denied
The problem is on the password for sudo, or a request for password. You need to pass ENV_VARIABLES to your container related with removing the sudo request for password, as follows:
<your-container-user> ALL = NOPASSWD: /sbin/poweroff, /sbin/start, /sbin/stop
You need to execute your sudo freely.
Related question:
How to fix 'sudo: no tty present and no askpass program specified' error?
I figured it out -- I can perform this task without sudo, but only if I do it before calling USER builder. It seems docker has the correct access it needs before I create any users.

Change permissions on a user folder - will this shell script work?

I am trying to change permissions on a user's folder that is not an administrator. I was told to run this script:
{
sudo chflags -R nouchg,nouappnd ~ $TMPDIR..
sudo chown -R $UID:staff ~ $_
sudo chmod -R u+rwX ~ $_
chmod -R -N ~ $_
} 2> /dev/null
But I am afraid that it will chown my user folder, instead of the user I am trying to fix.

sudo and chpasswd on one line

I need connect to server via ssh and change users' passwords without typing ssh or sudo password.
I found some example but I cannot complete one command.
I found this command for ssh connection without typing password and running sudo command for delete user:
sshpass -p $admin_password ssh -t $admin#$server "echo $admin_password | sudo -S /usr/sbin/userdel -r $usr"
then I found command for change users password:
echo "$usr:$password" | sudo -S /usr/sbin/chpasswd;
Finaly, I want something like this:
sshpass -p $admin_password ssh -t $admin#$server "echo $admin_password | sudo -S echo "$usr:$password" | sudo -S /usr/sbin/chpasswd;"
Do you have any ideas?
Your question is actually going to require 2 things. The command to change a password remotely for a user:
ssh remoteserver 'echo -e "passwdofuser\npasswdofuser" | passwd username'
In order to do that you probably need to be root, or in the sudo config, have "passwd" NOT require a password for your user to run.
visudo
Edit the file:
Cmnd_Alias MYPASSWD = /usr/bin/passwd
yourusername ALL = NOPASSWD: MYPASSWD
That should allow you to ssh in and not have to use a password to run the passwd command.

Resources