COMPUTER PROGRAMmING

profilepatrick07
CIS206SAGuide03281019.docx

Part 4: Scripting and Virtualization (due Week 7)

Objectives

1. To learn scripting on Windows and Linux

2. To add virtualization with a Linux distribution

Steps

Part 1—Windows Scripting

Basic Script: Scripting is useful for small programming projects or quick tasks. Often, these programs are short and meant for small problems. Unlike compiled programming languages, scripting languages are generally interpreted. Batch files or scripts are created to automate tasks and may contain several commands in one file. Scripts can be created in Notepad. These are short files that run each command in sequence at file execution. The windows command-line interface can be used to run scripts.

Below are some commands.

Echo = Displays a message in the batch file

Echo. displays a blank line

@command turns off the display of the current command

@echo off = does not echo back text

cls = clears your screen

:: = Adds comments to your code; this line will not be displayed

Start = used to start a windows application

Creating a Basic Script

cls

@echo off

::Your Name

echo "Creating a data dump file"

ipconfig /all > C:\Scripts\config_info.txt

echo end of script

Open Notepad by going to Start-> All Programs -> Accessories-> Notepad.

Type the above script into Notepad.

Create a directory named Scripts on the C:\ drive. Save this file in the C:\Scripts folder as myscript.cmd.

Do not close your Notepad file. To run, open a command prompt by typing cmd in the Search Programs and Files box when you click the Start button or search for cmd.

Change directory to the C:\Scripts folder by typing the following.

cd c:\Scripts

Then type in the following.

myscript.cmd

The script should run and will create a file.

Use the dir command to see what files are created.

Keep both the Notepad file and the command prompt open for the next step.

You can also shut down a computer from a script. This is helpful for remote shutdown in a networking situation. Add the following commands to your script and save it in Notepad. (Note: The ping command, though normally used for networking, here waits 4 seconds.)

shutdown /s /t 60 /c "Local shutdown in 1 minute!"

ping -w 1000 0.0.0.0 > nul

shutdown /a

echo "Shutdown has been aborted"

Click back to the command prompt.

Type in myscript.cmd to run the script.

You should see the script attempt to shut down, then abort the shutdown.

Keep both your Notepad and command prompt open.

Environment variables are built-in system variables available for all Windows processes describing users, paths, and so on.

Some common environment variables are as follows.

%PATH% = contains a list of directories with executable files, separated by semicolons. To add a path:

SET PATH = %PATH%;C:\Windows\Eclipse

%DATE% and %TIME% = current date and time

%RANDOM% = returns a random number between 0 and 32767

%WINDIR% = points to the windows directory C:\Windows

%PATHEXT% = displays executable file extensions ie .com, .exe, .bat, .cmd, .vbs, .vbe, .js, .wsf, .wsh

%USERNAME% = the current user

Add the following commands to your script.

echo "The date is "

echo %DATE%

echo "The path is "

echo %PATH%

Run your script in the command prompt.

Now modify the script to display all the files in the directory (dir command) before the shutdown command and display two more environment variables.

Take a screenshot of your script after running it and paste it in the lab report. Also paste your final script.

The next script will be creating a backup script. In order to do this you will need to determine what files you want to back up, and the destination. You may want to back up school files, pictures, important documents, and so on. The files you will backup will need to be in a specific folder (i.e., C:\Users\Susie\Documents\Files). Then you will need a separate place to back up your data. It could be a separate folder on your C: drive, or it could be an external drive or USB drive.

You will also give your backup folder a custom name with the date. However, to do this you will need to strip out the / signs in the date. If you keep the / signs in the date, then when you create your backup folder there will be several subfolders. For example, see the date below:

If we were to make a folder with the name backup_%date% it would have a confusing set of spaces and slashes:

So we need to strip out the month, day, and year. We do this by extracting a position and number of characters. So this command: %date:~4,2% will start at the fourth character and extract two characters with the result of 07. To pull out the month, day, and year, use the following command. %date:~4,2%%date:~7,2%%date:~10,4%

We will use this to create our backup folder name.

Next, we will use the xcopy command to copy all files and subdirectories. The xcopy command works like this: xcopy source destination

So if you want to copy files from your C:\users\gina\Documents\work\ to your E:\backup (E: is the USB drive in this case) you would use this command: xcopy C:\users\gina\Documents\work\* E:\backup\* /S /Y /I

Note that switch S will copy all files and folders including subfolders, Y will overwrite files and not ask for confirmation, I will automatically create subfolders.

Below is an example backup script. Replace Sheldon Cooper with your own name and replace the pathnames of the source and destination.

Take a screenshot of your script and paste it in the lab report. Also paste a screenshot showing your folder created for your backup.

Part 2 Linux Virtualization and Scripting

For this part of the lab, we will be using the Linux operating system. If you do not have a Linux environment, you can set one up using VMWare using the steps below:

1. Install VMware workstation player on your computer. Download the install files and work through the wizard to install VMware. https://www.vmware.com/products/workstation-player/workstation-player-evaluation.html

2. Download the linux iso from the Files section of your course. It is named: ubuntu-18.04.1-desktop-amd64.iso This is a distribution of Linux named Ubuntu. Copy the file downloaded to a folder so you will remember where it is stored. I created a folder named Ubuntu and copied the file in there.

Figure 2: Linux ISO

3. Next open VMWare and click the “Create a New Virtual Machine”

Figure 3: New Virtual machine

4. Choose the Installer disk file and browse for the location of Ubuntu:

Figure 4: Browse for disc file

5. Personalize Ubuntu with your name and password. IMPORTANT: write down your password so you will remember it

Figure 5: Personalize Linux

6. Follow the rest of the defaults to install the Linux image. You may get this virtualization error:

Figure 6: Virtualization error

7. If you get this virtualization error, you will need to go to the BIOS and enable virtualization.

8. Once Linux is installed explore the desktop

Figure 7: Applications

9. Click on the 9 dots on the lower left corner to see all of the applications.

Basic Script: We will be writing a script to create a directory and create a file within that directory. The file we will create will be the output of the ifconfig command. This command is similar to the ipconfig in Windows. We will direct the output of the ifconfig command into a new file using the file redirect >.

First we must install the net-tools.

Open the Terminal Window (it is one of the programs you see when you click on the 9 dots). Click on Terminal to open the terminal window

At the terminal window type type the following command:

sudo apt install net-tools

When it is finished updating the net-tools you will see the prompt again:

Next, we will write a script we will use the nano editor.

At the Linux prompt, enter this command.

nano create.sh

Type in the following (please note the ls—l has two lowercase Ls):

#This script named create.sh creates a directory and a file

mkdir newDir

echo “This is a script to create a directory”

ls -l # “l” as in larry, “s” as in sam, then minus sign then “l” as in larry

ifconfig > ./newDir/config.txt

To save the file, press Control and O. Press enter to confirm the name. The ^ symbol in nano stands for the control key. Press Control and X to exit.

To run a script type the following.

./create.sh

However, you will receive a permission denied error.

You will need to change permissions using the chmod command.

Type in the following command.

chmod 755 create.sh

Now try to run the script with the following.

./create.sh

Now that your script has run and created a file for you automatically, let's check the contents of that file. It is located in the newDir directory. Change directory to newDir and then use cat to view the contents of the config.txt file. The commands are below.

cd newDir

cat config.txt

Take a screenshot of the script and the result of running the script. To view the script, you will need to move to the parent directory out of newDir. To do this, type this command.

cd ..

Then either open the editor again (nano create.sh) or type cat create.sh at the command prompt.

Paste the screenshot of the script and the result of running the script in the lab report.

Next, we will set up our linux VM as a web server. Instructions will also follow below.

In order to set up the linux VM as a web server, it must be connected to the Internet. The first thing needed is the IP address of your linux machine. Type the command below.

ifconfig

Look at the inet addr. This is the IP address. In the screenshot above, the IP address is 192.168.93.128.

Then you will need to update your Raspberry Pi and install Apache. Use the following commands.

sudo apt-get update

sudo apt-get install apache2

This will take several minutes to install, and you may need to type Y to accept the install.

Next, go to a browser (on your linux VM – you will see Firefox in the upper left corner) and type in the IP address of the linux vm into the address bar.

You should get a message that it works!

http://192.168.93.128

Now let's modify the index.html page that you are viewing. Go back to the terminal window and navigate to the /var/www/html folder. Then type the ls command (lower case L) and you should see one file: index.html

cd /var/www/html

ls

Now we will modify index.html. We will modify just a few lines but if you have experience with html or would like to learn and play with it, you are welcome to modify as much as you want! To edit the index.html file type in the following command to open the nano editor.

sudo nano index.html

This is the current code. Feel free to modify it. Change the title from “Apache 2 Ubuntu Default Page: It Works” to your name.

Change what is between the <title> and </title>

Save the file using ctrl + o. Then exit with ctrl + x.

Open the web browser and type in the IP address of the pi into the address bar and you should see the new page. My page below has been changed quite a bit:

Paste the screenshot of your site in the lab report showing the IP address.

Deliverables Part 4

· Complete the Course Project Report

· Include the pictures and descriptions required in the explanation above

Part 5: Hardware and Software Presentation (due Week 8)

· Create a Powerpoint Presentation with at least 10 slides. Include an Introduction, Challenges, Career Skills Learned, and a Conclusion slide

· Create a video and or powerpoint presentation showing your project – all parts. An example video is here: https://lms.devry.edu/lms/video/player.html?video=1_xncbxdzb

· Submit powerpoint and link to your video