1

I have been looking all over for a simple way to combine various scripts to do this. I am not the strongest at scripting but I am working on improving so go easy on me. Any assistance here is appreciated.

I am not the author of this but I am using it as a start. I'd like to understand what the best approach to running this against a list of ips would be. I'm most interested in simplicity and understanding if a user exists already and if it errors out.

#!/bin/bash
# Script to add a user to Linux system
if [ $(id -u) -eq 0 ]; then
read -p "Enter username : " username
read -s -p "Enter password : " password
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
    echo "$username exists!"
    exit 1
else
    pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
    useradd -m -p $pass $username
    [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
fi
else
echo "Only root may add a user to the system"
exit 2
fi
  • Do you want to do the check for user existence on each destination host, or only on the host running the script? – Alex Stragies Dec 07 '16 at 20:08
  • On the destination. If the user already exists I would in this use case just want to skip over them and move on the the next user. – Glenn B. Dec 07 '16 at 22:08

2 Answers2

1

Rather than relying on scripts to do this, check out ansible, it makes these tasks less mistake prone, and can be run multiple times without issue as it's idempotent. It is also agentless and runs over ssh.

  • vars prompt: to ask for username and passwords
  • user module: creates users, home dir etc
  • group module: if you want to create groups outside of the user module
  • line in file: to modify sudoers, there are good examples but make sure you check out the one with the validate option.
David
  • 305
  • understood, ill check into leveraging Ansible, I use it for many other items as well as chef, im not against it, was just stumped by this effort in Bash – Glenn B. Dec 07 '16 at 22:12
  • Still working on this piece. Got through some basics today. Will continue to work at it and share what I come up with. – Glenn B. Dec 13 '16 at 02:58
0

Here is a script I just created to demonstrate how to do it.

 #!/usr/bin/env bash

warn() 
{
    printf '%s\n' "$@" >&2
}

die() 
{
    local st="$?"
    warn "$@"
    exit "$st"
}


anotherUser() {
    read -p "Add another user [y/n] " yn
    if [[ $yn = *[yY]* ]]; then
        checkUser
    fi
    exit        
}   
checkUser() {

while :
    do
        read -p "Enter user: " userName
        if id $userName 2>/dev/null
            then echo "User exists"
            anotherUser
        else
        read -p 'Enter pass: ' passWord
            adduser "$userName"
            echo "$passWord" | passwd "$userName" --stdin
            printf "User %s has been added\n" "$userName"
            exit
        fi  
    done
}

if (( EUID == 0 )); then
    checkUser
else
    die "You must be root"
fi

Make sure to copy the script on remote servers and execute it as follow

ssh -t root@remoteserver 'bash accountScript'

  • Took me a bit to get back and report that this worked great for an interactive script. Very grateful so thanks. I will continue to expand on this. – Glenn B. Dec 13 '16 at 02:59