Friday, 9 June 2017

Adding Linux users via ssh


bash script to add users via and set password via ssh
#!/bin/bash
 
USER_TO_ADD=ansible
USER_TO_ADD_UID=30000
USER_TO_ADD_GID=30000
 
[ $# -eq 1 ] || {
        echo "please provide target host"
        exit 1
}
 
TARGET_HOST=$1
R=$(ping ${TARGET_HOST} 2>&1)
[ $? -eq 0 ] || {
        echo "Unable to ping  ${TARGET_HOST}. Error is $R"
        exit 1
}
  
ssh -t ${TARGET_HOST} "sudo groupadd ${USER_TO_ADD} --gid ${USER_TO_ADD_GID}" && \
 "sudo useradd -m ${USER_TO_ADD} --uid ${USER_TO_ADD_GID} --gid ${USER_TO_ADD_GID}" && \
 "getent passwd ${USER_TO_ADD}" && \
 "usermod --groups wheel ${USER_TO_ADD}" && \
 "echo -e '${USER_TO_ADD}\n${USER_TO_ADD}\n' | sudo passwd ${USER_TO_ADD}"








No comments:

Post a Comment