Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review Please turn in your homework and practicals Packages, installation, rpm command Apache – Quick and easy way to set up a web server to play around.

Similar presentations


Presentation on theme: "Review Please turn in your homework and practicals Packages, installation, rpm command Apache – Quick and easy way to set up a web server to play around."— Presentation transcript:

1 Review Please turn in your homework and practicals Packages, installation, rpm command Apache – Quick and easy way to set up a web server to play around with

2 Today Scripting

3 Method of automating repeatable tasks Anything you do more than 3 times, you should script Script = quick program using commands instead of programming language – Ish, there are shell-script specific things you can use, they’re beyond the scope of this class

4 Programming Breaking down a larger task into smaller instructions that a computer can understand and carry out Making a peanut butter sandwich – Peanut butter, jelly, 2 slices of bread – Spread jelly on one slice, pb on other, combine You don’t say “walk to cupboard, open cupboard, get out peanut butter, put pb on counter, get out bread, close cupboard, etc…”

5 Computers Are Dumb But computers need all that – they’re not people They can’t interpret those logical steps that we take for granted So we have to break tasks down into those steps a computer understands

6 Shell Scripts Shell scripts are collections of commands Java/C++ are “compiled” – This allows you to create an object and interact with it in other ways Shell scripts are “interpreted” This means the file starts at the top, it takes the first one It runs the first one, waits for it to finish, and then goes to the next one

7 Backup Script One of the most useful scripts I have re- created almost every time I start in a new environment is Backup my system So we start with that over-arching task, and break it down Backup what? We don’t need system files, we need user files and application files (maybe temp)

8 Breaking A Task Down Backup /home Backup /opt Backup /tmp What do we do with it when it’s backed up? Move to another system (let’s say 192.168.1.50:/opt/backups) Let’s add output to help us see start/finish times

9 Build a script that… Announces it’s starting and the time Backs up /home, /opt, and /tmp Move to another system (let’s say 192.168.1.50:/opt/backups) Cleans up after itself Announces it’s finished

10 Map Commands to Actions Announces it’s starting and the time – echo “Announce and time” and date Backs up /home, /opt, and /tmp – tar file.tar /directory1 /directory2 /directory3 /dir4 etc… Move to another system (let’s say 192.168.1.50:/opt/backups) – scp from_system to_system Cleans up after itself - ? – rm file Announces it’s finished – echo “done”

11 Map Commands to Actions echo “Started backing up system at:” date tar backup.tar /home /opt /tmp scp backup.tar 192.168.1.50:/opt/backups rm backup.tar echo “Finished at:” date

12 Missing Anything? echo “Started backing up system at:” date tar backup.tar /home /opt /tmp scp backup.tar 192.168.1.50:/opt/backups rm backup.tar echo “Finished at:” date

13 New Functionality The ` is called a ‘tic’ Notice ` is not ‘ (tic is not apostrophe) It’s the same key as ~ This runs the command inside a script It inserts the results into your script tar -cvf `hostname`.tar /home /opt /tmp hostname is a command – returns system name This runs hostname and then inserts it into tar

14 Portability tar -cvf `hostname`.tar /home /opt /tmp This is now ‘portable’ and will work across systems So our system is it136centos65vm Another could be prodWEBcentos65 We can still run this on both systems, and differentiate between the output of the two (by filename) Would create it136centos65vm.tar and would create prodWEBcentos65.tar One script, multiple systems

15 With a Variable #!/bin/bash $DATE=`date` echo “Started backing up system at $DATE” tar `hostname`.tar /home /opt /tmp scp `hostname`.tar 192.168.1.50:/opt/backups rm `hostname`.tar echo “Finished at $DATE”

16 For Experienced Users If you can write a Bash script within 48 hours that: Has your name and today’s date as a comment Prints your username to STDOUT Checks if username is in sudoers file Starts a sleep(1000) command in the background Checks if Apache is installed via RPM Attempts to start Apache Runs regex against the Apache config file that checks for the ‘default’ documentroot Redirects the output to a file in /tmp Kill the sleep(1000) command using job # Come talk to me, this class is going to bore you

17 Continued Advanced Scripting – Control functions – If-then – Do-while – Variables – User input

18 Scripting You’ll know it when you see it – “I’m so sick of doing all this for each user!” Break tasks down until each one maps to a single command Each line of a script is a single task (command) Performs a useful, repeatable function – Burnin – “automation”

19 “Case Study” (Example) I took over the IDS systems It took me six months to make the devices more stable – In between dealing with failures After that, I was dealing with a HIPAA audit where I had to install over 1,000 IDS “agents” I scripted this, and the reporting Every time I was able to install, I’d run my script 20-30 agents installed within minutes I’d run my reporting script to make sure they were all running I’d Netflix the rest of the day

20 Quiz Monday 10 Questions – Covering all topics since the last quiz – Open book, open note, open computer Last subject – Will spend a week and a half on ‘regular expressions’ and their utilities And then the final!


Download ppt "Review Please turn in your homework and practicals Packages, installation, rpm command Apache – Quick and easy way to set up a web server to play around."

Similar presentations


Ads by Google