14.4.2.7 Lab – Write Basic Scripts in Windows and Linux Answers

Last Updated on July 28, 2020 by Admin

14.4.2.7 Lab – Write Basic Scripts in Windows and Linux Answers

Lab – Write Basic Scripts in Windows and Linux (Answers Version)

Answers Note: Red font color or gray highlights indicate text that appears in the Answers copy only.

Objectives

In this lab, you will write basic scripts in different scripting languages to help understand how each language handles automating tasks.

Background / Scenario

Writing scripts to automate common administration functions saves time and gives the administrator flexibility to perform other tasks. In the lab, you will write three types of scripts that will perform similar tasks. Compare the different languages as you automate some simple task.

Required Resources

  • Windows PC
  • VM running a Linux distribution

Instructions

Step 1:  Create a Windows batch script.

  1. In a text editor, such as Notepad, save a text file named info.bat in your home directory (C:\Users\yourusername) with the following text:

Open configuration window

@echo off

echo Computer Name is: %computername%

echo Windows version is:

ver

echo CPU is: %PROCESSOR_IDENTIFIER%

echo Total memory is:

rem Windows Management Instrumentation Command (WMIC) is a command line utility that can retrieve information about local or remote computers. For more inline information, enter help wmic or wmic /? at the command prompt.

wmic ComputerSystem get TotalPhysicalMemory

echo The disks that are installed and their freespace:

wmic logicaldisk get size,freespace,caption

echo All the %computername% IP addresses

rem netsh is a command line scripting utility that allows the users to view or modify the network configurations of a running computer. For more inline information, enter nesh /? at the command prompt.

rem findstr is used for searching for a text string in files. For more inline information, enter findstr /? at the command prompt.

netsh interface ip show address | findstr “IP Address”

Close configuration window

  1. Open a command prompt and navigate to your home directory.
  2. List the content of your home directory and verify that the file info.bat is saved with the correct file. If not, rename the file, for example, rename info.bat.txt info.bat.
  3. At the prompt, enter info.bat to run the script.

Questions:

What was the output?

Answers will vary

Computer Name is: KDV-PC

Windows version is:

Microsoft Windows [Version 10.0.17763.437]

CPU is: Intel64 Family 6 Model 61 Stepping 4, GenuineIntel

Total memory is:

TotalPhysicalMemory

17091084288

 

The disks that are installed and their freespace:

Caption  FreeSpace     Size

C:       212692340736  997923037184

 

All the KDV-PC IP addresses

    IP Address:                           192.168.56.1

    IP Address:                           169.254.45.2

    IP Address:                           169.254.126.64

    IP Address:                           192.168.159.1

    IP Address:                           192.168.181.1

    IP Address:                           192.168.1.40

    IP Address:                           127.0.0.1

What are the %name% used for in the script?

Answers will vary. These are environment variables which hold values set by the operating system.

Identify what the following commands do in the script:

echo:

Type your answer here.

Displays on the screen whatever comes after it

findstr:

Type your answer here.

Look for a string of characters.

netsh:

Type your answer here.

Network shell allows to display and modify network settings.

ver:

Type your answer here.

Gives the current OS version.

wmic:

Type your answer here.

Windows management interface allows an administrator to view or modify settings.

Step 2:  Create a Powershell ISE script.

  1. Click Start, Search for PowerShell ISE and right-click the selection and click Run as an administrator.
  2. Verify that you are in your home directory: PS C:\Users\YourUsername
  3. To allow the script to run, enter Set-ExecutionPolicy RemoteSigned at the prompt. Click Yes to allow the script to run. The settings can be changed back to No after the script is complete.

PS C:\Users\YourUsername> Set-ExecutionPolicy RemoteSigned

  1. Choose File -> New and create a new script.
  2. Enter the following text into the Untitled.ps1 window and save it as info.ps1 in your home directory.

Open configuration window

Write-Output “Computer name is:”

get-content env:computername

Write-Output “Windows version is:”

(Get-WmiObject -class Win32_OperatingSystem).Caption

Write-Output “CPU is:”

Get-WmiObject Win32_Processor | findstr “Name”

Write-Output “Total Memory is:”

[Math]::Round((Get-WmiObject -Class win32_computersystem ComputerName localhost).TotalPhysicalMemory/1Gb)

Write-Output “The Disks that are installed and their freespace:”

Get-WmiObject -Class Win32_logicaldisk -FilterDriveType = ‘3’”

Write-Output “IPv4 addresses”

Get-NetIPAddress AddressFamily IPv4 | Sort-Object -Property InterfaceIndex | Format-Table

Close configuration window

Note: The command Get-NetIPAddress is not available in Windows 7.

Note: Within PowerShell ISE, you can press F1 or select Help > Windows PowerShell ISE Help to get more information.

  1. To see the functions of each command, click Add-ons, verify that Command is checked. In the Command tab, enter the name of the command in the Name field. Select the desired command and click the ? for more information regarding the desired command.

In Windows 7, click Help > Select Windows PowerShell Help. Select Windows PowerShell Cmdlet Help Topics. Search for the desired command.

  1. Enter .\info.ps1 at the PS prompt. Note: Make sure you are using the correct slash.

Open configuration window

PS C:\Users\YourUsername> .\info.ps1

Close configuration window

Question:

What is the output of the script?

Type your answer here.

Answers will vary.

PS C:\> .\info.ps1

Computer name is:

KDV-PC

Windows version is:

Microsoft Windows 10 Education

CPU is:

Name: Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz

Total Memory is:

16

The Disks that are installed and their freespace:

 

 

DeviceID     : C:

DriveType    : 3

ProviderName :

FreeSpace    : 212635987968

Size         : 997923037184

VolumeName   : Windows

 

IPv4 addresses

 

ifIndex IPAddress                                       PrefixLength PrefixOrigin SuffixOrigin AddressState PolicyStore

——- ———                                       ———— ———— ———— ———— ———–

1       127.0.0.1                                                  8 WellKnown    WellKnown    Preferred    ActiveStore

3       169.254.220.156                                           16 WellKnown    Link         Tentative    ActiveStore

5       192.168.159.1                                             24 Manual       Manual       Preferred    ActiveStore

7       169.254.160.146                                           16 WellKnown    Link         Tentative    ActiveStore

11      192.168.56.1                                              24 Manual       Manual       Preferred    ActiveStore

15      169.254.45.2                                              16 WellKnown    Link         Preferred    ActiveStore

17      192.168.1.40                                              24 Dhcp         Dhcp         Preferred    ActiveStore

24      169.254.2.220                                             16 WellKnown    Link         Tentative    ActiveStore

25      169.254.126.36                                            16 WellKnown    Link         Tentative    ActiveStore

29      169.254.18.19                                             16 WellKnown    Link         Tentative    ActiveStore

39      192.168.181.1                                             24 Manual       Manual       Preferred    ActiveStore

73      169.254.126.64                                            16 WellKnown    Link         Preferred    ActiveStore

  1. Compare the two scripts. Match the batch command to the PowerShell commands below:

Windows Batch Command

PowerShell Command

echo Computer Name is: %computername%

Write-Output “Computer name is:”

get-content env:computername

echo Windows version is:

ver

Write-Output “Windows version is:”

(Get-WmiObject -class Win32_OperatingSystem).Caption

echo CPU is: %PROCESSOR_IDENTIFIER%

Write-Output “CPU is:”

Get-WmiObject Win32_Processor | findstr “Name”

echo Total memory is:

Write-Output “Total Memory is:”

wmic ComputerSystem get TotalPhysicalMemory

[Math]::Round((Get-WmiObject -Class win32_computersystem –ComputerName localhost).TotalPhysicalMemory/1Gb)

echo The disks that are installed and their freespace:

Write-Output “The Disks that are installed and their freespace:”

wmic logicaldisk get size,freespace,caption

Get-WmiObject -Class Win32_logicaldisk -Filter “DriveType = ‘3’”

echo All the %computername% IP addresses

Write-Output “IPv4 addresses”

netsh interface ip show address | findstr “IP Address”

Get-NetIPAddressAddressFamily IPv4 | Sort-Object -Property InterfaceIndex | Format-Table

Step 3:  Create a BASH script.

A text editor is used to create an executable script. One of the text editor tools, vi, or the improved vi version, vim, is based on letter and number-based commands to modify text. For example, dd will delete the whole line on which the cursor is placed. 5dd would delete 5 lines. When vi is in command mode, input is interpreted as a command.

To enter insert mode at the current cursor position type i. To append text at the end of the current line, type a. To insert text on a new line below the current line, type o. Use the Esc key to exit out of insert mode to command mode.

To save a file in the vi editor use :w from command mode. To save and quit, type :wq. To quit without saving type :q!.

Depending on your version of Unix-like OS, you may find other text editor tool, such as nano, pico, and gedit. The text editing tools, such as vi, nano, and pico, are accessible through the command line; while the GUI-based text editors, like gedit, may be located via the application menu or the command line.

  1. Start up a Linux computer or VM.
  2. Use a text editor tool and create a file named info.sh in your home directory with the following text:

Open configuration window

#!/bin/bash

echo “Computer name is: “  $HOSTNAME

echo “Operating System is:”

cat /etc/os-release | grep PRETTY_NAME

echo “CPU is”

lscpu | grep “Model name:” | sed -r ‘s/Model name:\s{1,}//g’

echo “Total Memory is”

cat /proc/meminfo | grep  MemTotal

echo “The disks that are installed and their freespace

df -h

echo “All the” $HOSTNAME “IP addresses”

hostname -I

Close configuration window

  1. Open a terminal and navigate to your home directory. To make the script executable, enter chmod 755 info.sh at prompt.
  2. At the prompt, enter ./info.sh to execute the script.

Questions:

What is the output of the script?

Type your answer here.

Answers will vary.

Computer name is:  KDV-PC

Operating System is:

PRETTY_NAME=”Ubuntu 16.04.5 LTS”

CPU is

Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz

Total Memory is

MemTotal:       16690512 kB

The disks that are installed and their freespace

Filesystem      Size  Used Avail Use% Mounted on

rootfs          930G  732G  198G  79% /

root            930G  732G  198G  79% /root

home            930G  732G  198G  79% /home

data            930G  732G  198G  79% /data

cache           930G  732G  198G  79% /cache

mnt             930G  732G  198G  79% /mnt

none            930G  732G  198G  79% /dev

none            930G  732G  198G  79% /run

none            930G  732G  198G  79% /run/lock

none            930G  732G  198G  79% /run/shm

none            930G  732G  198G  79% /run/user

C:              930G  732G  198G  79% /mnt/c

All the KDV-PC IP addresses

192.168.56.1 192.168.159.1 192.168.181.1 169.254.45.2 169.254.126.64 192.168.1.40

What does the #!/bin/bash mean at the beginning of the script?

Type your answer here.

It tells the script which interpreter to use for the code.

What command would you use to learn more about the df and lscpu commands?

Type your answer here.

Use the man command to learn more. At the prompt, enter man df and man lscpu.nd of Document