- 1 Shell
- 2 The Terminal
- 3 What is shell scripting
- 4 How do we create a shell script
- 5 Why do we use shell scripting
- 6 Windows scripts
- 7 How does Linux shell scripting differ from Windows shell scripting
We first need to define what the shell is:
In computing, a shell is a user interface for access to an operating system's services. In general, operating system shells use either:
- Command-line shell or command-line interface (CLI)
- Graphical shell or graphical user interface (GUI)
Depending on a computer's role and particular operation. It is named a shell because it is the outermost layer around the operating system.
Simply put, the shell is a program that takes commands from the keyboard (standard input) and gives them to the operating system to perform. In the old days, it was the only user interface available on a Unix-like system such as Linux. Nowadays, we have graphical user interfaces (GUIs) in addition to command-line interfaces (CLIs) such as the shell.
On most Linux systems a program called bash (which stands for Bourne Again SHell, an enhanced version of the original Unix shell program, sh, written by Steve Bourne) acts as the «shell» program. Besides bash, there are other shell programs that can be installed in a Linux system. These include: ksh, tcsh and zsh.
NAME bash - GNU Bourne-Again SHell SYNOPSIS bash [options] [command_string | file] COPYRIGHT Bash is Copyright (C) 1989-2016 by the Free Software Foundation, Inc. DESCRIPTION Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file. Bash also incorporates useful features from the Korn and C shells (ksh and csh). Bash is intended to be a conformant implementation of the Shell and Utilities portion of the IEEE POSIX specification (IEEE Standard 1003.1). Bash can be configured to be POSIX- conformant by default. OPTIONS . . .
It's a program called a terminal emulator. This is a program that opens a window and lets you interact with the shell. There are a bunch of different terminal emulators you can use. Most Linux distributions supply several, such as: gnome-terminal, konsole, xterm, rxvt, kvt, nxterm, and eterm.
What is shell scripting
A shell script is a computer program designed to be run by a command-line shell.
Para empezar vamos a definiendo un poco la noción de Scripting: Basically, all scripting languages are programming languages. One of the clearest differences between the two is that scripting languages do not require the compilation step and are rather interpreted.
Some types of scripts are generally described as composed of a series of commands or basic control statements. A shell script, for example, contains a sequence of shell commands. The shell reads this file and carries out the commands as though they have been entered directly on the command line.
The shell is somewhat unique, in that it is both a powerful command-line interface to the system and a scripting language interpreter. As we will see, most of the things that can be done on the command line can be done in scripts, and most of the things that can be done in scripts can be done on the command line.
Scripting can be very useful and help a system administrator save time by helping to automate certain tasks.
How do we create a shell script
- We create scripts by experimenting and running commands one at a time in a test environment
- Once we have one or more commands that can perform the task that we want to automate we can put those commands into a script file
- Once the script is written we can run that script on one or more servers which can save us lots of time and effort
- But remember, the scripts get built through testing one command at a time
Why do we use shell scripting
- Sys Admin task automation: Scripting can be very useful and help a system administrator save time by helping to automate certain tasks.
- Backups: We can schedule a complex automatic backup, creating snapshots.
- Dumping a MySQL database for backup.
- Rapidly provision servers with certain features: programs installed, firewall configurations, etc.
- We can, for example, configure an entire development environment by running a shell scripting.
- Configure a Web server to run a specific web application
- System monitoring:
- Find out what processes are eating up your system resources.
PowerShell and PowerShell scripting
Windows PowerShell is a command-line shell and scripting language developed by Microsoft for purposes of task automation and configuration management.
PowerShell is a task automation and configuration management framework, consisting of a command-line shell and scripting language. Unlike most shells, which accept and return text, PowerShell is built on top of the .NET Common Language Runtime (CLR) and accepts and returns .NET objects. This fundamental change brings entirely new tools and methods for automation.
- It is based on the .NET framework.
- PowerShell uses commands called cmdlets
- There are hundreds of cmdlets that are available to help you configure and provision servers quickly
- PowerShell has modules that can be added similar to the way Linux has packages in their Advanced Package Tool (APT)
A PowerShell script is a computer program designed to be run by PowerShell. The PowerShell Integrated Scripting Environment (ISE) allows us to do write a script that contains a collection of PowerShell cmdlets.
How to write a PowerShell script:
- It is easy to run PowerShell commands one at a time... but what if you want to run an entire collection of related cmdlets that will work together to perform some useful task or automate a series of tasks?
- The old tried-and-true method is to run batch files which are collections of commands that run when you execute the batch file. There is just one problem here... PowerShell will only accept commands that are in the correct format. Therefore we need to create a PowerShell script that ends with .ps1
- You can open and edit Windows PowerShell files in the Script Pane of the ISE.
- You can also create script data files (.psd1) and script module files (.psm1). These file types are syntax colored in the Script Pane editor.
- Other common file types you may open in the Script Pane are configuration files (.ps1xml), XML files, and text files
Some PowerShell scripts examples
Sets IPs SNMs DFG(1) & DNSs for one NIC / set firewall rules / Auto Update / Rename PC & Activate:
# Setting the IP address $Octet = Read-Host -Prompt 'Input the last octet of IP address' New-NetIPAddress -InterfaceAlias "Ethernet" -AddressFamily IPv4 -IPAddress 192.168.88.$Octet -PrefixLength 24 -DefaultGateway 192.168.88.1 # Setting the DNS servers $DNS = Read-Host -Prompt 'Input the last octet of DNS Server IP address' Set-DnsClientServerAddress -InterfaceAlias Ethernet -ServerAddresses 192.168.88.$DNS,18.104.22.168 # Netsh is a command-line scripting utility that allows you to display or modify the network configuration of a computer that is currently running. # Setting firewall rules netsh advfirewall firewall set rule group="network discovery" new enable=yes netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes $WUSettings = (New-Object -com "Microsoft.Update.AutoUpdate").Settings $WUSettings.NotificationLevel=2 $WUSettings.save() # Setting server name $Server = Read-Host -Prompt 'Input your server name' Rename-Computer -NewName $Server DISM /online /Get-TargetEditions DISM /online /Set-Edition:ServerDatacenter /ProductKey:Y4TGP-NPTV9-HTC2H-7MGQ3-DV4TW /AcceptEula Restart-Computer
Automates AD DS installation for new Domain Controller: AD DS (Active Directory Domain Services)
# Installing AD DS Install-windowsfeature -name AD-Domain-Services -IncludeManagementTools Import-Module ADDSDeployment # Creatting a new forest: Install-ADDSForest -CreateDnsDelegation:$false -DatabasePath "C:\Windows\NTDS" -DomainMode "Win2012R2" -DomainName "Domain39.abc" -DomainNetbiosName "DOMAIN39" -ForestMode "Win2012R2" -InstallDns:$true -LogPath "C:\Windows\NTDS" -NoRebootOnCompletion:$false -SysvolPath "C:\Windows\SYSVOL" -Force:$true Restart-Computer
Creating a zone for the DigiTech.cct website:
# This is run on the Domain Controller which already has DNS installed Add-DnsServerPrimaryZone -Name "DigiTech.cct" -ReplicationScope "Forest" -PassThru # The next line is an A-record customized for web cluster address 172.16.0.200 Add-DnsServerResourceRecordA -Name "www" -ZoneName "DigiTech.cct" -AllowUpdateAny -IPv4Address "172.16.0.200" -TimeToLive 01:00:00
Add IIS Web Services to a server:
# IIS Web Server Install-WindowsFeature -Name Web-Server # Management Tools Install-WindowsFeature -Name Web-Mgmt-Tools
This script would be run on all cluster nodes to add each node to the Domain:
Add-Computer -domainname Digitech.cct -Credential administrator -restart -force
How does Linux shell scripting differ from Windows shell scripting
- Linux shell scripts are case sensitive while Windows PowerShell scripts don't