1 / 47

CIT 383: Administrative Scripting

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

oshin
Download Presentation

CIT 383: Administrative Scripting

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIT 383: Administrative Scripting Introduction CIT 383: Administrative Scripting

  2. CIT 383: Administrative Scripting About Me http://www.nku.edu/~waldenj1 James Walden • Assistant Professor of Computer Science • waldenj@nku.edu • 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

  3. CIT 383: Administrative Scripting Course 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 • Email: waldenj@nku.edu • Office: AST 340 • Phone: (859) 572-5571

  4. CIT 383: Administrative Scripting Course 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.

  5. CIT 383: Administrative Scripting Expected 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

  6. CIT 383: Administrative Scripting First Half Topics • Logistics • Syllabus • Background • Why Administrative Scripting? • Ruby • How to Study • Numbers

  7. CIT 383: Administrative Scripting What 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.

  8. CIT 383: Administrative Scripting Why Administrative Scripting? Why do you need to program in IT?

  9. CIT 383: Administrative Scripting Why 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.

  10. CIT 383: Administrative Scripting Advantages of Automation • Greater reliability. • Regularity. • Timing and efficiency.

  11. CIT 383: Administrative Scripting Popular Sysadmin Languages • sh • Rexx • Ksh • bash • Perl • Python • Ruby

  12. CIT 383: Administrative Scripting Ruby 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

  13. CIT 383: Administrative Scripting Hello World JAVA public class HelloWorld { public static void main(String []args) { System.out.println(“Hello World”); } } RUBY puts “Hello World”

  14. CIT 383: Administrative Scripting Ruby • Dynamic • High level • Object oriented • Open source • Programmer efficient • String handling • VHLL

  15. CIT 383: Administrative Scripting Where 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/

  16. CIT 383: Administrative Scripting How 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.

  17. CIT 383: Administrative Scripting How 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.

  18. 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

  19. CIT 383: Administrative Scripting Why 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.

  20. CIT 383: Administrative Scripting

  21. CIT 383: Administrative Scripting Two 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.

  22. CIT 383: Administrative Scripting Types of Numbers Numeric Integer Float Fixnum Bignum

  23. CIT 383: Administrative Scripting Integer Literals Different bases Decimal: 255 Octal: 0377 Binary: 0b11111111 Hexadecimal: 0xFF Readability Insert _ as thousands separator. Can write 1000000000 as 1_000_000_000

  24. CIT 383: Administrative Scripting Float 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

  25. CIT 383: Administrative Scripting Arithmetic 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

  26. CIT 383: Administrative Scripting Logical 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

  27. CIT 383: Administrative Scripting Floating 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

  28. CIT 383: Administrative Scripting Variables 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

  29. CIT 383: Administrative Scripting Second 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

  30. CIT 383: Administrative Scripting Single-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.’

  31. CIT 383: Administrative Scripting Double-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.”

  32. CIT 383: Administrative Scripting String Escapes

  33. CIT 383: Administrative Scripting Interpolation Include result of code in double-quoted string • “1 + 1 == #{1+1}” • x = 2*3.1415926 • “360 degrees == #{x} radians”

  34. CIT 383: Administrative Scripting Choose 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.|

  35. CIT 383: Administrative Scripting Here 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 <html><head> <title>Here Document!</title> </head><body> “A quoted body isn’t normal.” </body></html> HERE

  36. CIT 383: Administrative Scripting Here 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

  37. CIT 383: Administrative Scripting Character 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’

  38. CIT 383: Administrative Scripting String 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 ...

  39. CIT 383: Administrative Scripting Logical 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’

  40. CIT 383: Administrative Scripting Accessing 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

  41. CIT 383: Administrative Scripting Substrings 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”

  42. CIT 383: Administrative Scripting Method 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.

  43. CIT 383: Administrative Scripting Kernel Class Methods defined by Kernel are global puts gets rand sprintf Kernel methods can be called w/o object puts “Hello Ruby” gets name

  44. CIT 383: Administrative Scripting What 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

  45. CIT 383: Administrative Scripting Learning 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

  46. CIT 383: Administrative Scripting Ruby Documentation Online documentation ri Fixnum Web documentation http://www.ruby-doc.org/core/classes/Fixnum.html

  47. CIT 383: Administrative Scripting References • 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.

More Related