Sunday, November 13, 2011

Login to Gmail with Win32::IEAutomation


#!/usr/bin/perl

use strict;
use Win32::IEAutomation;
my $VERSION = "1.0";

if($#ARGV != 1)
{
print "\n";
print "************************************************************\n";
print "Usage: Gmail.pl \n";
print "Gmail.pl - Login to gmail account with Internet Explorer.\n";
print "- Chetan Giridhar \n";
print "************************************************************\n";
exit(0);
}
     
# Creating new instance of Internet Explorer.
my $ie = Win32::IEAutomation->new( visible => 1, maximize => 1);
     
# Navigating to www.google.com.
$ie->gotoURL('http://www.google.com');
     
# Finding hyperlinks and clicking them
# Using 'linktext:' option (text of the link shown on web page)
$ie->getLink('linktext:', "Gmail")->Click;

my $user = $ARGV[0];
my $password = $ARGV[1];

# Using 'name:' option
$ie->getTextBox('name:', "Email")->SetValue($user);
$ie->getTextBox('name:', "Passwd")->SetValue($password);


# Finding button and clicking it
# using 'caption:' option
$ie->getButton('caption:', "Sign in")->Click;


Friday, November 12, 2010

PyRebootOps

Introduction

Often in Windows systems we observe that move or delete operations can’t be executed on a file if it’s locked. Files can get locked if different processes start accessing it or if the file has already been loaded in RAM.

Such problems can be resolved by scheduling file operations for the next system restart before the processes or services start and set locks on the files.

PyRebootOps is a Python utility that uses windows mechanism for scheduling operations on a file so that these operations can be executed during next system restart.

PyRebootOps

* Schedules – Move, Rename, Delete operations on locked files.
* View and Reset scheduled operations.
* Restart the user system.

PyRebootOps Help

***********************************************************
PyRebootOps1.0: Schedule file opeartions to be executed
during the next system reboot. – Chetan Giridhar
***********************************************************

syntax: PyRebootOps.exe [...]

-move: Moves a file from source to destination.
and are required.

-delete: Deletes the file from the harddisk after reboot.
not required.

-rename: Renames a file.
and are required.

-scheduled: Prints all the scheduled operations for the next reboot.

-reset: Resets all the operations that were previously scheduled.

-reboot: Restarts the system after a timeout of 1 sec.

PyRebootOps on sourceforge

Wednesday, July 21, 2010

DOS: SystemInfo

Problem Statement:
Using DOS command to get all the information pertaining to the sytem.

Solution:
Use of Dos coammnd: systeminfo.
Below is a typical output of systeminfo command on DOS prompt.


Using Perl Script to get OS Name:
use strict;
system("systeminfo find \"OS Name\" > C:\\temp\\temp.txt");
open(FH, "<", "C:\\temp\\temp.txt");
my $contents = ;
my @osInfo = split(':', $contents);
$osInfo[1] = s/\s\s+//g;
print $osInfo[1];

#This script would print the OS Name. Like it would print "Microsoft Windows 7 Utlimate" if run on Windows 7 - Ultimate Version of OS.

Tuesday, July 13, 2010

Perl: Using WMI in Perl

Problem Statement: Using WMI in Perl

Solution:

use Win32::OLE;

$instances = Win32::OLE->GetObject("winmgmts:\\\\localhost\\root\\CIMV2")->InstancesOf(Win32_LogicalDisk) or die "Cant access WMI";

for my $proc (in $instances)

{

printf "%5d %7d \n", $proc->{ProcessId}, $proc->{WorkingSetSize};

}

More examples on: http://www.perlmonks.org

Friday, July 9, 2010

Python: Threading in Python

Problem Statement:
Demonstrate the usage of threading in python.

Scripts:
Let's consider an example where we have two scripts, one of them is the "main.py" script that can be considered as parent and the other that would run in threads "log.py".

Let's first take a look at
"log.py"
---------
import time

 class log:
  def __init__(self, times):
   self.times = times
   self._bOn = True

  def start(self):
   while (self._bOn):
    print "in start"
    time.sleep(self.times)


  def stop(self):
   self._bOn = False
   print "in stop"

log.py contains a class log with constructor that takes argument for time and sets the bOn variable to True.
start() - starts printing "in start" till the time bOn is True.
stop() - prints "in stop" when it is called. It would also stop the start() function as bOn is set to False now.

Now on to the parent script that would call log.py to run in thread.
"Main.py"
----------
import os
import log
import time
import thread

obj = log.log(5)

def startlog():
 print "in thread"
 obj.start()

thread.start_new_thread(startlog,())
time.sleep(11)
obj.stop()

Main.py, imports log.py with constructor argument as "5".
startlog() - prints "in thread" and then would call start() of log.py
startlog() is now run as a thread
Main script sleeps for 11 seconds and then
calls the stop() of log.py

Explanation:
In this example, when Main.py starts running, it imports log.py and sets the argument(time) of the constructor to 5. It then starts a thread for the function startlog() and goes to sleep for 11 seconds.
Now that a thread has started with startlog() function, it prints "in thread", then calls start() of log.py. Now that bOn is True, it prints "in start" and sleeps for 5 seconds as set by Main.py. After 5 seonds again it prints "in start".
During this time the Main.py is still sleeping. When 11 seconds pass by, Main.py comes out of sleep (log.py is still running though) and calls the stop() of log.py. Because of which, start() gets stopped as bOn is now set to False and it prints "in stop".
Thus we have seen, Main.py and log.py both ran in parallel and Main.py could control the execution of log.py as it was run in thread.

Friday, July 2, 2010

C++: Get Function Name inside the Function

Problem Statement:
Recently I faced a problem in C++ where I wanted to know the name of the function inside the function that was being called...Confused?! I meant, if I am writing the definition of function named Func(), I want to know the name "Func" inside the function.

Solution:
Solution is the use of printf("%s", __FUNCTION__);
The attribute __FUNCTION__ contains the name of the function which is being executed.
This is what we needed, isn't it?

Hope this helps!

Sunday, June 27, 2010

Python: Making Objects Callable

Problem Statement: Have you ever wandered if we could make an object callable? Yes, I mean just use the object name as if you were calling function! Intersted?
Here's a simple solution!

Solution:
class Add:
#class for addition
 def __init__(self, num1, num2):
  self.num1 = num1
  self.num2 = num2
  print "Sum of", self.num1, "and", self.num2, "is:"

 def __call__(self):
  return (self.num1 + self.num2)

add = Add(1,2)
print add()

Output:
Sum of 1 and 2 is:
3

Explanation:
In this example, when 'add' is created using add = Add(1,2), def __init__() is called, since the constructor is called while object is getting created.
Because of the attribute __call__, the object becomes callable and hence we could use as add(). when add() is used, def __call__() is called.

Hope it's clear!