1

I have below bash script,every time i want to create a user i need to copy this script to server and run it.

Is there any thing we can do so that this script fetches the IP's from a hosts.txt file(running script on multiple server's)one by one and create user on server.Also,we need to be root to run this script.

1.ask for password once and use it in script for rest of the script where ever necessary. 2.login as normal user. 3.become a root user and run the script

#!/bin/bash

#Script to Add User

read -p 'Please Enter The Username To Add: ' name
echo "$name" > /tmp/userlist.txt
clear
echo -e "Hallo $name\nYour Name Is Added To The List."
userfile=/tmp/userlist.txt
username=$(cat /tmp/userlist.txt | tr 'A-Z' 'a-z')
for user in $username
do
useradd $user -N -s /bin/bash
usermod -aG sudo $user
passwd $user
done
echo "=================================="

echo "User $name Have Been Created."

echo "=================================="
tail /etc/passwd | cut -d: -f1
2
  • You are ready to start using a configuration management/orchestration system, such as ansible. Consider making a simple ansible playbook and inventory file to add a user. (Probably someone has already published one online.) It will probably take longer than five minutes but you will learn a lot that will help you automate other tasks too. – Michael Hampton Sep 2 '20 at 2:28
  • @michael : Thanks you.Yes i know about ansilble and we are shifting to it and it will take some time.Meanwhile , we need this script – GauravG Sep 2 '20 at 2:44
1

If you want to copy the script and run it from a centralized point create a general provisioning script in charge of copying and executing

  1. Create an array list, in this case I used a file called hostList.active

  2. Set the only argument to be the script you're trying to copy & execute on the remote server. (In this case it will run in the remote machines home directory)

    #! /bin/bash
    while read box; do
      ping -c 1 -w 1 -q $box > /dev/null
      if (test $? = 0); then
          echo "***************************************************************"
          echo $box
          scp $1 $box:~/.
          ssh -n -o stricthostkeychecking=no -X $box "~/$1"
       else echo $box is not responding to ping
           echo $box >> hostList.notdone
       fi
    done < hostList.active
    

I placed 2 test servers in my array list file (hostList.active) and it connected to both, copied the script and executed it.

   ./copyandrun.sh bogus.sh

   ***************************************************************
    tsweb
    bogus.sh                                                            100%   36     0.0KB/s   00:00    
    Running a Test
    ***************************************************************
    tsdb
    bogus.sh                                                            100%   36     0.0KB/s   00:00    
    Running a Test

Ensure the script your copying has proper permissions to be able to be executed by whatever user your copying it over as.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.