2. Topics covered:
• What is shell scripting
• Types of Shell scripting
• Benefits of Shell scripting
• What is Bash Scripting
• Basic Bash Scripting
• Basic Linux Commands –
• Package installation
• LINUX commands
• Session monitoring LINUX commands
• NMAP usage and commands
3. • A shell script is a list of commands in a computer program that is
run by the UNIX shell which is a command line interpreter. A shell
script usually has comments that describe the steps. The different
operations performed by shell scripts are program execution, file
manipulation and text printing.
4. Types of Shell Scripting:
• Bash aka Bourne Again Shell
• CSH / C shell
• KSH or Korn Shell
• TCSH
5. Benefits of shell script
• Shell scripts don’t have to be boring:
• Most shell scripts are uninteresting. They don’t
show anything, nor have any colouring .
• Reusing:
• Why recurring the same statement in your shell
scripts every time? Develop an effective set of
functions and consist of that in your existing and
new shell scripts.
6. What is Bash scripting?
• Bash- is a type of interpreter that processes shell commands. A shell
interpreter takes commands in plain test format and calls OS services
to do something.
• A shell scripting is writing a program for the shell to execute and a
shell script is a file or program that shell will execute.
• Bash is one of many available UNIX shells. And is a
replacement/improvement of the original Borne-shell.
• Shell scripting is scripting in any shell,whereas bash scripting
specifically for Bash.
7. • Always available:
• Shell scripting can constantly be used, on each
program you encounter
• Readability:
• With a shell script, the possibility of creating
things really unreadable is much lower. Sure you
can use unique features of the shell, which
others don’t comprehend.
• e[ – Begin color changes
• Shell scripting is powerful:
Is is convenient, no compilation
required and helps almost every
single UNIX based system there is.
Merge it with the commonly
accessible tools like awk, grep and
sed and you have a great bias.
8. Setting Of PS1 variable: export PS1="e[0;32m[u@h W]$ e[0m"
• e[ – Begin color changes
• 0;32m – Specify the color code
• [u@h W]$ – This is the code for your normal BASH prompt (username@hostname Workingdirectory $)
• e[0m – Exit color-change mode
The first number in the color code specifies the typeface:
• 0 – Normal
• 1 – Bold (bright)
• 2 – Dim
• 4 – Underlined
The second number indicates the color you want:
• 30 – Black
• 31 – Red
• 32 – Green
• 33 – Brown
• 34 – Blue
• 35 – Purple
• 36 – Cyan
• 37 – Light gray
Additionally, if you combine the bright option with a color code, you get a lighter version of that color. For example, if you use color code 1;32, you would get light green instead of the normal green. If you use 1;33, you get yellow instead of brown.
The second number indicates the color you want:
• 30 – Black
• 31 – Red
• 32 – Green
• 33 – Brown
• 34 – Blue
• 35 – Purple
• 36 – Cyan
• 37 – Light gray
Additionally, if you combine the bright option with a color code, you get a lighter version of that color. For example, if
you use color code 1;32, you would get light green instead of the normal green. If you use 1;33, you get yellow instead
of brown.
9. Basic Linux Commands:
• pwd- get the full path of the current
working directory
• cd – Navigate to the last directory you
were working in
• cd – or just cd Navigate to the current
user home directory
• cd .. Go to the parent directory of
current directory
• ls –l : List the files and directories in the current directory in long
format
• ls –ld dir-name: - List information about the directory dir-name
instead of its contents
• ls –a : List all the files including the hidden ones
• lf –F: Appends a symbol at the end of a file name to indicate its
type (* means executable , /means directory, @ means symbolic
link, = means socket, I mean named pipe, > means door)
10. • ls –lt : List the files sorted by last
modified time with most recently
modified files showing at the top
• ls –lh: List file sizes in human readable
format
• ls –lR : shows all subdirectories
recursively
• Tree : Will generate a tree
representation of the file system
starting from the current directory
• cp –p source destination- Will copy the file from source to destination . –p:
stands for preservation. It preserves the original attributes of file while
copying like file owner, timestamp , group , permissions
• cp –R source_directory destination_directory – Will copy source directory to
specified destination recursively
• mv file1 file2 : In Linux there is no rename command as such. Hence mv
moves/renames the file file1 to file2
11. • rm –i filename : Asks you before
every file removal for
confirmation.
• rm –R dir-name : Will remove
the directory dir-name
recursively
• rm –rf dir-name: Will remove
the directory dir recursively,
ignoring non-existent files
• rmdir dir-name : Will remove the
directoty dir-name , if it’s empty.
• mkdir dir-name : Create a directory
dir-name
• mkdir –p dir-name/dir-name :
Create a directory hierarchy
• touch filename: Create a filename,
if it doesn’t exist, otherwise change
the timestamp of the file to current
time.
12. Basic Linux Commands :
• File/ Directory permissions and groups:
• chmod <specification> filename: Change the
file permissions , specifications
• chmod –R <specification> dir-name: Change
the perimissions of a directory recursively.
• chmod go=+r myfile : Add read permission for
the owner and the group
• chmod a+rwx myfile : allow all users to read , write or execute myfile.
• chmod go-r myfile: Remove read permissions from the group and others
• chown owner1 filename – change ownership of a file to user owner1
• chgrp grp_owner_filename: change primary group ownership of directory
dir-name grp_owner.
• chgrp-R grp_owner dir-name: change primary group ownership of directory
dir-name to group grp_owner
14. Basic Bash Scripting:
• Conditional execution:
git commit && git push
git commit || echo
• Conditions:
if [[-z “$string”]];
then
echo “String is empty”
elif [[-n “$string”]]; then
Echo “String is not empty”
fi
• Brace expansion:
• echo {A,B}.js
• {A,B} – Same as A B
• {A, B}.js – Same as A.js B.js
• {1…5}- Same as 1 2 345
• Strict mode:
• Set –euo pipefail
• IFS=$’nt’
15. Useful commands with LINUX machine:
1. ss: used to dump socket statistics.
• [root@ins-qasvr-common05 Automation]# ss -a | grep ssh
• LISTEN 0 128 :::ssh :::*
• LISTEN 0 128 *:ssh *:*
• ESTAB 0 0 172.31.201.214:37118 172.31.146.95:ssh
• ESTAB 0 0 172.31.201.214:51378 172.31.144.92:ssh
• [root@ins-qasvr-common05 Automation]#
We have active SSH connections from 172.31.146.95
16. • 2) Last : Searches back through
the file /var/log/wtmp (or the
file designated by the -f flag) and
displays a list of all users logged
in (and out) since that file was
created.
• 3) W: Displays information
about the users currently on
the machine, and their
processes
[root@node3 ~]# last -a | grep -i still
deepak pts/1 Fri May 31 16:58 still logged in 10.0.2.31
root pts/2 Fri May 31 16:50 still logged in 10.0.2.30
root pts/0 Fri May 31 09:17 still logged in 10.0.2.2
[root@node3 ~]# w
17:01:41 up 7:44, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 10.0.2.2 09:17 9:41 0.31s 0.00s less -s
deepak pts/1 10.0.2.31 16:58 3:06 0.03s 0.03s -bash
root pts/2 10.0.2.30 16:50 5.00s 0.07s 0.02s w
17. IP address to find who using the SSH server
• echo $SSH_CLIENT | awk ‘{print$1}’
• Provides the IP address of the user who logged in through SSH
• Pinky
• Command lists the login users with IP details
18. Environment variables:
• HOME – users home directory
• PATH – colon separated list of directories used by the shell when
searching for programs to run when typed on the command line
• MAIL – users mail filename, which is used to check new mail
• MAILPATH – colon-separated list of filenames used to check for new
mail
• MAILCHECK – how often to check for new mail
• PS1 – user’s shell prompt displayed on the command line
19. • PS2 – secondary shell prompt when waiting for more input from the
user
• CDPATH – colon-separated list of directories used by the cd command
when searching , for the directory specified to change into
• SHELL – name of the current user’s shell
• DISPLAY- specify the display name for X WINDOWS applications
• $$ - process ID of the current shell
• $! – process ID of the most recent background
• $? – return code from the previously executed command
20. Setting Shell variables:
• Export command is used to
export variable names to new
sub-shells. The user can also
create his/her own variables by
setting them to an initial value.
• $PATH = /bin:/usr/sbin:
/usr/etc:/usr/ucb:/usr/X11R6/bin
• PS1 = ‘Command>’
• Command > MYNAME = “John
Smith”
• Command > echo $MYNAME
• Command> export PATH PS1
21. Finding files and directories with grep utility:
• find . –name resume.txt –print
• grep –ilnv search-pattern file-list: search-pattern is the word being
searched for and file-list is the list of files on which to perform the
search.
22. Network Administration related commands:
• Host address – IP address of your system ; it has a network part to identify the network you
are on and a host part to identify your own system.
• Network address - IP address of your network
• Broadcast address – IP address for sending messages to all hosts of your network at once
• Gateway address – IP address of your gateway system if you have one
• Domain nameserver addresses: IP addresses of domain nameservers that your network uses
• Netmask – Network part of your host IP address set to 255 with host part set to 0
23. File:
• /etc/hosts: Associates hostnames with IP addresses
• /etc/networks: Associate's domain names with network addresses
• /etc/rc.d/init.d/inet : Script to configure your ethernet interface when you boot up
• /etc/host.conf: List resolver options
• /etc/hosts: Lists domain names for remote hosts with their IP addresses
• /etc/resolv.conf: Lists domain nameserver names, IP addresses and domain names where remote hosts may be located.
• /etc/protocols: Lists protocols available on your system
• /etc/services: Lists available network services such as ftp and telnet
• /etc/hostname: Holds the name of your system