eithernet2010
eithernet2010

Reputation: 1

Hide plaintext password from showing in bash script?

I have the following bash script to restart the network manager in Debian. The script works as is it should, but not as I would like it to. When the script asks for the sudo password I am able to pass it along using echo, but it displays the password in terminal while the script executes, making it less asthetically pleasing than I would like. Is there anyway to have the script enter the password, but not display the password text while the script calls for the sudo password?

I have tried as many suggestions on Stack Overflow as i could find, well as Stack Exchange before submitting this question.

Script is as follows:

#!/bin/bash 
clear
echo "Restarting service Network Manager"
echo""
sleep 1
echo -e "\033[0;31m......................................\033[0m"
echo -e "\033[0;31m......................................\033[0m"
sleep 1
echo""  
sudo service network-manager restart
sleep 2
echo <Password> 
sleep 2
echo "Service Network Manager Restarted"
sleep 1
echo "" 
echo "Relinquishing control of terminal to user..."
sleep 7
clear

Upvotes: 0

Views: 1426

Answers (3)

Is there anyway to have the script enter the password

Putting password in script is not a good idea. First, from security point of view, password may be recovered from script from anyone with access to script. Second, from maintenance view, once you change your password, scripts suddenly stop working and you have to update them all.

Fortunately, as you are already using sudo there is better solution. You can configure sudo to allow running certain command without password, by using NOPASSWD rule in /etc/sudoers.

myuser ALL=(ALL) NOPASSWD: service network-manager restart

See:

Warning: Always edit /etc/sudoers with visudo, never directly. It prevents you from breaking /etc/sudoers. Once you break your /etc/sudoers, you won't be able to use sudo, including using sudo to fix /etc/sudoers.

Upvotes: 2

Amit Bondwal
Amit Bondwal

Reputation: 161

try this /bin/echo -e "password\n" | sudo apt-get update

or see this Use sudo with password as parameter

Upvotes: 0

DevSolar
DevSolar

Reputation: 70323

Remove the echo <Password> line? I am pretty sure it does nothing other than display the password, as sudo apparently (through an appropriate entry in /etc/sudoers) works without you having to give a password. (What you write to terminal with echo does not get passed to any other process.)

Generally speaking, you can use sudo -S to make sudo expect the password on stdin. But also generally speaking, if you have to hardcode a password in a script, you're doing it wrong in some way.

Upvotes: 3

Related Questions