How to give password in shell script?
If you can't use ssh trust and must enter the password later on in your script, use read -s -p "Password:" USER_PASSWORD
to silently read in the password. You can then export USER_PASSWORD
to an expect script, avoiding it being displayed in ps
:
#!/usr/bin/expect -f
spawn scp some.file USER@otherhost:~
expect "assword:"
send -- "$env(USER_PASSWORD)\r"
expect eof
How to get a password from a shell script without echoing
Here is another way to do it:
#!/bin/bash
# Read Password
echo -n Password:
read -s password
echo
# Run Command
echo $password
The read -s
will turn off echo for you. Just replace the echo
on the last line with the command you want to run.
In some shells (e.g. bash) read supports -p prompt-string
which will allow the echo and read commands to be combined.
read -s -p "Password: " password
How to write bash script that enters password after the first command?
You may use expect
script. You can pass arguments from cmd line. Sample code I write:
#!/usr/bin/expect
set timeout 100
set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set command [lindex $argv 3]
spawn ssh $username@$host $command
#puts $command
expect {
"(yes/no)?"
{
send "yes\n"
expect "*assword:" { send "$password\n"}
}
"*assword:"
{
send "$password\n"
}
}
Using the passwd command from within a shell script
from "man 1 passwd
":
--stdin
This option is used to indicate that passwd should read the new
password from standard input, which can be a pipe.
So in your case
adduser "$1"
echo "$2" | passwd "$1" --stdin
[Update] a few issues were brought up in the comments:
Your passwd
command may not have a --stdin
option: use the chpasswd
utility instead, as suggested by ashawley.
If you use a shell other than bash, "echo" might not be a builtin command,
and the shell will call /bin/echo
. This is insecure because the password
will show up in the process table and can be seen with tools like ps
.
In this case, you should use another scripting language. Here is an example in Perl:
#!/usr/bin/perl -w
open my $pipe, '|chpasswd' or die "can't open pipe: $!";
print {$pipe} "$username:$password";
close $pipe
How to provide password to a command that prompts for one in bash?
Take a look at autoexpect
(decent tutorial HERE). It's about as quick-and-dirty as you can get without resorting to trickery.
How to automatically add user account AND password with a Bash script?
You can run the passwd command and send it piped input. So, do something like:
echo thePassword | passwd theUsername --stdin
how to create a file in shell script with specific password
If zipping is an option for you, then you can password protect the zip file:
zip -P 1234 order.zip $dir/output/ORDER.csv
Related Topics
Can a Program Read Its Own Elf Section
Delete Line from Text File with Line Numbers from Another File
Arm Performance Counters Vs Linux Clock_Gettime
Using Flycheck/Flymake on Kernel Source Tree
Docker Installation on Linux Mint 19.2 Doesn't Work
Error While Installing Mongodb on Ubuntu 16.04
Amazon Linux Ami Ec2 Gui/Remote Desktop
Default Font Size of Javafx Under Linux Is Larger as on Windows
How to Capture Remote System Network Traffic
Driver Ch341 Usb Adapter Serial Port or Qserialport Not Works in Linux
Dynamic Listening Ports Inside Docker Container
Scp Command Between 2 Servers with 2 Different .Pem Keys
Is There an Os Command I Can Run to Determine If Running Inside a Xen Based Virtual Machine
How to Delete All Files Starting with ._ from The Shell in Linux
Linux Core Dumps Are Too Large!
Dependency Failure While Installing Libboost-All-Dev on Ubuntu Core 14.04