Original Script. Display commands to manually creating an account. #!/bin/bash

Similar documents
COMP 4/6262: Programming UNIX

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

Everything about Linux User- and Filemanagement

INd_rasN SOME SHELL SCRIPTING PROGRAMS. 1. Write a shell script to check whether the name passed as first argument is the name of a file or directory.

Week 6 Lesson 1: Control-Flow Statements (Continued)

LOG ON TO LINUX AND LOG OFF

CSC 2500: Unix Lab Fall 2016

Vi & Shell Scripting

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Bash shell programming Part II Control statements

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

Lab #12: Shell Scripting

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University

9.2 Linux Essentials Exam Objectives

Computer Systems and Architecture

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

Exploring UNIX: Session 3

Windshield. Language Reference Manual. Columbia University COMS W4115 Programming Languages and Translators Spring Prof. Stephen A.

Conditional Control Structures. Dr.T.Logeswari

Shell programming. Introduction to Operating Systems

Prof. Navrati Saxena TA: R. Sachan Bharat Jyoti Ranjan

bash Execution Control COMP2101 Winter 2019

Chapter 8: Security under Linux

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Overview of the UNIX File System

5/8/2012. Encryption-based Protection. Protection based on Access Permission (Contd) File Security, Setting and Using Permissions Chapter 9

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

CSE II-Sem)

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Assume that username is cse. The user s home directory will be /home/cse. You may remember what the relative pathname for users home directory is: ~

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

Shell script/program. Basic shell scripting. Script execution. Resources. Simple example script. Quoting

Bash scripting basics

Unix Shell scripting. Dr Alun Moon 7th October Introduction. Notation. Spaces

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

Shell scripting and system variables. HORT Lecture 5 Instructor: Kranthi Varala

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

Unix Introduction to UNIX

UNIX System Programming Lecture 3: BASH Programming

CS Unix Tools & Scripting

Computer Systems and Architecture

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Shell Programming (bash)

While Statement Examples. While Statement (35.15) Until Statement (35.15) Until Statement Example

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

Useful Unix Commands Cheat Sheet

A Brief Introduction to the Linux Shell for Data Science

Assignment 3, Due October 4

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

CSE 15L Winter Midterm :) Review

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012

CST8207: GNU/Linux Operating Systems I Lab Six Linux File System Permissions. Linux File System Permissions (modes) - Part 1

Linux shell programming for Raspberry Pi Users - 2

OPERATING SYSTEMS LAB LAB # 6. I/O Redirection and Shell Programming. Shell Programming( I/O Redirection and if-else Statement)

How to monitor RedHat Enterprise Linux 5 or 6 using Microsoft System Center Operations Manager (SCOM) 2012 SP1 - Part 1

Lab 5a Shell Script Lab 4 Using Arithmetic Operators in shell script

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Outline. UNIX security ideas Users and groups File protection Setting temporary privileges. Examples. Permission bits Program language components

Environment Variables

CS1101: Lecture 9 The Shell as a Programming Language

Understanding bash. Prof. Chris GauthierDickey COMP 2400, Fall 2008

Processes and Shells

Bash Shell Programming Helps

12.1 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION Writing a Simple Script Executing a Script

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

CS214 Advanced UNIX Lecture 4

Shell Control Structures

MANAGING THE NONUNIFORM BEHAVIOUR OF TERMINALS AND KEYBOARDS. : WHEN THINGS GO WRONG

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

Introduction to remote command line Linux. Research Computing Team University of Birmingham

5/20/2007. Touring Essential Programs

Chapter-3. Introduction to Unix: Fundamental Commands

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

Shell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh

Scripting. More Shell Scripts. Adapted from Practical Unix and Programming Hunter College

Shell Programming Overview

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

COMP 2718: Shell Scripts: Part 2. By: Dr. Andrew Vardy

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

On successful completion of the course, the students will be able to attain CO: Experiment linked. 2 to 4. 5 to 8. 9 to 12.

Exercise Sheet 2. (Classifications of Operating Systems)

System Administration

Shifter Configuration Guide 1.0

CSCI 2132 Software Development. Lecture 5: File Permissions

Assignment 2. Summary. Some Important bash Instructions. CSci132 Practical UNIX and Programming Assignment 2, Fall Prof.

BIOINFORMATICS POST-DIPLOMA PROGRAM SUBJECT OUTLINE Subject Title: OPERATING SYSTEMS AND PROJECT MANAGEMENT Subject Code: BIF713 Subject Description:

Linux Shell Script. J. K. Mandal

Perl and R Scripting for Biologists

More Scripting Techniques Scripting Process Example Script

Course 144 Supplementary Materials. UNIX Fundamentals

Unix basics exercise MBV-INFX410

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Race Condition Vulnerability Lab

Lab 4: Shell scripting

Getting Started with MySQL

Introduction to the UNIX command line

Shell Programming. Introduction to Linux. Peter Ruprecht Research CU Boulder

Transcription:

To show some of the basic components of shell scripting, we are going to take a common task, creating a new user, and design a script to help automate this process. Original Script Display commands to manually creating an account Program: makeuser Usage: makeuser Purpose: To automate the creation of a user account History: Created Edit system files echo "passwd:" echo "smo1:x:1000:100:scott1:/root/n321/home/smo1:/bin/bash" echo "shadow:" echo "smo1:*:::::::" Create home directories echo "cp -r /etc/skel /root/n321/home/smo1" echo "chown -R 1000:100 /root/n321/home/smo1" echo "chmod -R go-rwx /root/n321/home/smo1" done

Revision 1 Write to temporary data files and directories. Never write to live (production) areas when debugging scripts. Note: code segments that are in bold indicate changes from the previous example. Program: makeuser Usage: makeuser Purpose: To automate the creation of a user account History: Created Wrote to local files Edit system files echo "smo1:x:1000:100:scott1:/root/n321/home/smo1:/bin/bash" >> passwd echo "smo1:*:::::::" >> shadow Create home directories cp -r /etc/skel /root/n321/home/smo1 chown -R 1000:100 /root/n321/home/smo1 chmod -R go-rwx /root/n321/home/smo1 done

Revision 2: As scripts become more complex, it is sometimes difficult to find all the occurrences of hard coded data (i.e. file names) when you need to make a change. A way to minimize this is to create a number of global variables at the beginning of the script for those items. You only need to make the change in one place. Program: makeuser Usage: makeuser Purpose: To automate the creation of a user account History: Created Wrote to local files Added global variables Account Variables USERNAME=smo2 PASSWORD="*" UUID=1002 UGID=100 GCOS=Scott2 HOME=/root/n321/home/smo2 SHELL=/bin/bash File variables PASSWD=passwd SHADOW=shadow SKEL=/etc/skel Edit system files echo "$USERNAME:x:$UUID:$UGID:$GCOS:$HOME:$SHELL" >> $PASSWD echo "$USERNAME:*:::::::" >> $SHADOW Create home directories cp -r $SKEL $HOME chown -R $UUID:$UGID $HOME chmod -R go-rwx $HOME done

Revision 3: Thus far, we have hard coded user account information directly into the script. Unfortunately, every time we need to create a new user, we would have to edit the script. A better approach would be to be able to include key pieces of information directly on the command line. Program: makeuser Usage: makeuser <username> <uid> <gcos> Purpose: To automate the creation of a user account History: Created Wrote to local files Added global variables Added command line arguments Account Variables USERNAME=$1 PASSWORD="*" UUID=$2 UGID=100 GCOS=$3 HOME=/root/n321/home/$USERNAME SHELL=/bin/bash File variables PASSWD=passwd SHADOW=shadow SKEL=/etc/skel Edit system files echo "$USERNAME:x:$UUID:$UGID:$GCOS:$HOME:$SHELL" >> $PASSWD echo "$USERNAME:*:::::::" >> $SHADOW Create home directories cp -r $SKEL $HOME chown -R $UUID:$UGID $HOME chmod -R go-rwx $HOME done

Revision 4: While script variables tend to be used as strings, sometimes it is helpful to treat them as integers so that we can basic mathematical operations on them (i.e. loop counters). It is also convenient to be able to set variables to the output of a UNIX command line. Program: makeuser Usage: makeuser <username> <gcos> Purpose: To automate the creation of a user account History: Created Wrote to local files Added global variables Added command line arguments Automatically generate UID Account Variables USERNAME=$1 PASSWORD="*" UGID=100 GCOS=$2 HOME=/root/n321/home/$USERNAME SHELL=/bin/bash File variables PASSWD=passwd SHADOW=shadow SKEL=/etc/skel Get new UID LUID=`tail -1 $PASSWD cut -d: -f3` UUID=`expr 1 + $LUID` Edit system files echo "$USERNAME:x:$UUID:$UGID:$GCOS:$HOME:$SHELL" >> $PASSWD echo "$USERNAME:*:::::::" >> $SHADOW Create home directories cp -r $SKEL $HOME chown -R $UUID:$UGID $HOME chmod -R go-rwx $HOME done

Revision 5: Assuming we enter everything correctly, our script should create new user accounts. What happens if we make a mistake on the command line? Depending on what we type, we could create a lot of problems. A way to minimize this is to add code to make sure there are no errors. This revision shows an initial example. Program: makeuser Usage: makeuser <username> <gcos> Purpose: To automate the creation of a user account History: Created Wrote to local files Added global variables Added command line arguments Automatically generate UID Added some error checking Enough Arguments? if [ $ -ne 2 ] echo "Usage: $0 <username> <Full name>" else echo "Creating account for $2" fi Account Variables USERNAME=$1 PASSWORD="*" UGID=100 GCOS=$2 HOME=/root/n321/home/$USERNAME SHELL=/bin/bash File variables PASSWD=passwd SHADOW=shadow SKEL=/etc/skel Get new UID LUID=`tail -1 $PASSWD cut -d: -f3` UUID=`expr 1 + $LUID` Edit system files

echo "$USERNAME:x:$UUID:$UGID:$GCOS:$HOME:$SHELL" >> $PASSWD echo "$USERNAME:*:::::::" >> $SHADOW Create home directories cp -r $SKEL $HOME chown -R $UUID:$UGID $HOME chmod -R go-rwx $HOME done

Revision 6: Script output has already been addressed in the first version of our script but what if the script needs some information from the user running it that wasn t provided on the command line. There needs to be a method to enable a user to enter it if prompted. In this revision, we had a new command line option, -v, that, if present, will prompt the user to verify that the new account should be created. Also introduced in this revision is code that allows us to different things based on a range of conditions. Program: makeuser Usage: makeuser [-v] <username> <gcos> Purpose: To automate the creation of a user account History: Created Wrote to local files Added global variables Added command line arguments Automatically generate UID Added some error checking Add verification before creating account Enough Arguments? case $ in 2) 3) *) USERNAME=$1 GCOS=$2 if [ $1!= "-v" ] echo "Usage: $0 <username> <Full name>" else echo -n "Are you sure? (Y N): " read ANSWER if [ $ANSWER!= "Y" -a $ANSWER!= "y" ] exit 3 fi fi USERNAME=$2 GCOS=$3 echo "Usage: $0 <username> <Full name>"

esac Account Variables PASSWORD="*" UGID=100 HOME=/root/n321/home/$USERNAME SHELL=/bin/bash File variables PASSWD=passwd SHADOW=shadow SKEL=/etc/skel Get new UID LUID=`tail -1 $PASSWD cut -d: -f3` UUID=`expr 1 + $LUID` Edit system files echo "$USERNAME:x:$UUID:$UGID:$GCOS:$HOME:$SHELL" >> $PASSWD echo "$USERNAME:*:::::::" >> $SHADOW Create home directories cp -r $SKEL $HOME chown -R $UUID:$UGID $HOME chmod -R go-rwx $HOME done

Revision 7: The final revision of our code provides a additional error checking if the script is run with the create user verification option. In the previous revision, the account is only created if Y or y is entered. If anything else is typed, it is assumed that it was the same as typing N or n. In this revision, we want to make sure that only yes or no responses are entered. Program: makeuser Usage: makeuser [-v] <username> <gcos> Purpose: To automate the creation of a user account History: Created Wrote to local files Added global variables Added command line arguments Automatically generate UID Added some error checking Add verification before creating account Enough Arguments? case $ in 2) 3) USERNAME=$1 GCOS=$2 if [ $1!= "-v" ] echo "Usage: $0 <username> <Full name>" else while [ 1 ] do echo -n "Are you sure? (Y N): " read ANSWER case $ANSWER in Y y) break N n) exit 3 *) esac done

esac *) fi USERNAME=$2 GCOS=$3 echo "Usage: $0 <username> <Full name>" Account Variables PASSWORD="*" UGID=100 HOME=/root/n321/home/$USERNAME SHELL=/bin/bash File variables PASSWD=passwd SHADOW=shadow SKEL=/etc/skel Get new UID LUID=`tail -1 $PASSWD cut -d: -f3` UUID=`expr 1 + $LUID` Edit system files echo "$USERNAME:x:$UUID:$UGID:$GCOS:$HOME:$SHELL" >> $PASSWD echo "$USERNAME:*:::::::" >> $SHADOW Create home directories cp -r $SKEL $HOME chown -R $UUID:$UGID $HOME chmod -R go-rwx $HOME done

Now that we have a working makeuser script, we can create a second one called makeclass that will enable us to create a group of users at one time. This script will read a file and parse it line by line pass the appropriate arguments to makeuser. The format of the input file is username:gcos. Original Script: We can pass the output of a command that can be used to feed a for loop. Program: makeclass Usage: makeclass <user-file> Purpose: Create a group of users History: Created USERFILE=$1 if [ $ -ne 1 ] echo "Usage: $0 <user-file-list>" elif [! -f $USERFILE ] echo "$USERFILE not found" exit 2 fi for NEWUSER in `cat $USERFILE` do NEWUSERNAME=`echo $NEWUSER cut -d: -f1` NEWGCOS=`echo $NEWUSER cut -d: -f2` done./makeuser8 $NEWUSERNAME $NEWGCOS done

Revision 1: One problem with the original approach is that any spaces on a line will be seen as an item separator. While this is exactly what we want when parsing most commands, it is not necessarily good when dealing with files. This revision includes a method to specifically read a file for input. Program: makeclass Usage: makeclass <user-file> Purpose: Create a group of users History: Created USERFILE=$1 if [ $ -ne 1 ] echo "Usage: $0 <user-file-list>" elif [! -f $USERFILE ] echo "$USERFILE not found" exit 2 fi while read NEWUSER do NEWUSERNAME=`echo $NEWUSER cut -d: -f1` NEWGCOS=\"`echo $NEWUSER cut -d: -f2`\"./makeuser8 "$NEWUSERNAME \"$NEWGCOS\"" done < $USERFILE done