introduction
Download
Skip this Video
Download Presentation
CIT 383: Administrative Scripting

Loading in 2 Seconds...

play fullscreen
1 / 47

CIT 383: Administrative Scripting - PowerPoint PPT Presentation


  • 183 Views
  • Uploaded on

Introduction . CIT 383: Administrative Scripting. About Me http://www.nku.edu/~waldenj1. James Walden Assistant Professor of Computer Science [email protected] Experience: System administration (CMU, Intel, UT, NKU) ‏ Operating systems: VMS, UNIX, Linux, IOS

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CIT 383: Administrative Scripting' - oshin


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
about me http www nku edu waldenj1
CIT 383: Administrative ScriptingAbout Me http://www.nku.edu/~waldenj1

James Walden

  • Assistant Professor of Computer Science
  • [email protected]
  • Experience:
    • System administration (CMU, Intel, UT, NKU)‏
    • Operating systems: VMS, UNIX, Linux, IOS
    • Scale: dozens to thousands of machines, 1-dozens of sites
    • Scripting: sh, csh, perl, PHP, python, ruby
course administration
CIT 383: Administrative ScriptingCourse Administration

Web Site

  • Notes, readings, and assignments on web site.
  • http://faculty.cs.nku.edu/~waldenj1

Assignment submission

  • Use submit command on kosh.nku.edu.

Contact Information

course goals
CIT 383: Administrative ScriptingCourse Goals

A successful student should be able to

1. Read and understand programs written in the ruby language.

2. Construct portable, secure programs in ruby.

3. Automate common system administration tasks.

4. Write networking scripts that interact with e-mail, web, and directory servers.

5. Write programs to parse common data formats such as CSV, XML, and YAML.

expected background
CIT 383: Administrative ScriptingExpected Background

Programming background:

  • INF 120: Elementary Programming
  • INF 260: Object Oriented Programming I
  • variables, conditionals, loops, arrays

Linux background:

  • CIT 140: Introduction to CIT
  • CIT 370: System Administration
  • bash, vim, cd, ls, cp, mv, rm, chmod, grep, find
first half topics
CIT 383: Administrative ScriptingFirst Half Topics
  • Logistics
    • Syllabus
    • Background
  • Why Administrative Scripting?
  • Ruby
  • How to Study
  • Numbers
what do sysadmins do
CIT 383: Administrative ScriptingWhat do sysadmins do?
  • Add and remove users.
  • Add and remove hardware.
  • Perform and restore from backups.
  • Install and patch software.
  • Troubleshooting.
  • Performance tuning.
  • Auditing security.
  • Helping users.
why do sysadmins need to program
CIT 383: Administrative ScriptingWhy do sysadmins need to program?
  • Make your job easier.
  • Solve problems that can’t be solved by installing or configuring others’ software.
  • Provide new features to your users.
advantages of automation
CIT 383: Administrative ScriptingAdvantages of Automation
  • Greater reliability.
  • Regularity.
  • Timing and efficiency.
ruby timeline
CIT 383: Administrative ScriptingRuby Timeline

1993: Matz starts building Ruby.

1995: Ruby released in Japan.

1998: First Ruby announcement in English.

2000: First Ruby book in English.

2003: Ruby 1.8 released.

2004: First public release of Ruby on Rails.

2007: Ruby 1.9 released.

201?: Ruby 2.0

hello world
CIT 383: Administrative ScriptingHello World

JAVA

public class HelloWorld {

public static void main(String []args) {

System.out.println(“Hello World”);

}

}

RUBY

puts “Hello World”

slide14
CIT 383: Administrative ScriptingRuby
  • Dynamic
  • High level
  • Object oriented
  • Open source
  • Programmer efficient
  • String handling
  • VHLL
where to get ruby
CIT 383: Administrative ScriptingWhere to get Ruby

Linux

  • Debian/Ubuntu: apt-get install irb ruby
  • Fedora: yum install ruby

Windows

  • http://www.ruby-lang.org/en/downloads/

Cygwin (UNIX command line for Win)‏

  • http://www.cygwin.com/

In your browser

  • http://tryruby.hobix.com/
how to study
CIT 383: Administrative ScriptingHow to Study

Before class

  • Read the book. Take notes.
  • Print out the slides and lab notes.
  • Read the slides and lab notes.
  • Write down any questions you have.

Spend 12-18 hours a week outside of class.

  • It’s an 8-week course.
  • Programming languages are like human languages—practice is necessary for fluency.
how to study17
CIT 383: Administrative ScriptingHow to Study

Do every lab

  • Read the lab before typing anything.
  • Try all of the exercises in irb.
  • Do the independent program.

Do every assignment

  • Read the assignment the day it’s assigned.
  • It will take time to design solutions.

Prepare for tests at least a week beforehand

  • It’s mostly programming, so be sure you can do the independent programs at the end of the labs without help.
types of numbers
CIT 383: Administrative Scripting

Integers

Numbers without decimal points.

-3, 0, 2, 65536

Precise operations.

1 + 2 = 3

1 – 2 = -1

1 * 2 = 2

1 / 2 = 0

Floats

Numbers with decimal points.

-199.9482, 0.0, 3.14

Rounding errors.

1.0 + 2.0 = 3.0

1.0 – 2.0 = -1.0

1.0 * 2.0 = 2.0

1.0 / 2.0 = 0.5

Types of Numbers
why two types of numbers
CIT 383: Administrative ScriptingWhy Two Types of Numbers?

Different uses

  • Money calculations should avoid rounding.
  • Measurements must often be floats.

Performance

  • Floats take more space than integers. (usually)
  • CPU has separate integer and float units.
two types of integers
CIT 383: Administrative ScriptingTwo Types of Integers

Fixnum

  • 32-bit machine integer
  • Fast (calculations in hardware)
  • Ranges from -231 to 231 – 1
  • Ruby promotes to Bignum beyond range.

Bignum

  • Arbitrary precision integer
  • Slow (calculations in software)
  • No limit to size.
integer literals
CIT 383: Administrative ScriptingInteger Literals

Different bases

Decimal: 255

Octal: 0377

Binary: 0b11111111

Hexadecimal: 0xFF

Readability

Insert _ as thousands separator.

Can write 1000000000 as 1_000_000_000

float literals
CIT 383: Administrative ScriptingFloat Literals

Always need a decimal point

1 is an integer, 1.0 is a float

Scientific notation

Avogadro’s number is 6.0221415e23

Readability

1_000_000_000.0

arithmetic operators
CIT 383: Administrative ScriptingArithmetic Operators

Addition: +

7 + 3 == 10

Subtraction: -

7 – 3 == 4

Multiplication: *

7 * 3 == 21

Division: /

13 % 2 == 6

Remainder: %

13 % 2 == 1

Exponentiation: **

2**8 == 256

logical operations
CIT 383: Administrative ScriptingLogical Operations

Return a true or false value.

Equality

1 == 1

Inequality

1 != 1

Less Than

1 < 2

Greater Than

1 > 2

Less Than or Equal To

1 >= 2

Greater Than or Equal To

1 <= 1

floating point rounding
CIT 383: Administrative ScriptingFloating Point Rounding

Machine floats

  • Stored as binary fractions: ½, ¼, etc.
  • Decimal fractions: 0.1 cannot be exactly represented, as it’s repeating in binary like 1/3.

Don’t use equality tests for floats

0.4 – 0.3 == 0.1 is false

Check if difference is sufficiently small

(0.4 – 0.3) – 0.1 < 1.0e-9 is true

variables
CIT 383: Administrative ScriptingVariables

Variables allow us to name values

x = 1.0 # Assigns the value 1.0 to x

x # A variable reference, evals to 1.0

Variable naming

  • Valid characters: letters, numbers, _
  • Name must start with letter or _
  • Case sensitive: now, noW, nOw are different
  • If name starts with capital, it is a constant.
  • Examples: x, y2, new_val, _secret, PI
second half strings and methods
CIT 383: Administrative ScriptingSecond Half: Strings and Methods
  • Single-quoted strings.
  • Double-quoted strings.
  • Choose your own quotes.
  • Characters.
  • String operators.
  • Method Calls
  • Kernel
  • Expressions and methods
  • Learning about methods
single quoted strings
CIT 383: Administrative ScriptingSingle-quoted Strings

Create strings using single quotes.

  • ‘Hello ruby’

Escape ‘ using \

  • ‘O\’Reilly published Learning Ruby.’
  • ‘A \ is just itself in the middle.’
  • ‘This string ends with one backslash.\\’
  • ‘You can also have multi-line strings using \ to escape the newline character.’
double quoted strings
CIT 383: Administrative ScriptingDouble-quoted Strings

Create strings using double quotes.

  • “Hello Ruby”

Double quoted strings have more escapes

  • “Hello \”Rubyist\””
  • “A multi-\nline string.”
  • “\tString indented by one tab.”
  • “No need to use backslash to escape newlines in double quoted strings.”
interpolation
CIT 383: Administrative ScriptingInterpolation

Include result of code in double-quoted string

  • “1 + 1 == #{1+1}”
  • x = 2*3.1415926
  • “360 degrees == #{x} radians”
choose your own quotes
CIT 383: Administrative ScriptingChoose your own Quotes

If your string has a lot of ‘ or “ in it, you have to do a lot of escaping so ...

Ruby allows you to choose your own quotes

  • %q acts like single-quoted string
  • %Q acts like double-quoted string
  • Character after q or Q is the delimiter.
  • Initial and final delimiters are identical unless you’re using one of a matched pair: (,[,{,< match ),],},> respectively.

Examples

  • %q(No need to worry about escaping ‘ here)
  • %Q|Or for escaping “ in this string.|
  • %Q|But you do have to escape \| here.|
here documents
CIT 383: Administrative ScriptingHere Documents

For long string literals, any chosen delimiter may be used within the string, so Ruby can delimit text using arbitrary strings like bash.

document = <

Here Document!

“A quoted body isn’t normal.”

HERE

here documents36
CIT 383: Administrative ScriptingHere Documents

Behave like double-quoted strings

  • String interpolation
  • Escape characters

Single-quoted here documents:

document = <<‘EOD’

You can use #{1+1} to escape ruby code,

and you can use \t as backslash and t,

as they don’t do anything special here.

EOD

character literals
CIT 383: Administrative ScriptingCharacter Literals

Single characters denoted by a ? prefix

  • ?a is the character a
  • ?” is the double-quote character
  • ?\t is the tab character

Not the same as a single character string

  • ?a != ‘a’
string operators
CIT 383: Administrative ScriptingString Operators

Concatenation

  • “Hello” + “ “ + “Ruby” == “Hello Ruby”

Converting numbers to strings

  • version = 1.9
  • “Hello Ruby “ + version.to_s == “Hello Ruby 1.9”
  • “Hello Ruby #{version}”

Multiplication

  • ellipsis = ‘.’*3 # Evaluates to ...
logical operators
CIT 383: Administrative ScriptingLogical Operators

Equality

‘Hello’ == ‘Hello’

Inequality

‘Hello’ != ‘hello’

Less Than

‘a’ <= ‘b’

Less Than or Equal To

‘a’ <= ‘a’

Greater Than

‘baz’ > ‘bar’

Greater Than or Equal To

‘baz’ >= ‘baz’

accessing characters
CIT 383: Administrative ScriptingAccessing Characters

Use index to access individual characters

x = “Hello”

x[0] == ?H

x[1] == ?e

Negative numbers index from the end

x[-1] == ?o

x[-2] == ?l

Use index to modify string, -1 index special

x[0] = ?M # changes x to Mello

x[-1] = “” # changes to Mell

substrings
CIT 383: Administrative ScriptingSubstrings

Use double index to access substrings

x = “Hello”

x[0,2] == “He”

x[-2,2] == “lo”

x[0,0] == “” # For all strings

x[0,10] == “Hello”

x[0,-1] == nil # Negative lens ret nil

Modify string by assigning to index

x[0,2] = “Ma”

x[-2,2] = “ow”

x == “Mallow”

x[2,2] = “”

x == “Maow”

method calls
CIT 383: Administrative ScriptingMethod Calls

Method calls in ruby use the dot syntax:

  • object.method
  • object.method(arg)
  • object.method(arg1, arg2)

If the object is not specified, the method is invoked on the default object self.

  • When defining a class, self is current object.
  • Outside of a class definition, self is Kernel.
kernel class
CIT 383: Administrative ScriptingKernel Class

Methods defined by Kernel are global

puts

gets

rand

sprintf

Kernel methods can be called w/o object

puts “Hello Ruby”

gets name

what can you call a method on
CIT 383: Administrative ScriptingWhat can you call a method on?

Methods can be called on any objects.

1.abs

(1-2).abs

(1-2).to_s

Including objects returned by methods

(1-2).abs.to_s

1.methods.sort

learning about methods
CIT 383: Administrative ScriptingLearning About Methods

Ruby objects know their own methods

object.methods will list methods

object.methods.sort will sort the list too

Any object of class knows about its methods

x=5

x.methods

1.methods

You can also ask the class about its methods

Fixnum.methods

ruby documentation
CIT 383: Administrative ScriptingRuby Documentation

Online documentation

ri Fixnum

Web documentation

http://www.ruby-doc.org/core/classes/Fixnum.html

references
CIT 383: Administrative ScriptingReferences
  • Mark Burgess, Principles of System and Network Administration, Wiley, 2000.
  • Aeleen Frisch, Essential System Administration, 3rd edition, O’Reilly, 2002.
  • Ruby FAQ, http://www.rubygarden.org/faq/main/, 2006.
  • Dave Thomas with Chad Fowler and Andy Hunt, Programming Ruby, 2/e, Pragmatic Programmers, 2005.
  • Wikipedia, http://en.wikipedia.org/wiki/Ruby_programming_language, 2006.
ad