lunedì 30 gennaio 2012

Shell Script esempio ciclo while

#!/usr/bin/ksh
while :
do
 read a b c d e f g h
if [ $? -ne 0 ]
        then
                break
        fi

echo $1,$2,$3,$4,$5,$6,$7,$8 >> /tmp/alarm.sandro

done < /tmp/allarmi.txt

Shell Script espressioni utili AWK


HANDY ONE-LINERS FOR AWK                                  22 July 2003
compiled by Eric Pement            version 0.22
   Latest version of this file is usually at:
   http://www.student.northpark.edu/pemente/awk/awk1line.txt


USAGE:

    Unix:  awk '/pattern/ {print "$1"}'    # standard Unix shells
 DOS/Win:  awk '/pattern/ {print "$1"}'    # okay for DJGPP compiled
           awk "/pattern/ {print \"$1\"}"  # required for Mingw32

Most of my experience comes from version of GNU awk (gawk) compiled for
Win32. Note in particular that DJGPP compilations permit the awk script
to follow Unix quoting syntax '/like/ {"this"}'. However, the user must
know that single quotes under DOS/Windows do not protect the redirection
arrows (<, >) nor do they protect pipes (|). Both are special symbols
for the DOS/CMD command shell and their special meaning is ignored only
if they are placed within "double quotes." Likewise, DOS/Win users must
remember that the percent sign (%) is used to mark DOS/Win environment
variables, so it must be doubled (%%) to yield a single percent sign
visible to awk.

If I am sure that a script will NOT need to be quoted in Unix, DOS, or
CMD, then I normally omit the quote marks. If an example is peculiar to
GNU awk, the command 'gawk' will be used. Please notify me if you find
errors or new commands to add to this list (total length under 65
characters). I usually try to put the shortest script first.

FILE SPACING:

 # double space a file
 awk '1;{print ""}'
 awk 'BEGIN{ORS="\n\n"};1'

 # double space a file which already has blank lines in it. Output file
 # should contain no more than one blank line between lines of text.
 # NOTE: On Unix systems, DOS lines which have only CRLF (\r\n) are
 # often treated as non-blank, and thus 'NF' alone will return TRUE.
 awk 'NF{print $0 "\n"}'

 # triple space a file
 awk '1;{print "\n"}'

NUMBERING AND CALCULATIONS:

 # precede each line by its line number FOR THAT FILE (left alignment).
 # Using a tab (\t) instead of space will preserve margins.
 awk '{print FNR "\t" $0}' files*

 # precede each line by its line number FOR ALL FILES TOGETHER, with tab.
 awk '{print NR "\t" $0}' files*

 # number each line of a file (number on left, right-aligned)
 # Double the percent signs if typing from the DOS command prompt.
 awk '{printf("%5d : %s\n", NR,$0)}'

 # number each line of file, but only print numbers if line is not blank
 # Remember caveats about Unix treatment of \r (mentioned above)
 awk 'NF{$0=++a " :" $0};{print}'
 awk '{print (NF? ++a " :" :"") $0}'

 # count lines (emulates "wc -l")
 awk 'END{print NR}'

 # print the sums of the fields of every line
 awk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print s}'

 # add all fields in all lines and print the sum
 awk '{for (i=1; i<=NF; i++) s=s+$i}; END{print s}'

 # print every line after replacing each field with its absolute value
 awk '{for (i=1; i<=NF; i++) if ($i < 0) $i = -$i; print }'
 awk '{for (i=1; i<=NF; i++) $i = ($i < 0) ? -$i : $i; print }'

 # print the total number of fields ("words") in all lines
 awk '{ total = total + NF }; END {print total}' file

 # print the total number of lines that contain "Beth"
 awk '/Beth/{n++}; END {print n+0}' file

 # print the largest first field and the line that contains it
 # Intended for finding the longest string in field #1
 awk '$1 > max {max=$1; maxline=$0}; END{ print max, maxline}'

 # print the number of fields in each line, followed by the line
 awk '{ print NF ":" $0 } '

 # print the last field of each line
 awk '{ print $NF }'

 # print the last field of the last line
 awk '{ field = $NF }; END{ print field }'

 # print every line with more than 4 fields
 awk 'NF > 4'

 # print every line where the value of the last field is > 4
 awk '$NF > 4'


TEXT CONVERSION AND SUBSTITUTION:

 # IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format
 awk '{sub(/\r$/,"");print}'   # assumes EACH line ends with Ctrl-M

 # IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format
 awk '{sub(/$/,"\r");print}

 # IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format
 awk 1

 # IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format
 # Cannot be done with DOS versions of awk, other than gawk:
 gawk -v BINMODE="w" '1' infile >outfile

 # Use "tr" instead.
 tr -d \r outfile            # GNU tr version 1.22 or higher

 # delete leading whitespace (spaces, tabs) from front of each line
 # aligns all text flush left
 awk '{sub(/^[ \t]+/, ""); print}'

 # delete trailing whitespace (spaces, tabs) from end of each line
 awk '{sub(/[ \t]+$/, "");print}'

 # delete BOTH leading and trailing whitespace from each line
 awk '{gsub(/^[ \t]+|[ \t]+$/,"");print}'
 awk '{$1=$1;print}'           # also removes extra space between fields

 # insert 5 blank spaces at beginning of each line (make page offset)
 awk '{sub(/^/, "     ");print}'

 # align all text flush right on a 79-column width
 awk '{printf "%79s\n", $0}' file*

 # center all text on a 79-character width
 awk '{l=length();s=int((79-l)/2); printf "%"(s+l)"s\n",$0}' file*

 # substitute (find and replace) "foo" with "bar" on each line
 awk '{sub(/foo/,"bar");print}'           # replaces only 1st instance
 gawk '{$0=gensub(/foo/,"bar",4);print}'  # replaces only 4th instance
 awk '{gsub(/foo/,"bar");print}'          # replaces ALL instances in a line

 # substitute "foo" with "bar" ONLY for lines which contain "baz"
 awk '/baz/{gsub(/foo/, "bar")};{print}'

 # substitute "foo" with "bar" EXCEPT for lines which contain "baz"
 awk '!/baz/{gsub(/foo/, "bar")};{print}'

 # change "scarlet" or "ruby" or "puce" to "red"
 awk '{gsub(/scarlet|ruby|puce/, "red"); print}'

 # reverse order of lines (emulates "tac")
 awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' file*

 # if a line ends with a backslash, append the next line to it
 # (fails if there are multiple lines ending with backslash...)
 awk '/\\$/ {sub(/\\$/,""); getline t; print $0 t; next}; 1' file*

 # print and sort the login names of all users
 awk -F ":" '{ print $1 | "sort" }' /etc/passwd

 # print the first 2 fields, in opposite order, of every line
 awk '{print $2, $1}' file

 # switch the first 2 fields of every line
 awk '{temp = $1; $1 = $2; $2 = temp}' file

 # print every line, deleting the second field of that line
 awk '{ $2 = ""; print }'

 # print in reverse order the fields of every line
 awk '{for (i=NF; i>0; i--) printf("%s ",i);printf ("\n")}' file

 # remove duplicate, consecutive lines (emulates "uniq")
 awk 'a !~ $0; {a=$0}'

 # remove duplicate, nonconsecutive lines
 awk '! a[$0]++'                     # most concise script
 awk '!($0 in a) {a[$0];print}'      # most efficient script

 # concatenate every 5 lines of input, using a comma separator
 # between fields
 awk 'ORS=%NR%5?",":"\n"' file



SELECTIVE PRINTING OF CERTAIN LINES:

 # print first 10 lines of file (emulates behavior of "head")
 awk 'NR < 11'

 # print first line of file (emulates "head -1")
 awk 'NR>1{exit};1'

  # print the last 2 lines of a file (emulates "tail -2")
 awk '{y=x "\n" $0; x=$0};END{print y}'

 # print the last line of a file (emulates "tail -1")
 awk 'END{print}'

 # print only lines which match regular expression (emulates "grep")
 awk '/regex/'

 # print only lines which do NOT match regex (emulates "grep -v")
 awk '!/regex/'

 # print the line immediately before a regex, but not the line
 # containing the regex
 awk '/regex/{print x};{x=$0}'
 awk '/regex/{print (x=="" ? "match on line 1" : x)};{x=$0}'

 # print the line immediately after a regex, but not the line
 # containing the regex
 awk '/regex/{getline;print}'

 # grep for AAA and BBB and CCC (in any order)
 awk '/AAA/; /BBB/; /CCC/'

 # grep for AAA and BBB and CCC (in that order)
 awk '/AAA.*BBB.*CCC/'

 # print only lines of 65 characters or longer
 awk 'length > 64'

 # print only lines of less than 65 characters
 awk 'length < 64'

 # print section of file from regular expression to end of file
 awk '/regex/,0'
 awk '/regex/,EOF'

 # print section of file based on line numbers (lines 8-12, inclusive)
 awk 'NR==8,NR==12'

 # print line number 52
 awk 'NR==52'
 awk 'NR==52 {print;exit}'          # more efficient on large files

 # print section of file between two regular expressions (inclusive)
 awk '/Iowa/,/Montana/'             # case sensitive


SELECTIVE DELETION OF CERTAIN LINES:

 # delete ALL blank lines from a file (same as "grep '.' ")
 awk NF
 awk '/./'


CREDITS AND THANKS:

Shell Script esempi SED


cat | grep -n  | sed -n -e '2,$p'
stampa dalla seconda riga in poi.
- stampa tutte le linee di un file tranne i commenti (iniziano con '#')
  cat $FILE | sed -e '/^#/d'
- stampa tutte le linee che iniziano con un numero
  cat $FILE | sed -n "/^[0-9]/p"
- stampa tutte le linee che NON iniziano con un numero
  cat $FILE | sed -n "/^[0-9]/p"
- rimuove tutte le linee vuote (costituite solo da \n)
  cat $FILE | sed -e "/^$/d"
- rimuove tutte le linee formate da spazi (e seguite da \n)
  cat $FILE | sed -e "/^[ ][    ]*$/d"
- rimuove tutte le linee che contengono 'exportPIPPO'
  sed "/export[ |       ]*PIPPO/d" $FILE
- rimuove il path di un filename
  echo "/usr/bin/prova" | sed -e "s/.*\///"
  scrive: prova
- scrive solo il path
  echo "/usr/bin/prova" | sed -e "s/\/[^\/]*$//"
  scrive: /usr/bin
- stampa la 'n'esima riga di un file (nell'esempio la 5a)
  sed -n -e "5p" $FILE
- stampa le righe di un file (nell'esempio dalla 2a alla 5a)
  sed -n -e "2,5p" $FILE
- esempi di sostituzioni:
  sed "s/\"/ /g"        .. sostituisce tutte le '"' con space
  sed "s/,/ /g"         .. sostituisce tutte le ',' con space
  sed 's:bin::'     .. sostituisce il primo 'bin' con nulla

- esempi di estrazioni:
  - id | sed -n "/^uid=[0-9]*(\([^)]*\)).*/s//\1/p"
    estrae: nome dell'user
  - echo "-T100" | sed 's/^-T//'
    estrae: 100

- estrae i campi user e home (il 1o e il 6o) dal file /etc/passwd e formatta:
  cat /etc/passwd | sed 's/\([^:]*\):.*:\(.*\):[^:]*$/_dir=\2 _user=\1/'
  scrive: _dir=/ _user=root
          ...
          _dir=/home/bellina _user=bellina

ESX enable ssh and snmp


Enable SSH on ESX 3.5
ESXi 3.5 does ship with the ability to run SSH, but this is disabled by default (and is not supported). If you just need to access the console of ESXi, then you only need to perform steps 1 - 3.
1) At the console of the ESXi host, press ALT-F1 to access the console window.
2) Enter unsupported in the console and then press Enter. You will not see the text you type in.
3) If you typed in unsupported correctly, you will see the Tech Support Mode warning and a password prompt. Enter the password for the root login.
4) You should then see the prompt of ~ #. Edit the file inetd.conf (enter the command vi /etc/inetd.conf).
5) Find the line that begins with #ssh and remove the #. Then save the file. If you're new to using vi, then move the cursor down to #ssh line and then press the Insert key. Move the cursor over one space and then hit backspace to delete the #. Then press ESC and type in :wq to save the file and exit vi. If you make a mistake, you can press the ESC key and then type it :q! to quit vi without saving the file.
6) Once you've closed the vi editor, run the command /sbin/services.sh restart to restart the management services. You'll now be able to connect to the ESXi host with a SSH client.
Tip - with some applications like WinSCP, the default encryption cipher used is AES. If you change that to Blowfish you will likely see significantly faster transfers.
Update for ESXi 3.5 Update 2 - With Update 2 the service.sh command no longer restarts the inetd process which enables SSH access. You can either restart your host or run ps | grep inetd to determine the process ID for the inetd process. The output of the command will be something like 1299 1299 busybox      inetd, and the process ID is 1299. Then run kill -HUP (kill -HUP 1299 in this example) and you'll then be able to access the host via SSH.



ENABLE SSH AND SNMP ON ESX
There are two ways to go about with this depending upon your installation
a. If your esxi installation is new and/ or your evaluation license is still valid, then go to the section that says Enable       SNMP via RCLI
b. If your esxi installation has been in production for a while / and or you got a license installed and your evaluation period has expired then go to the following section which is to enable SNMP via SSH. For this you will need to enable SSH first on your esxi host by following the steps detailed below.
Enabling SSH on your ESXI host.
  1. Shutdown all your running VM’s on the esxi host and place the host on Maintenance mode.
  2. Log onto your Esxi Console physically from the box by pressing F2
  3. Type in your root password
  4. Press ALT+ F1 to enter the Technical Support login. Beware that any changes from here will not be warranted for support with VMware. So please be careful and test this and practice on a test esxi host before going live with production.
  5. Type unsupported and press enter
  6. Type in your root password
    at the prompt again and press enter
  7. Now you need to edit the inetd.conf file. To do this type vi /etc/inetd.conf
  8. Scroll down using your down arrow key sort of towards the end of the screen where you would see two lines starting with #ssh. Place the cursor on top of the # and press delete. Do this for the next line as well.
  9. Now press SHIFT+: and type wq to save the file.
  10. Now you will be back at your #prompt. To restart the service type services.sh restart.
  11. Press ALT+F1 to return to the esxi host console page.
  12. Press ESC to log off
  13. Press F12 to initiate your restart of the esxi host.
  14. You will be promoted for your root password and press enter.
  15. Press F11 to restart the host.
  16. Once the esxi host is up and running again use your favourite SSH client mine is Putty and WinSCP to test SSH connectivity to your esxi host.
Enabling SNMP via SSH on your ESXI host.
  1. Once you got SSH running , download and install WinSCP on your workstation..
  2. Open WinSCP and type in the details for your esxi host including IP address, root as the username and root password. Make sure you change connection type to SCP from default SFTP option as shown
http://wellytonian.com/wp-content/uploads/2010/03/032410_2152_EnablingSSH1.png
  1. Once logged in your WinSCP window will look like the follow. On the Esxi host side navigate to /etc/vmware and find the file called snmp.xml
http://wellytonian.com/wp-content/uploads/2010/03/032410_2152_EnablingSSH2.png
  1. Drag and drop the snmp.xml file from the right to the left of the screen.
http://wellytonian.com/wp-content/uploads/2010/03/032410_2152_EnablingSSH3.png
  1. Open the document with your favourite Notepad editor. I am using Notepad++
http://wellytonian.com/wp-content/uploads/2010/03/032410_2152_EnablingSSH4.png
  1. It would have the following default settings:
http://wellytonian.com/wp-content/uploads/2010/03/032410_2152_EnablingSSH5.png
  1. First and foremost make a copy of the file before editing it and save it with a different file name so that if something goes wrong you always have a copy of the original.
  2. Now do the following editing to it and save it with same file name snmp.xml.
http://wellytonian.com/wp-content/uploads/2010/03/032410_2152_EnablingSSH6.png
  1. Save the file and now go back to WinSCP and drag the file from the left to the right and accept the warning message to overwrite the file
http://wellytonian.com/wp-content/uploads/2010/03/032410_2152_EnablingSSH7.png
  1. Once that is done restart your esxi host and test SNMP which will be followed up in the next section.
Enabling SNMP via RCLI ( Only works for New Installation of Esxi , Valuation License still valid state)
  1. Download the Remote Command Line Interface from Vmware RCLI Download for Vsphere.
  2. Install it on your workstation.
  3. To start RCLI browse to your VMware folder under all programs and kick start the command line under vmware
http://wellytonian.com/wp-content/uploads/2010/03/032410_2152_EnablingSSH8.png
  1. At the command prompt type cd bin and then do a dir. This Directory has all the scripts that are required to run a variety of tasks on your esxi and esx servers.
  2. Type the following command
vicfg-snmp.pl –server ESXISERVERIP -c SNMPCOMMUNITYSTRING -t MONITORINGSERVERIP@162_or_161/SNMPCOMMUNITYSTRING
  1. To enable SNMP type vicfg-snmp.pl –server ESXISERVERIP –E
  2. To show the running config of SNMP on your esxi server type vicfg-snmp.pl –server ESXISERVERIP –s
  3. To test the SNMP confi type vicfg-snmp –server ESXISERVERIP –T
As you may have seen in the heading of this section this works only for new installation of esxi or those that have their Valuation license still valid (60 Days). For those who don’t fall into the category the only option is to go with SSH and then enabling SNMP option.
You will see this error if you try via RCLI
http://wellytonian.com/wp-content/uploads/2010/03/032410_2152_EnablingSSH9.png
This is because remote write permission is disabled on the free version of esxi.
http://wellytonian.com/wp-content/uploads/2010/03/032410_2152_EnablingSSH10.png
Now user your favourite SNMP monitoring station Cacti, Zenoss, Spiceworks there are so many to start monitoring your Esxi environment.
Next post update on this will be to check on Cacti monitoring these ladies
I would really appreciate some feedback for this article as to if this can be done in a simpler way or if there is a way to hack the write permission issue for the free esxi hosts.


Enabling SNMP on VMware ESXi Servers
The following procedure enables SNMP on VMware ESXi.
Notes:
  • The free version of VMware ESXi does not support SNMP.
  • The following procedure to enable SNMP requires the vSphere command line interface (CLI). The vSphere CLI is not packaged with your ESXi Server by default, so you will need to download it from VMware, as indicated.
To enable SNMP on VMware ESXi:
  1. Download and install the VMware vSphere command line interface from the VMware Download Center (http://downloads.vmware.com/d/).
  2. Use the vSphere CLI to view your ESXi server SNMP settings, as indicated in the following procedure:
    1. In the Perl\bin directory of your vSphere installation, execute the following script:
      perl ..\..\bin\vicfg-snmp.pl --server ip_address –s
      Notes:
      • C:\Program Files\VMware\VMware vSphere CLI\Perl\bin is the default location of the vSphere Perl\bin directory.
      • Replace ip_address with the IP address of your ESXi server.
    2. Enter an appropriate user name at the prompt.
      Note: For most environments,
      root should be sufficient.
    3. Enter the associated password at the prompt.
  3. Use the vSphere CLI to enable SNMP on your ESXi server, as indicated in the following procedure:
    1. In the Perl\bin directory of your vSphere installation, execute the following script to add an appropriate community string:
      perl ..\..\bin\vicfg-snmp.pl --server ip_address -c cstring
      Note: Replace
      ip_address with the IP address of your ESXi server, and replace cstring with the community string you are adding. For most environments, the community string public should be sufficient.
    2. Enter an appropriate user name at the prompt.
      Note: For most environments,
      root should be sufficient.
    3. Enter the associated password at the prompt.
    4. In the Perl\bin directory of your vSphere installation, execute the following script to enable SNMP:
      perl ..\..\bin\vicfg-snmp.pl --server ip_address –E
      Note: Replace
      ip_address with the IP address of your ESXi server.
    5. Enter an appropriate user name at the prompt.
      Note: For most environments,
      root should be sufficient.
    6. Enter the associated password at the prompt.
  4. Reboot your ESXi server to allow settings to take effect.