creating username and set password remotely - shell

I am running script which generated strong password and takes input as user name and calls
remote script to create username and password .
script goes like this
USERNAME=$1
PASS=`cat /dev/urandom|tr -dc 'a-zA-Z0-9-!##%()+{}$|:?='|fold -w 10 | head -n 1| grep -i '[!##%()+{}|:$?=]'`
ssh -i /home/ubuntu/test.pem ubuntu#192.168.10.32 "sudo /bin/bash /root/useradd.sh $USER $PASS "
It works fine ,if the password does not contain any extra characte like | , & and $ .
e.g.
ssh -i /home/ubuntu/test.pem ubuntu#192.168.10.32 "sudo /bin/bash /root/useradd.sh testuser1 12345 "
it fails with strong password as follows .
ssh -i /home/ubuntu/test.pem ubuntu#192.168.10.32 "sudo /bin/bash /root/useradd.sh testuser1 v|9q4TT8={ "
Is there any workaround for this .
Regards

Use enclosing " " to use strong password, bash will treat any character between " " as String.
ssh -i /home/ubuntu/test.pem ubuntu#192.168.10.32 "sudo /bin/bash /root/useradd.sh \"$USER\" \"$PASS\" "
And don't forget to escape inner quotes. i.e. add like \"

Related

Bash execute multiple command to a remote server on EOF

I try from my terminal, to execute on remote server a openstack command to a docker. The purpose is to get the id of openstack project.
when condition is true, I want to get id, but the script below failed to get id. I don't know if I can execute if condition to EOF statement
ret="$(ssh -qT root#server << EOF
docker exec openstack bash -c ". adminrc &&
if [ false ]; then openstack project create p_ops &>/dev/null
else :
fi
id_u=$(openstack user show u_ops | grep " id" | cut -d "|" -f3 | xargs)
openstack role add SwiftOperator --project p_ops --user $id_u
id_p=$(openstack project show p_ops | grep " id" | cut -d "|" -f3|xargs)
echo "$id_p""
EOF
)"
I get the output :
Missing value auth-url required for auth plugin password
Missing value auth-url required for auth plugin password
usage: openstack role add [-h]
[--system <system> | --domain <domain> | --project <project>]
[--user <user> | --group <group>]
[--group-domain <group-domain>]
[--project-domain <project-domain>]
[--user-domain <user-domain>] [--inherited]
[--role-domain <role-domain>]
<role>
openstack role add: error: argument --user: expected one argument
I desired the id of project :
echo $id_p
faafe2044c4235ac648faaceae5d1a3bf2a8f7a8ca8a765f5a9621a5e53d9162
You can avoid the use of nested quotes which are giving you problems.
Instead of the complex sentence:
openstack user show u_ops | grep " id" | cut -d "|" -f3 | xargs
you can just write:
openstack user show -f value -c id
ret=$(ssh -qT root#server << EOF
docker exec openstack bash -c ". adminrc &&
if [ false ]; then openstack project create p_ops &>/dev/null
else :
fi
id_u=$(openstack user show -f value -c id u_ops)
openstack role add SwiftOperator --project p_ops --user $id_u
id_p=$(openstack project show -f value -c id p_ops)
echo $id_p"
EOF
)

How to set the password for a new created in bash user using awk

Need some help in assigning a password to each newly created user from a text file using awk.
For example:
Text file:
John Doe 12345678
Jane Doe 87654321
Newly created user:
JDoe5678 with password: 12345678
JDoe4321 with password: 87654321
My current code:
#!/bin/bash
PATH_EMPLOYEE_FILE="employeelist"
password=($(awk '{print {print $3}))}' "${PATH_EMPLOYEE_FILE}"))
groupname="group1"
USERS_LIST=($(awk '{print substr($1,1,1) $2 substr($3,length($3)-3,length($3))}' "${PATH_EMPLOYEE_FILE}"))
for USER in "${USERS_LIST[#]}"
do
echo "User account created: ${USER}"
useradd -m -G "${groupname}" "${USER}" -p ${password}
done
You're not indexing $password, so you're always using the first password in the useradd command.
There's no need for awk or arrays, you can use bash's read command, and its parameter expansion operators to extract parts of the first name and password into the username.
while read -r fname lname password; do
username=${fname:0:1}$lname${password: -4} # don't forget the space before -4
echo "User account created: $username"
useradd -m -G "$groupname" "$username" -p "$password"
done < "$PATH_EMPLOYEE_FILE"

bash create user with password: password not set as expected

In I want to set a username and password non-interactively, but the password is not getting set correctly.
create_user.sh
user=username
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
echo $user $pass
useradd -m -p $pass $user
In the terminal:
$ sudo ./create_user.sh
username pa8fg5oAyLo8g
$ tail -1 /etc/passwd
username:x:1004:1004::/home/username:
$ su - username
Password: password
su: Authentication failure
What am I doing wrong?
UPDATE
This works, but it prints username password to the terminal window, which may not be desirable, and it requires hard-coded values:
create_user.sh
user=username
pass=password
useradd -m username
echo 'username:password' | chpasswd
In the terminal:
$ sudo ./create_user.sh
username password
$ su - username
Password: password
username#hostname:~$
My syntax was wrong. Here is a working version:
create_user.sh
user=username
pass=password
salt=Az # or any 2-character string from [A-za-z]
# Encrypt the password
pass=$(perl -e 'print crypt($ARGV[0], $salt)' $pass)
echo $user $pass
useradd -p $pass -m $user
In the terminal:
$ sudo ./create_user.sh
username AzSzB2uy8JFlk
$ su - username
Password: password
username#hostname:~$

BASH - how echo works inside EOF tags

I would like to execute the followings:
PASSWORD="mypassword"
RUNCOMMAND=$(cat <<EOF
echo $PASSWORD | sudo -S sudo echo "this is it babe"
EOF
)
But instead of this is it babe, I get the following result:
mypassword | sudo -S sudo echo "this is it babe"
I tried with cat <<\EOF, cat <<'EOF' still no luck.
Any ideas?
You are confusing a heredoc with a pipeline.
heredoc with variable expansion:
cat <<EOF
some text, possibly with variables: ${HOME} / $(whoami)
EOF
some text, possibly with variables: /home/attie / attie
heredoc without variable expansion:
cat <<"EOF"
some text, possibly with variables: ${HOME} / $(whoami)
EOF
some text, possibly with variables: ${HOME} / $(whoami)
pipeline with variable expansion (note the quotes, "):
echo "some text, possibly with variables: ${HOME} / $(whoami)" | cat
some text, possibly with variables: /home/attie / attie
pipeline without variable expansion (note the quotes, '):
echo 'some text, possibly with variables: ${HOME} / $(whoami)' | cat
some text, possibly with variables: ${HOME} / $(whoami)
${...} expands an environment variable
$(...) runs a command, and substitutes its stdout
It also looks like you're trying to have your password entered into sudo - this won't work, as sudo will repoen the terminal to acquire your password, before passing it's stdin to the final application.
You are starting from a false premise, that eval $RUNCOMMAND is something you should do. It is not; variables are for data, functions are for code.
run_command () {
docker_run_options=(
--restart=always
--name "${USER_NAME}_$(date +%Y%m%d-%H%M%S)"
-d
-e "VIRTUAL_HOST=$USER_VIRTUAL_HOST"
-e "VIRTUAL_PORT=$USER_VIRTUAL_PORT"
-e "PORT=$USER_VIRTUAL_PORT"
-p "$USER_VIRTUAL_PORT:$USER_VIRTUAL_PORT"
)
echo "$1" | sudo -S sudo docker run "${docker_run_options[#]}" "$USER_IMAGE"
}
fun_run_command () {
run_command "PASSWORD"
}
The final solution is rather simple:
PASSWORD="mypassword"
RUNCOMMAND=$(cat <<EOF
echo $PASSWORD | sudo -S sudo echo "this is it babe"
EOF
)
And execute it via eval:
eval $RUNCOMMAND
Sorry for stealing your times with this obvious problem guys:)
The usecase for the above is to echo a given command before really executing it.
Like this:
fun_run_command(){
# execute the final command
echo `eval $RUNCOMMAND`
}
fun_echo_command(){
# echo the command which will be launched (fun_run_command())
echo ${RUNCOMMAND//$PASSWORD/PASSWORD}
}
RUNCOMMAND=$(cat <<EOF
echo $PASSWORD | sudo -S sudo docker run --restart=always \
--name ${USER_NAME}_`date +%Y%m%d-%H%M%S` \
-d \
-e "VIRTUAL_HOST=$USER_VIRTUAL_HOST" \
-e "VIRTUAL_PORT=$USER_VIRTUAL_PORT" \
-e "PORT=$USER_VIRTUAL_PORT" \
-p $USER_VIRTUAL_PORT:$USER_VIRTUAL_PORT \
$USER_IMAGE
EOF
)
As you can see the command what I launch is quite long,
so it is always make sense to doublecheck what is executed by the script.
Having copy&paste the same command to multiple function is prone to error.

How to auto input the password when run vncserver :1?

I'm writing a bash script that will connect to my remote machine then run some commands, one of them is vncserver :1, but this command need to input a password. How can I do it in my shell script? (I just need to run the script only, don't need to input the password)
This is my script:
ssh -i $pem -o StrictHostKeyChecking=no -o 'IdentitiesOnly yes' admin#$ip -f '
pkill vnc ;
vncserver :1 ;
'
Thanks all,
It's ok now:
ssh -i $pem -o StrictHostKeyChecking=no -o 'IdentitiesOnly yes' admin#$ip -f '
pkill vnc ;
expect -c "
spawn vncserver :1;
expect -nocase \"password:\" {
send \"$pass\r\";
expect -nocase \"Verify:\" {
send \"$pass\r\";
expect -nocase \"Would you like to enter a view-only password \(y\/n\)\?\" {
send \"n\r\";
expect eof }; }; interact } ;
"
'

Resources