Linux New Users Form

Following on from my recent upload on Linux scripting, I have yet again created a BASH script to make my Linux’ing life easier. This is also my second script created in BASH so I guess i’ve accomplished something by not running for the hills…

What I needed was a script to make creating FTP users easier on my CentOS box. Below is a list of things I needed the script to accomplish:

  • Get a username from a user prompt
  • Get a Description from a user prompt
  • Create the new user
  • Change the users password
  • Add the username to /etc/vsftpd.userlist
  • Add the username to /etc/vsftpd/chroot_list
  • Make a directory in the home folder of the user called “ftp”
  • Change the permissions and ownership on this directory
  • Make a directory in the “ftp” folder called “files”
  • Change the permissions and ownership on this directory
  • Ask the user to create a share or not
  • Get a share name from user prompt
  • Make a directory in the users “files” folder with the same name as the share name
  • Mount the share to the “files” directory
  • Ask if the new user is the owner of the share
    • if so then change the ownershipa and permissions on the share
    • if not then just change the permissions on the share
  • Finish

Here is my script for achieving these goals:

#!/bin/bash
#Creating new FTP users

##Gathering Variables
echo "Enter a username"
read Username

echo "Enter a description"
read Description

useradd -m -c "$Description" -s /bin/bash $Username

passwd $Username

echo "$Username" | tee -a /etc/vsftpd.userlist
echo "$Username" | tee -a /etc/vsftpd/chroot_list

mkdir /home/$Username/ftp
chown nobody:nobody /home/$Username/ftp
chmod a-w /home/$Username/ftp

mkdir /home/$Username/ftp/files
chown $Username:$Username /home/$Username/ftp/files
chmod 0700 /home/$Username/ftp/files

read -p "Create a share? [yn]: " CreateShare
if [[ $CreateShare = y ]] ; then

 read -p "Enter a share name: " ShareName

 mkdir /home/$Username/ftp/files/$ShareName
 mkdir /home/shares/$ShareName
 mount --bind /home/shares/$ShareName /home/$Username/ftp/files/$ShareName
elif [[ $CreateShare = n ]] ; then
 read -p "Enter the pre-existing share name: " ShareName
 mkdir /home/$Username/ftp/files/$ShareName
 if [ -d /home/shares/$ShareName ] ; then
  echo "Mounting share"
  mount --bind /home/shares/$ShareName /home/$Username/ftp/files/$ShareName
 else
  echo "Cannot find the share name : $ShareName"
 fi
else
 echo "Not a y or n"
fi

read -p "Is $Username the owner of this share? [yn]: " ShareOwner
if [[ $ShareOwner = y ]] ; then
 chown $Username:$Username /home/shares/$ShareName
 chmod 0775 /home/shares/$ShareName
else
 echo "$Username is not the owner of $ShareName"
 chmod 0775 /home/shares/$ShareName
fi

echo "Finished creating user : $Username" * Insert your code here

Hopefully somebody gains something from this, probably not though. Enjoy!

File & Folder Creation with File Sorting in Powershell

Long post today. Definitely my most complex and longest script. Basically what this does it is looks at two separate CSV files, one has a list of locations and the other has a list of users/ people with a location assigned to them as well as some other attributes. WARNING! this script is variables center!

First of all, lets import our locations and create a folder with that location name. This is the code I used for this:

$get_Location_csv = Import-Csv -path 'C:\#PATH TO LOCATIONS CSV FILE'
[string]$root_dir = 'C:\#PATH TO ROOT FOLDER'
try{
 foreach ($location_data in $get_Location_csv){
  $location = $location_data.location #obtain location info
  if(Test-Path -Path $root_dir$location){
   Write-Host "Path already exists!"
  }else{
   New-Item -path $root_dir -Name "$location" -ItemType directory -ErrorAction SilentlyContinue
  }
 }

So from this you can see that we import the folder with the CSV values, check if where the folder will be created already exists, if so then output text, if not then create the folder with the location name. In case you were wondering, here is the value from my CSV file for the locations:

1 location CSV entries

Now that the files have been created, we want to import the other CSV file which contains a list of the users we need sorting and where they need sorting to. Below is the code I used to get the values I wanted, create the file, name it, put data in it and then put it into the relevant folder based on the users location.

$get_user_data = Import-Csv -Path "$root_dir\#USER INFO CSV"

foreach ($i in $get_user_data){
 $first = $i.first
 $last = $i.last
 $username = $i.username
 $location = $i.location
 $datetime = [datetime] (Get-Date)
 $district = $i.district
 foreach($l in $location){
  New-Item -Path "$root_dir$location" -Value "$first $last $location District = $district $datetime" -Name "$username .txt" -ErrorAction Stop
 }
}

To end the code off so that it worked more cleanly in the case of an error I added the following:

}catch{
 $_.exception.message
 Write-Warning 'location:'$i 'was not created successfull!'
}

This catches any errors that might occur during the scripts operation

Again, in case your wondering what is actually in the users CSV file, you can see that in the below screenshot:

Users CSV file

 

This is the output of the script

Here you can see the folders the script created:Folders createdYou can see that the script created all of the locations in the location CSV file. (Leeds, Newcastle, Rotherham, Sheffield, York)

Here you can see the files that are in one of the folders (I chose Leeds):

Users created in leedsHere is what is in each of the files:

User information

From these screenshots you can see that the script has successfully created and sorted the files by the location defined to each user in the users CSV file. You could probably make this work from just the one CSV file, but I went for the long way round 🙂

Here is the entire script!

$get_Location_csv = Import-Csv -path 'C:\#PATH TO LOCATIONS CSV FILE'
[string]$root_dir = 'C:\#PATH TO ROOT DIRECTORY'
try{
 foreach ($location_data in $get_Location_csv){
  $location = $location_data.location
  if(Test-Path -Path $root_dir$location){
   Write-Host "Path already exists!"
  }else{
   New-Item -path $root_dir -Name "$location" -ItemType directory -ErrorAction SilentlyContinue
  }
 }

$get_user_data = Import-Csv -Path "$root_dir\Users.csv"

foreach ($i in $get_user_data){
 $first = $i.first
 $last = $i.last
 $username = $i.username
 $location = $i.location
 $datetime = [datetime] (Get-Date)
 $district = $i.district
 foreach($l in $location){
  New-Item -Path "$root_dir$location" -Value "$first $last $location District = $district $datetime" -Name "$username .txt" -ErrorAction Stop
 }
}
}catch{
 $_.exception.message
 Write-Warning 'location:'$i 'was not created successfull!'
}