Wednesday 30 October 2013

PS_03_Perl - Basic_I/O_&_Operators

Basic I/O
The easiest means to get operator input to your program is using the “diamond” operator:
$input = <>;
The input from the diamond operator includes a newline (\n). To get rid of this pesky character, use either chop() or chomp(). chop() removes the last character of the string, while chomp() removes any line-ending characters (defined in the special variable $/). If no argument is given, these functions operate on the $_ variable. To do the converse, simply use Perl’s print function:
print $output.”\n”;

Basic Operators
Arithmetic
Example Name Result
$a + $b Addition Sum of $a and $b
$a * $b Multiplication Product of $a and $b
$a % $b Modulus Remainder of $a divided by $b
$a ** $b Exponentiation $a to the power of $b

String
Example Name Result
$a . “string” Concatenation String built from pieces
$a string” Interpolation String incorporating the value of $a
$a x $b Repeat String in which $a is repeated $b times

Assignment
The basic assignment operator is “=”: $a = $b.
Perl conforms to the C idiom that lvalue operator= expression is evaluated as: lvalue = lvalue operator expression
So that $a *= $b is equivalent to $a = $a * $b
$a += $b $a = $a + $b
This also works for the string concatenation operator: $a .= “\n”

Autoincrement and Autodecrement
The autoincrement and autodecrement operators are special cases of the assignment operators, which add or subtract 1 from the value of a variable:
++$a, $a++ Autoincrement Add 1 to $a
--$a, $a-- Autodecrement Subtract 1 from $a

Logical
Conditions for truth: Any string is true except for “” and “0” Any number is true except for 0 Any reference is true Any undefined value is false
Example Name Result
$a && $b And True if both $a and $b are true
$a || $b Or $a if $a is true; $b otherwise
!$a Not True if $a is not true
$a and $b And True if both $a and $b are true
$a or $b Or $a if $a is true; $b otherwise
not $a Not True if $a is not true
Logical operators are often used to “short circuit” expressions, as in:
open(FILE,”< input.dat”) or die “Can’t open file”;

Comparison
Comparison Numeric String Result
Equal == eq True if $a equal to $b
Not equal != ne True if $a not equal to $b
Less than < lt True if $a less than $b
Greater than > gt True if $a greater than $b
Less than or equal <= le True if $a not greater than $b
Comparison <=> cmp 0 if $a and $b equal
1 if $a greater
-1 if $b greater

Operator Precedence
Perl operators have the following precedence, listed from the highest to the lowest, where operators at the same precedence level resolve according to associativity:
Associativity Operators Description
Left Terms and
list operators
Left -> Infix dereference operator
++
--
Auto-increment
Auto-decrement
Right
Right
Right
\
! ~
+ -
Reference to an object (unary)
Unary negation, bitwise complement
Unary plus, minus
Left
Left
=~
!~
Binds scalar to a match pattern
Same, but negates the result
Left * / % x Multiplication, Division, Modulo, Repeat
Left + - . Addition, Subtraction, Concatenation
Left >> << Bitwise shift right, left
< > <= >=
lt gt le ge
Numerical relational operators
String relational operators
== != <=>
eq ne cmp
Numerical comparison operators
String comparison operators
Left & Bitwise AND
Left | ^ Bitwise OR, Exclusive OR
Left && Logical AND
Left || Logical OR
.. In scalar context, range operator
In array context, enumeration
Right ?: Conditional (if ? then : else) operator
Right = += -= etc Assignment operators
Left ,
=>
Comma operator, also list element
separator
Same, enforces left operand to be string
Right not Low precedence logical NOT
Right and Low precedence logical AND
Right or xor Low precedence logical OR
Parentheses can be used to group an expression into a term. A list consists of expressions, variables, or lists, separated by commas. An array variable or an array slice many always be used instead of a list.

PS_02_Perl - Data_Types_&_Variables

Basic Types
The basic data types known to Perl are scalars, lists, and hashes. Scalar $foo Simple variables that can be a number, a string, or a reference. A scalar is a “thingy.” List @foo An ordered array of scalars accessed using a numeric subscript.
$foo[0]
Hash %foo An unordered set of key/value pairs accessed using the keys as subscripts. $foo{key}
Perl uses an internal type called a typeglob to hold an entire symbol table entry. The effect is that scalars, lists, hashes, and filehandles occupy separate namespaces (i.e., $foo[0] is not part of $foo or of %foo). The prefix of a typeglob is *, to indicate “all types.” Typeglobs are used in Perl programs to pass data types by reference. You will find references to literals and variables in the documentation. Literals are symbols that give an actual value, rather than represent possible values, as do variables.
For example in $foo = 1, $foo is a scalar variable and 1 is an integer literal.
Variables have a value of undef before they are defined (assigned). The upshot is that accessing values of a previously undefined variable will not (necessarily) raise an exception.

Variable Contexts
Perl data types can be treated in different ways depending on the context in which they are accessed. Scalar Accessing data items as scalar values. In the case of lists and hashes, $foo[0] and $foo{key}, respectively. Scalars also have numeric, string, and don’t-care contexts to cover situations in which conversions need to be done. List Treating lists and hashes as atomic objects Boolean Used in situations where an expression is evaluated as true or false. (Numeric: 0=false; String: null=false, Other: undef=false) Void Does not care (or want to care) about return value Interpolative Takes place inside quotes or things that act like quotes.

Special Variables (defaults)
Some variables have a predefined and special meaning to Perl. A few of the most commonly used ones are listed below.
$_ The default input and pattern-searching space
$0 Program name
$$ Current process ID
$! Current value of errno
@ARGV Array containing command-line arguments for the script
@INC The array containing the list of places to look for Perl scripts to
be evaluated by the do, require, or use constructs
%ENV The hash containing the current environment
%SIG The hash used to set signal handlers for various signals

Scalars
Scalars are simple variables that are either numbers or strings of characters. Scalar
variable names begin with a dollar sign followed by a letter, then possibly more letters,
digits, or underscores. Variable names are case-sensitive.

Numbers
Numbers are represented internally as either signed integers or double precision floating
point numbers. Floating point literals are the same used in C. Integer literals include
decimal (255), octal (0377), and hexadecimal (0xff) values.

Strings
Strings are simply sequences of characters. String literals are delimited by quotes:
Single quote ‘string’ Enclose a sequence of characters
Double quote “string” Subject to backslash and variable interpolation
Back quote `command` Evaluates to the output of the enclosed command
The backslash escapes are the same as those used in C:
\n Newline \e Escape
\r Carriage return \\ Backslash
\t Tab \” Double quote
\b Backspace \’ Single quote
In Windows, to represent a path, use either “c:\\temp” (an escaped backslash) or
c:/temp” (UNIX-style forward slash).
Strings can be concatenated using the “.” operator: $foo = “hello” . ”world”;

ELA_29_IPv4_Configuration

IPv4 Configuration:

Features:
1. DHCP
2. Static
3. Virtual (Sub) Interfaces - supports single physical connected to multiple logical
i.e. 192.168.0.0/24 && 192.168.1.0/24 && 10.0.0.0/30

Tasks:
1. Explore key: Directories & Files
a. '/etc/sysconfig/network' - system-wide settings: i.e. hostname, gateway, enabled|disabled
b. '/etc/sysconfig/networking' - 'system-config-network' tool controls this directory. Don't edit it manually.
c. '/etc/hosts' - local name DB - should contain a record for the localhost: i.e.
'localhost.localdomain'

Note: Add hosts to: '/etc/hosts', for which you cannot or should not resolve via DNS

d. '/etc/sysconfig/network-scripts'
d1. Interface configuration files - describes up/down config of interfaces: i.e. eth0
d2. Control files - describes hoe interfaces are to be brought: up/down - scripts
d3. Network function files - contais key network information required for the stack
d4. 'ifup-eth' - brings up ethernet interfaces: i.e. 'eth0', 'eth1', etc.
d5. 'ifdown-eth' - brings down ethernet interfaces: i.e. 'eth0', 'eth1', etc.

e. 'ifconfig' - enumerates configuration of interfaces
Note: At minimum, a routeable, connected system has at least 2 interfaces:
1. 'lo' - loopback - 127.0.0.1
2. 'eth0' - Ethernet0 - Your routable IP/Net
e2. 'ifconfig eth0:1 192.168.0.101 netmask 255.255.255.0'
e2.1 'ping -c 3 -I 192.168.0.100 192.168.0.101' - sources traffic as: 192.168.0.101
e3. 'ifconfig eth0:2 192.168.0.102 netmask 255.255.255.0'

e4. Preserve changes across system restart/ 'NetworkManager' service restart
e4.1. 'cp -v /etc/sysconfig/network-scripts/ifcfg-eth0:1'

f. 'ifcfg eth0:3 add 192.168.0.103/24' - Does duplicate address detection & sends ARP to hosts on the same Net as the interface
f1. 'ifcfg eth0:1 delete 192.168.0.101/24' - removes the sub-interface
f2. 'ifconfig eth0:3 del 192.168.0.103' - removes the sub-interface

ELA_28_Common_Network_Utilities

Common Network Utilities:

Features:
1. Determine if remote host is up/available: 'ping'
2. Determine if local/remote service is available: 'telnet'
3. Determine network sockets stats/connections: 'netstat'
4. View L2 information:'arp'
5. View path taken by packets to remote system: 'traceroute'
6. Hostname-to-IP and reverse resolution: 'nslookup', 'dig'
7. Learn more information about and IP and/or block: 'whois'
Tasks:
1. Explore Packet Internet Groper (PING)
a. 'ping -c 3 192.168.0.100 -s 32' - sends 32-bytes + 8-bytes (ICMP overhead)
b. 'ping -c 3 -i 3 192.168.0.100' - sends 3-packets of 56-bytes, every 3-seconds to the target.
Note: PING may be blocked by L3 devices on your network and/or the Internet
2. Telnet - Don't use for TTY access to remote host. Use SSH. Use telnet to test port-accessibility.
a. 'telnet 192.168.0.50' - Install if necessary using yum.
3. Netstat - reveals TCP:UDP:Unix sockets - '/proc/net'
a. 'netstat -a' - dumps all sockets with: service/port and hostname resolution
b. 'netstat -an' - same as above, but suppresses name resolution
c. 'netstat -ntl' - suppresses name resolution, shows only TCP sockets, and Listeners
d. 'netstat -ntlp' - same as above, includes programs bound to ports
Note: 'Use '-p' option as root to reveal ALL programs'
Note: ':::514' - means that port is bound to ALL IPv6 addresses configured on the host
Note: '0.0.0.0:514' - means that port is bound to ALL IPv4 addressesconfigures on the host
e. 'netstat -i'
f. 'netstat -nulp' - returns ALL UDP listeners
g. 'netstat -rn' - returns kernel routing table

4. ARP - Address Resolution Protocol
a. 'arp -a || arp -e'
Note: ARP is usually self managing.

5. Traceroute - follows path taken by packets across the network (Intra/Internet)
a. 'traceroute 192.168.0.100'
b. 'traceroute www.hindux.com'

6. 'nslookup'
a. 'nslookup www.hindux.com'
DNS client tools use: '/etc/resolv.conf' to determine which DNS servers to query

7. 'dig'
a. 'dig www.hindux.com'
b. 'dig -x 192.168.0.100' - performs a reverse lookup
c. 'dig hindux.com mx'

8. 'whois' - Finds IP/domain ownership information
a. 'whois hindux.com'

ELA_27_Syslog_Configuration

Syslog config:

Features:
1. Logs daemon information
2. Logs remotely
3. Accepts, if configured, logs fromremote hosts: i.e. routers, switches, firewalls, content switches, Linux Hosts, etc.,
4. Supports: Unix Domain Sockets (/dev/log)
5. Supports: Internet Sockets: (UDP:514) and/or (TCP:514)

Centralized log server (syslog server)
Suppose we have a server and 5 client machines. And we want to monitor the logs of all those client machines. In situations like this, we will use centralized server as a log server. Whatever events are happening in client machines, the logs will be sent to the server. So that we can monitor all the logs from a centralized server. We make use of syslog service for this.
Features of syslog:
1. Logs the daemon information to localhost
2. Logs the daemon information to Remote host
3. Logs the daemon information to List of users
4. Logs the daemon information to console

Instaltion Package is sysklogd
[root@apache ~]# rpm -q sysklogd
sysklogd-1.4.1-44.el5
[root@apache ~]#

Or you can check as follows:
[root@apache ~]# rpm -qf /etc/syslog.conf
sysklogd-1.4.1-44.el5
[root@apache ~]#

Starting the syslog daemon
[root@apache ~]# /etc/init.d/syslog start
Starting system logger: [ OK ]
Starting kernel logger: [ OK ]
[root@apache ~]#

Checking the process name. it is syslogd
[root@apache ~]# ps -ax | grep syslog
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.7/FAQ
5190 ? Ss 0:00 syslogd -m 0
5210 pts/0 S+ 0:00 grep syslog
[root@apache ~]#

Configuration of server machine(syslog server)

Service name: syslog
configuration file: /etc/sysconfig/syslog

Steps:

1. Open the /etc/sysconfig/syslog file and add "-r" option to the variable SYSLOGD_OPTIONS as shown below.

[root@server ~]# cat /etc/sysconfig/syslog
# Options to syslogd
# -m 0 disables 'MARK' messages.
# -r enables logging from remote machines
# -x disables DNS lookups on messages recieved with -r
# See syslogd(8) for more details
SYSLOGD_OPTIONS="-r -m 0"
# Options to klogd
# -2 prints all kernel oops messages twice; once for klogd to decode, and
# once for processing with 'ksymoops'
# -x disables all klogd processing of oops messages entirely
# See klogd(8) for more details
KLOGD_OPTIONS="-x"
#
SYSLOG_UMASK=077
# set this to a umask value to use for all log files as in umask(1).
# By default, all permissions are removed for "group" and "other".
[root@server ~]#

2. Restart the syslog service.

[root@server ~]# service syslog restart
Shutting down kernel logger: [ OK ]
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
Starting kernel logger: [ OK ]
[root@server ~]#

Configuration for client machines

service name: syslog
Configuration file: /etc/syslog.conf


The configuration file /etc/syslog.conf has two parts
Eg:
*.info;mail.none;authpriv.none;cron.none /var/log/messages
[selector field(Facility.priority)] [action field]

They are selector field and actions field. Selector field is again divided into two. Facilities and priorities.
Facility examples are (authpriv,kern,mail,local7 etc)
The priority is one of the following in ascending order: debug(0), info, notice, warning(warn), error(err), crit, alert, emerg(panic(7))
Actions can be regular files, console, list of users, remote machine ip etc.
Steps:

1. Open the configuration file /etc/syslog.conf and add an entry to redirect the logs to the remote server.

[root@vm1 ~]# cat /etc/syslog.conf
# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.* /dev/console

*.* @192.168.0.19

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none /var/log/messages


# The authpriv file has restricted access.
##authpriv.* /var/log/secure

# Log all the mail messages in one place.
mail.* -/var/log/maillog

# Log cron stuff
cron.* /var/log/cron

# Everybody gets emergency messages
*.emerg *

# Save news errors of level crit and higher in a special file.
uucp,news.crit /var/log/spooler

# Save boot messages also to boot.log
local7.* /var/log/boot.log

[root@vm1 ~]#

2. Restart the service

[root@vm1 ~]# service syslog restart
Shutting down kernel logger: [ OK ]
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
Starting kernel logger: [ OK ]
[root@vm1 ~]#

Checking:

In server open a terminal and watch /var/log/messages and restart syslog service in client. You can see the log from clinet coming to server.

[root@server ~]# tail -f /var/log/messages

Tasks:
1. Exploration of environment
a. '/etc/rsyslog.conf' - primary config file
b. '/etc/sysconfig/rsyslog' - ancillary config file, containing startup options
2. '/etc/rsyslog.conf' - exploration
Selector(s) Action(s)
#### RULES ####
*.info;mail.none;authpriv.none;cron.none /var/log/messages
authpriv.* /var/log/secure
mail.* /var/log/maillog
cron.* /var/log/cron
*.emerg *
local7.* /var/log/boot.log

3. Configure UDP:514 routing of messages from Cisco Router
a. '/etc/rsyslog.conf'
Under the Modules uncomment the lines
$Modload imtcp.so
$UDPServerRun 514
b. 'service rsyslog restart'

Monday 28 October 2013

Bash Exercise - 8

Script 36: Nested For
#!/bin/bash
# nested-loop.sh: Nested "for" loops.

outer=1 # Set outer loop counter.

# Beginning of outer loop.
for a in 1 2 3 4 5
do
echo "Pass $outer in outer loop."
echo "---------------------"
inner=1 # Reset inner loop counter.

# ===============================================
# Beginning of inner loop.
for b in 1 2 3 4 5
do
echo "Pass $inner in inner loop."
let "inner+=1" # Increment inner loop counter.
done
# End of inner loop.
# ===============================================

let "outer+=1" # Increment outer loop counter.
echo # Space between output blocks in pass of outer loop.
done
# End of outer loop.

exit 0

Script 37: Create New File
#!/bin/bash
if test -e new
then
echo "file exist"
else
touch new
fi


Script 38: For
#!/bin/bash
sum=0
for var in $1 $2 $3 $4 $5
do
sum=$(($sum + $var))
done
echo "sum is $sum"

Script 39: Check whether the two numbers are equal or not
#!/bin/bash

echo "two values"

read num1 num2

if [ $num1 -ne $num2 ]

then

echo "two not are equal"

fi

Script 40: Open File
#!/bin/bash
echo "enter the file name to be opened"
read file_name
#cat $file_name
echo "number of characters in the file is:"
wc -m $file_name
echo "number of lines in the file is:"
wc -l $file_name
echo "number of words in the file is:"
wc -w $file_name

Bash Exercise - 7

Script 31: Leap year
#!/bin/bash
# This script will test if we're in a leap year or not.
year=`date +%Y`
if [ $[$year % 4] -eq 0 ]

then
echo "This is a leap year, February has 29 days."
else
echo "This is not a leap year. February has 28 days."
fi
else
echo "This is not a leap year. February has 28 days."
fi

Script 32: If – Else Statement
#!/bin/bash
echo "enter some integer"
read a
echo "enter some integer"
read b
c=30
if test $(($a+$b)) -eq $c
then
echo "equal"
else
echo "not equal"
fi
Script 33: Nested If-Else
if test $1 -gt $2
then
echo $1 is greater than $2
elif test $1 -lt $2
then
echo $1 is lesser than $2
fi
Script 34: Let
#!/bin/bash
#letting "lett" do arithmetic
echo

let a=11 # Same as 'a=11'
let a=a+5 # Equivalent to let "a = a + 5"
# (Double quotes and spaces make it more readable.)
echo "11 + 5 = $a" # 16

let "a <<= 3" # Equivalent to let "a = a << 3"
echo "\"\$a\" (=16) left-shifted 3 places = $a"
# 128

let "a /= 4" # Equivalent to let "a = a / 4"
echo "128 / 4 = $a" # 32

let "a -= 5" # Equivalent to let "a = a - 5"
echo "32 - 5 = $a" # 27

let "a *= 10" # Equivalent to let "a = a * 10"
echo "27 * 10 = $a" # 270

let "a %= 8" # Equivalent to let "a = a % 8"
echo "270 modulo 8 = $a (270 / 8 = 33, remainder $a)"
# 6

echo

exit 0
Script 35: Mail
`rm -rf file file1`
`rm -rf file.log`
df -h | sort -rnk5 | awk '{print $5,$6}' | sed '$d' > file
df -h | sort -rnk5 | awk '{print $5,$6}' | awk 'BEGIN { FS="%" } { print $1 }'| sed '$d' > file1
rm -f file.log
a=0
for i in `cat file1`
do
a=$(($a+1))
if [ $i -ge 70 ]
then
##echo `sed -n ''$a'p' file | awk '{print $2}'`
`sed -n ''$a'p' file >> file.log`
#echo `sed -n ''$a'p' file `
fi
done
cat file.log
mail -v -s "disk usage" $1@nerds < /root/file.log

PS_00_Exercise Hello_World_0

Perl : (Practical Extraction Reporting Language)
It is a powerful language for text manipulation and system administration.
It is modular, it supports procedures, object orientation, it can parse text.

# touch helloworld.pl
# vi helloworld.pl

#!/usr/bin/perl -w
# Author: Arjun Shrinivas
# Date: 27.10.2013
# Purpose: Echoes Hello World.

print "Hello World\n";

#END
:wq

Now here's some explanation (in case you need any):

1. The string is enclosed in double-quotes(" ... ") because that's how string constants are represented in perl.
2. \n is a special character that is called "newline". When you print a newline you will see that your output started on a new line.
3. The semi-colon (;) at the end indicates that this is the end of a perl command. Every perl command should be terminated with a semicolon.

"The content which is placed with in double quotes will be printed explicitly."
The output for "Hello World\n"; will be
# perl -w helloworld.pl
Hello World
#
'The content which is placed with in single quotes will be printed literally than explicitly.'
The output for 'Hello World\n'; will be
# perl -w helloworld.pl
Hello World\n#

PS_01_Perl - Basics

Script names
While generally speaking you can name your script/program anything you want, there are a number of conventional extensions applied to portions of the Perl bestiary:
.pm - Perl modules
.pl - Perl libraries (and scripts on UNIX)
.plx - Perl scripts

Language properties
· Perl is an interpreted language – program code is interpreted at run time. Perl is unique among interpreted languages, though. Code is compiled by the interpreter before it is actually executed.
· Many Perl idioms read like English
· Free format language – whitespace between tokens is optional
· Comments are single-line, beginning with #
· Statements end with a semicolon (;)
· Only subroutines and functions need to be explicitly declared
· Blocks of statements are enclosed in curly braces {}
· A script has no “main()

Invocation
On platforms such as UNIX, the first line of a Perl program should begin with
#!/usr/bin/perl
and the file should have executable permissions. Then typing the name of the script will cause it to be executed.
Unfortunately, Windows does not have a real equivalent of the UNIX “shebang” line. On Windows 95/98, you will have to call the Perl interpreter with the script as an argument:
> perl myscript.plx
On Windows NT, you can associate the .plx extension with the Perl interpreter:
> assoc .plx=Perl
> ftype Perl=c:\myperl\bin\perl.exe %1% %*
> set PATHEXT=%PATHEXT%;.plx
After taking these steps, you can execute your script from the command line as follows:
> myscript
The ActivePerl distribution includes a pl2bat utility for converting Perl scripts into batch files. You can also run the interpreter by itself from the command line. This is often useful to execute short snippets of code:
perl –e ‘code’
Alternatively, you can run the interpreter in “debugging” mode to obtain a shell-like environment for testing code scraps:
perl –de 1