larrenV2.003's Avatar, Join Date: Jun 2007
Light Poster
btw wats the command to run the script?
munkyeetr's Avatar, Join Date: May 2007
Go4Expert Member
To run the script you would use:

Code:
bash <scriptname>.sh
I'm just going to look over your code in the PM and I will respond ASAP.
munkyeetr's Avatar, Join Date: May 2007
Go4Expert Member
I will go through some parts of your code with you, though I know that you have not run this code because it will not do anything productive:

Code:
sux –     #input command line sux – to enter root
             echo“123pass”    #input password 123pass
Here you are trying to use the script to automate switching to root. It is extremely dangerous to include your root password in a shell script. This is clear text and when you try to run this piece of code you will still be prompted for your root password, and then your password will be echoed to the command-line. Very dangerous from a security standpoint. And regardless of that, the code will not work.

Code:
groupadd –g 747 IMFDelegates    #create group with command line groupadd –g 747 IMFDelegates
Good. Create your group.

Code:
useradd -c common-name -d /home/username -g gid -u uid -s shell -p password
I said that the code will look something like this. You just cut and pasted that into your code, but it is the wrong syntax and in the wrong place. I said that i pretty much gave it to you, not that I did give it to you. BAD! And this will not do what you want it to do.

Code:
       while [ $num –lt 99]
       do
           echo user:x:$uid:747:Secret Agent${num}:/home/agent:/bin/sh    #create         account
           num=$((num+1))
           passwd user${num}        #create password
           echo “p@Ss${num}”
       done
It is inside the while loop that you should have your useradd syntax. Instead, you are still trying to recreate the format of the /etc/passwd file, though now you are just echoing it to the command-line. The $num variable is present as a counter, but you don't really need it, you can just use the $uid to count (one less variable to worry about).

Basically I hope you are just having a hard time digesting the way scripting works because it doesn't look like you've put too much effort into making this script work. Again I have to ask if you've even tried to run it?

Against my better judgement, here is the script I wrote. I just hope that when you take this into class and they ask you what each part is doing, you can explain it:

Code:
  #!/bin/bash
  path="/usr/sbin/"
  uid=1900;
  
  ${path}groupadd -g 747 IMFDelegates
  
  while [ ${uid} -lt 2000 ]; do
  	${path}useradd -c "Secret Agent $(($uid-1900))" -d /home/agent$(($uid-1900)) -g 747 -u ${uid} -s /bin/sh -p p@sS$(($uid-1900)) user$(($uid-1900))
  	let uid=uid+1
  done
larrenV2.003's Avatar, Join Date: Jun 2007
Light Poster
Yes i managed to run successfully! Thank you very much on your source code i get the group, user id and user name correct the only problem i face is the password whe i keyed in correctly it says login failed. Do i have to make changes in the shadow file for it to recognise the password?

anyway i was tasked to create a script to delete the useracct and groups

here is my code


Code:
#!/bin/bash
path="/usr/sbin/"
uid=1900;

while [ ${uid} -lt 2000 ];
do
 ${path}userdel -p "Secret Agent $(($uid-1900))" -d /home/agent$(($uid-1900)) -g 747 -u ${uid} -s /bin/sh -p  user$(($uid-1900))
let uid=uid+1
done

${path}groupdel -p 747 IMFDelegates
When i run it says error for userdel.....
munkyeetr's Avatar, Join Date: May 2007
Go4Expert Member
Again, look at the documentation for these commands.

man userdel or userdel --help
man groupdel or groupdel --help

Your answer is there.