Wednesday, December 14, 2011

Basics of Powershell ~ empowering Windows Config/Admin Automation

Basics  of  MS Windows Powershell


Introduction
A dotNet framework based scripting language to automate the configuration/administration of Microsoft Windows machine.
Powershell is loaded with several cmdlets (special command-lets) acting as a built-in shell utilities to perform different tasks on Windows machine for performing administrative tasks.


Getting Started
Powershell's cmdlets act upon and return objects as a result of any action taken.
These can be used in combination with traditional Windows services like Registry, Net and more.

To try your hands over powershell, access it at 'Start Menu' > 'Accessories' > 'Windows Powershell';
there you'll get mainly a shell from 'Powershell', and an interactive IDE-like shell 'Powershell ISE'.

Using powershell, cmdlets are the main power-source of this Powershell which are discussed briefly below.....

to get a quick /Hello World/ feel of Powershell, you could try on few next steps
* emulating a dos command 'echo' used to print message at console, 
   command: 
      Write-Host "[get-help write-host] would tell about cmdlet write-host"
* emulating a dos command 'cd' used to change current directory, 
   command: Set-Location C:\Temp
* emulating a dos command 'mkdir' used to create directory, 
   command: New-Item -name "ztemp" -type directory -Force
* emulating a dos command 'dir' used to list items in current directory, 
   command: Get-ChildItem -path C:\Temp
* even the windows 'dir' command can be executed and played with inside powershell.
.....could get a list of cmdlets available in Powershell2.0 ~ http://ss64.com/ps/


[] cmdlets
Command-lets have a specific name format of /'verb'-'noun'/ such as 'Get-ChildItem', 'Get-Help', etc.
To know more about any 'cmdlet' (like using man in linux shell), use
command: Get-Help <_cmdlet_> -detailed

[] A Glimpse of system level SuperUser stuff, as possible on a linux shell
  [+] to get a listing of all System Services,
          command:  Get-Service
  [+] to just get a listing of 'Spooler' named system service, telling its 'name','status' and 'display name'
          command:  Get-Service | Where {$_.name -eq 'Spooler'}
  [+] to get current state of 'Spooler' named system service, whether its running/stopped/paused
          command:  Get-Service | Where {$_.name -eq 'Spooler'} | %{ $_.status }
  [+] to have a Powershell script, check for a system service..... start it if stopped
  $svc_name = "aspnet_state"
  $svc_status = Get-Service | Where {$_.name -eq $svc_name} | %{ $_.status }
  if (-not $svc_status) {
     Write-Host "Error: $svc_name not found"
  }
  elseif ($svc_status -eq "running"){
     Write-Host "status ok $svc_name"
  } else {
     Start-Service $svc_name
  }
  [+] Now you can save the script above as any file say 'start_aspnet.ps1', but to execute it as an external script you would need the local system's Execution Policies to be unrestricted for the script.
       It's not advisable to have it un-restricted all the times, so you could pass on the specific modes along-with the script you desire to be run in un-restricted mode. As below.....
          cmd_prompt:> powershell -executionpolicy unrestricted -file ".\start_aspnet.ps1"
     


helpful links:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa973757(v=vs.85).aspx
http://blogs.msdn.com/b/powershell/
http://thepowershellguy.com/blogs/posh/default.aspx
http://powershell.com/cs/
http://stackoverflow.com/questions/496234/what-tutorial-do-you-recommend-for-learning-powershell

Saturday, November 12, 2011

XML-Motor v0.0.3 ~ mine XML once..... multiple search

released v0.0.3 of xml-motor with following update: http://rubygems.org/gems/xml-motor

Earlier:
  * it just supported one search per every XML data processing
Now:
  * could retrieve processed XML 'Information' once, and search again & again with minimized cost

Now, along-with.....
  * a single method-call search for Tag (w/ or w/o Attribute) in provided XML String or File
You also have the capability to.....
  * fetch the two (or one, depends how you wanna roll) required set of Information mined by providing it XML String
  * then provide that information everytime to search anything, and hence making it way quicker for multiple searches


So, the improved way for multiple searches:

  First, update your 'xml-motor' gem using
    $ gem install xml-motor

  Now, the way to use it
#!/usr/bin/env ruby
require 'xml-motor'

fyl = File.open "myXMLfile.xml"
str = fyl.read
xNodes = XMLMotorEngine._splitter_ str
xTags = XMLMotorEngine._indexify_ xNodes
result1 = XMLMotorEngine.pre_processed_content(
            xNodes, xTags, "h1")

result2 = XMLMotorEngine.pre_processed_content(
            xNodes, xTags, "div", "class='wanted'")

Wednesday, November 9, 2011

[new portable algo] Easy XML Parsing using XML-Motor (currently just in Ruby)

Easy XML Parsing using XML-Motor 
(currently just implemented in Ruby as a Gem)
Using a new, compact algorithm of XML Partsing

===================================================
v 0.0.2
@GitHub: https://github.com/abhishekkr/rubygem_xml_motor

@RubyGems: http://rubygems.org/gems/xml-motor
===================================================

A new short XML Parsing Algorithm implemented in LessThan 200 Ruby lines.
An easy-to-use XML Parser without any Native Dependencies.

[How To Use]

Installing and Loading:
   + $ gem install xml-motor
   + in your ruby code: require 'xml-motor'

Usage:
  + To find values of an xml node from an xml file
      XMLMotor.get_node_from_file [file_name_with_path], [tag_name]
  + To find values of an xml node from an xml string
      XMLMotor.get_node_from_content [xml_data], [tag_name]
  + To find values of an xml node with a tag_name having required attribute
      XMLMotor.get_node_from_content [xml_data], [tag_name], [attr_key=attr_value]

Example Calls As Code:
 + XMLMotor.get_node_from_content "<A i='1'>a<A><B>b<C>c<C><A>ba<A><B>", "A"
     RETURNS: ["a", "ba"]
 + XMLMotor.get_node_from_content "<A i='1'>a<A><B>b<C>c<C><A>ba<A><B>", "B.A"
     RETURNS: ["ba"]
 + XMLMotor.get_node_from_content "<A i='1'>a<A><B>b<C>c<C><A>ba<A><B>", "A", "i='1'"
     RETURNS: ["a"]

===================================================

[Example Code]

require 'xml-motor'

puts XMLMotor.get_node_from_file "eloqjs.htm", "h1"
 
fyl = File.open("elogjs.htm")
XMLData = fyl.read

puts XMLMotor.get_node_from_content XMLData, "div.span"

puts XMLMotor.get_node_from_content XMLData, "div"

===================================================

Thursday, October 20, 2011

http_voodoo_mongo ~ remote control your MongoDB over HTTP

@github: https://github.com/abhishekkr/http_voodoo_mongo

project name: http_voodoo_mongo
platform:     linux
requirement:  ruby

usage: it's a remote controller for your local/remote MongoDB instances working over an HTTP mocking service

What it consists of?

[] a rakefile 'Rakefile' allowing you to auto install and start 'http_voodoo_mongo' as a system service using "rake http_mongo:install" task.

[] a ruby script 'mongo_voodoo.rb' which mocks HTTP Server and allows you control MongoDB using HTTP Requests.

[] a system service script 'mongo_voodoo' which enables you to start||stop||status||restart the Ruby Script as a System Service

*currently I've just added "mongodump" task to it (with & without authorization detail), will slowly add more..... it's all opensource, even you can update if you want


======================================================
a REST API for Remote Controlling MongoDB Server over HTTP using the same 'authentication' as of MongoDB Server

[] need to access the service at HTTP_URL => http://[MongoDB_IP]:30303/

[] Optional GET Request Parameters [name] the username permitted access [pass] the password linked to username [port] the port at which MongoDB Server is running

 ::MongoDB Database Backup 
  http://[MongoDB_IP]:30303/[db_name]?action=dump&dir=[dir]&more_switch=[more_switch]&userniame=[username]&password=[password]&port=[port] 

[] GET Parameters [dir] the directory to take in backup using mongodump [more_switch] any options you wanna add to mongodump command 
======================================================

Tuesday, August 9, 2011

Make *nix automatic re-spawn a process/service if stopped


To make your *nix system automatic re-spawn a process/service if stopped ~

Write a shell script to check for presence of a service and start it if it is not running.
Then attach that shell-script to a cronjob running at frequent intervals.

Example Script to monitor Apache HTTPD web server and start it if is not started ~
#!/bin/sh
pids_=`ps ax | grep /usr/sbin/httpd | grep -v grep | wc -l `
if [ $pids_ -ne 0 ];
then
     echo "Apache is running"
else
     echo "Apache was stopped. Starting..."
     apache_start_=`/etc/init.d/httpd start`
     if [ $? -eq 0 ];
     then
         echo "Apache Started Successfully"
     fi
fi
#############################
 this simply checks for HTTPD process to be present and re-spawn the service in absence of even a single process thread.

There is a wonderful Ruby utility "bluepill" to achieve the same solution in a more advanced and comfortable manner. Cover it sooooon.....

Thursday, July 28, 2011

derailed_cuke ~ now test any website using cucumber as an independent testing utility

GitHub Linkhttps://github.com/abhishekkr/abk-labs.cucumber
[] What is "Cucumber"?
+ http://cukes.info/
Cucumber lets software development teams define the behavior of any application in Business Readable DSL as plain text.
Hence serves as documentation, automated tests and development aid ~ All In One.


[] What is "derailed_cuke"?
+ Project Repo @GitHub


[] How To get "derailed_cuke"? + you could either clone the repo or download compressed project in following ways ~
Clone Repository:
#git clone git://github.com/abhishekkr/abk-labs.cucumber.git


[] Pre-requisites for "derailed_cuke"?
+ Ruby, RubyGems & Bundler Gem installed


[] How to use "derailed_cuke"?
+ The downloaded project would have a directory "abk-labs.cucumber" with a directory "derailed_cuke" inside it.
~~~~~~~~~~
Step#1 Now all you need to do is open a console, change current working directory to "derailed_cukes".|
Step#2 Run "#bundle install" to install all required gems.
|

Step#3 If your ruby binary file is accessible by any name other than 'ruby' {i.e. say you are jruby and thus access it using 'jruby'}; then edit the file "Fcuke.rb" and replace the value of variable "RUBY_BIN" in line number 2 with the name you access your Ruby Binary.
|
Step#4 
Now simple execute "
Fcuke.rb" and by default it will check for two (already present for testing) features checking Google.com and Twitter.com
|
Step#5 
To add custom web-application behavioral tests ~ user will need to write the custom Cucumber Features and respective Step Definitions {as in general scenario for using Cucumber}. For reference present feature and step definitions can be seen & used.

~~~~~~~~~~

[] Where to learn cucumber?
+ http://wiki.github.com/cucumber/cucumber/tutorials-and-related-blog-posts
In current release of "derailed_cuke", user need to know writing Cuke Features and Step Definitions.


………………………………………………………
[] Planned Goals for the Project involves
+ auto-generation of basic tests by analysis of application,
 then major task left would be to add uncovered custom tests
 and tweak the generated tests (if required).


………………………………………………………

Wednesday, June 22, 2011

Host Your Site on Google AppEngine in 7 Easy Steps

Project/Example Site: http://gae-flat-web.appspot.com



Host Your Site on Google AppEngine in 7 Easy Steps:
  1.  Register any $App at Google ApEngine using your Gmail Account
    {as if $App is you Application Name giving you url like $App.appspot.com}

  2.  Download the Python Script 'flat.py' provided on this site or from repo
  3.  Execute #python flat.py $App
  4.  It will created required structure in a dir $App
  5.  Copy all your static site to $App/flat_web/*
  6.  Download "Google AppEngine Python SDK"
  7.  Update this $App using Google AppEngine SDK as
        #python appcfg.py update $Path_to_App

[[ Your own Static Content Website served by Google AppEngine ]]

 It's GitHub Repository: https://github.com/abhishekkr/gae-flat-web

Wednesday, May 25, 2011

Cacti for IT infrastructure monitoring & graphing ~ TechXpress Guide

Cacti for IT infrastructure monitoring & graphing ~ TechXpress Guide

.::Task Details::.
[] Setting up Cacti for IT Infrastructure Service Graphing solution

@Scribd: http://www.scribd.com/doc/54585795/A-TechXpress-Guide-Cacti-for-IT-Service-Monitoring-Graphing?in_collection=3004309

@Slideshare: http://www.slideshare.net/AbhishekKr/an-express-guide-cacti-for-it-infrastructure-monitoring-graphing
A TechXpress Guide Cacti for IT Service Monitoring Graphing

Thursday, May 5, 2011

Nagios for IT Infrastructure Monitoring ~ TechXpress Guide

Nagios for IT Infrastructure Monitoring ~ TechXpress Guide


::Task Detail::
 Setting up Nagios machine on a LAN to monitor resources and services
 Generating an e-mail notifications if any of them goes down



@Scribd: http://www.scribd.com/doc/54585786/A-TechXpress-Guide-Nagios-for-IT-Infrastructure-Monitoring


@Slideshare:

SNMP for Secure Remote Resource Monitoring ~ TechXpress Guide

SNMP for Secure Remote Resource Monitoring ~ TechXpress Guide


It's an Express Guide to "Basic & Secure Setup of SNMP with purpose of Remote Resource Monitoring" ~~~~~ described here with a use-case of setting it up for monitoring availability of Network Connection on a remote machine and Trap notification in case the link goes down ~~~~~ for both Linux & Windows platforms


@Scribd: http://www.scribd.com/doc/53003044/An-Express-Guide-SNMP-for-Secure-Rremote-Resource-Monitoring


@SlideShare: