IT 441: Network Services Administration
Class #14 Exercise

Complete by: 4/16/2018


Log in to users.cs.umb.edu

Regexes - Character ranges and counts

perl>  $mac1 = "A0:5B:96:FC:FE:D2" ;

perl>  $mac2 = "5b:96:fc:fe:d2:34" ;  

perl>  if ($mac1 =~ /([0-9A-F][0-9A-F]:)+[0-9A-F][0-9A-F]/) { print "Match!\n" ; } else { print "Sorry, no match.\n" ; }

perl>  if ($mac1 =~ /([\dA-F][\dA-F]:)+[\dA-F][\dA-F]/) { print "Match!\n" ; } else { print "Sorry, no match.\n" ; }

perl>  if ($mac1 =~ /([\dA-F]{2}:)+[\dA-F]{2}/) { print "Match!\n" ; } else { print "Sorry, no match.\n" ; }

perl>  if ($mac2 =~ /([\dA-F]{2}:)+[\dA-F]{2}/) { print "Match!\n" ; } else { print "Sorry, no match.\n" ; }

perl>  if ($mac2 =~ /([\da-f]{2}:)+[\da-f]{2}/) { print "Match!\n" ; } else { print "Sorry, no match.\n" ; }

perl>  if ($mac2 =~ /([\dA-F]{2}:)+[\dA-F]{2}/i) { print "Match!\n" ; } else { print "Sorry, no match.\n" ; }

perl>  $not_a_mac1 = "A0:5B:96:FC:FE:D2:34:56" ;

perl>  if ($not_a_mac1 =~ /([\dA-F]{2}:)+[\dA-F]{2}/i) { print "Match!\n" ; } else { print "Sorry, no match.\n" ; }

perl>  if ($not_a_mac1 =~ /([\dA-F]{2}:){5}[\dA-F]{2}/i) { print "Match!\n" ; } else { print "Sorry, no match.\n" ; }

perl>  $not_a_mac2 = "A0:5B:D2:56" ;

perl>  if ($not_a_mac2 =~ /([\dA-F]{2}:){5}[\dA-F]{2}/i) { print "Match!\n" ; } else { print "Sorry, no match.\n" ; }

perl>  if ($not_a_mac2 =~ /([\dA-F]{2}:){1,5}[\dA-F]{2}/i) { print "Match!\n" ; } else { print "Sorry, no match.\n" ; }

Regexes - Groups, Arrays, and Positional Variables

perl>  $ipStr = "Our IP address is 156.92.241.47. How nice!";  

perl>  if ($ipStr =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) { print "Match!\n" ; } else { print "Sorry, no match.\n" ; }

perl>  @ipStrParts = ($ipStr =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/);

perl>  for $part (@ipStrParts) { print "$part\n"; }

perl>  @ipStrParts = ($ipStr =~ /((\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}))/);

perl>  for $part (@ipStrParts) { print "$part\n"; }

perl>  if ($ipStr =~ /((\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}))/) { print "The IP address is: $1\n\n" ; print "First Octet:  $2\n" ; print "Second Octet: $3\n" ; print "Third Octet:  $4\n" ; print "Fourth Octet: $5\n" ; }

perl>  $modifier = "";

perl>  print $ipStrParts[0];

perl>  for $part (@ipStrParts[1..4]) { if ($part > 255) { $modifier = "not "; last; } }

perl>  print "$ipStrParts[0] is " . $modifier . "a valid IP";

perl>  $ipStr = "Our IP address is 156.92.341.47. How nice!";

perl>  @ipStrParts = ($ipStr =~ /((\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3}))/);

perl>  $modifier = "";

perl>  for $part (@ipStrParts[1..4]) { if ($part > 255) { $modifier = "not "; last; } }

perl>  print "$ipStrParts[0] is " . $modifier . "a valid IP";

Regexes - Groups, Arrays, and Positional Variables

perl>  open ( INFILE , '<' , './passwd' ) or die $!;

perl>  @passwd_lines = <INFILE> ;

perl>  close (INFILE);

perl>  $passwd = join ("", @passwd_lines) ; 

perl>  print $passwd ;

perl>  @users = ($passwd =~ /([a-z0-9]+):x/g) ;

perl>  print (join (", ", @users)) ; print "\n" ;

perl>  @data = ($passwd =~ /([a-z0-9]+):.+:(\/bin\/.*sh)/g) ;

perl>  %users_shells = @data;

perl>  foreach $k (keys(%users_shells)) { print "User:  $k\nShell: $users_shells{$k}\n\n" ; }

File Tests and Strings

perl>  print "What file or directory should I test: ";

perl>  chomp($target = <STDIN>);

perl>  print ("Target file or directory is: ", $target, "\n");

perl>  if (-e $target) { print "$target exists\n"; } else { print "$target does not exist\n"; }

perl>  if (-d $target) { print "$target is a directory\n"; } else { print "$target is not a directory\n"; }

perl>  open ( INFILE , '<' , '/home/ckelly/course_files/it441_files/filetesting.pl' ) or die $!;

perl>  print <INFILE> ;

perl>  close (INFILE);

perl>  $string = "foo bar blech" ; 

perl>  print "The string \'$string\' is " . length($string) . " characters long." ;

perl>  print "The string \'bar\' begins at index #" . index($string, 'bar') ; 

perl>  print "The string \'blech\' begins at index #" . index($string, 'blech') ; 

perl>  print substr($string, 4, 3) . "\n" ;

perl>  print substr($string, 0, 7) . "\n" ;

perl>  print substr($string, 8) . "\n" ;

End the Perl Session

perl>  exit

End the script session

Use the exit command. You should then have a file called typescript in your current working directory.