Sunday, September 25, 2011

Perl function to get Date and Time in required format

# Function Name    : getDateTime
# Input            : Time in epoch or it could be empty.  In latter case, it would return current date and time.
# Output        : current time in dd-mm-yyyy hh:mm:sec format.
# Description    : This function returns current date and time in above given format.
# Created By    : Rinkesh Bansal
# Date            : 25-Sep-2011
#
sub getDateTime($)
{
    my ($input) = @_;
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);

    if (trim($input) eq "")
    {
        ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    }
    else
    {
        ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($input);
    }

    $mon = $mon+1;
    $year = $year + 1900;

    $mday = (length($mday) eq 1)?0 . $mday:$mday;
    $mon = (length($mon) eq 1)?0 . $mon:$mon;
    $hour = (length($hour) eq 1)?0 . $hour:$hour;
    $min = (length($min) eq 1)?0 . $min:$min;
    $sec = (length($sec) eq 1)?0 . $sec:$sec;

    return "$mday-$mon-$year $hour:$min:$sec"; // change this line to change the format you like it to return
}

Thursday, September 8, 2011

Running Netmeeting on Windows 7 box

Hi Everyone,

As you may have noticed that Windows-7 doesn't allow netmeeting.  But many users (like me) wanted netmeeting badly.  Follow below steps to make netmeeting work.

- Download and install Windows Virtual PC (approx 29 MB) and  Windows XP Mode (approx 470 MB) from
http://www.microsoft.com/windows/virtual-pc/download.aspx

- Install Windows Virtual PC

- Install Windows XP mode

- Launch Windows XP mode

- Login to it

- Run netmeeting as usual

- if you want to share your desktop (only screens of virtual pc not windows 7) then follow below steps
  • Close NetMeeting
  • Select "Disable integration Features" from the "Tools" menu
  • Launch NetMeeting again 
- Enjoy

Note:  There is one catch here.  You cannot share the desktop of your Windows-7, only desktop of your virtual PC will be shown. 

Tuesday, August 9, 2011

Transferring display of non-windows machine using Putty and X-Manager

Pre-requisites
1. Download and Install Putty.exe.  Latest putty can be downloaded from http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
2. Doiwnload and Install XManager 2.0 or above.  XManager can be downloaded from http://download.cnet.com/Xmanager/3000-7240_4-10038129.html

Steps you need to follow
1. Go to Start->Programs->Xmanager2->Xmanager - Passive
2. Launch Putty and load/provide the IP of the machine you wanted to connect
3. Goto SSH->X11->Enable X11 forwarding
4. Click Open
5. Set Display environment variable by using below command

    export DISPLAY=:0.0
 6. Test if display is transferred by using xclock command

Note: This will work only if host is having SSH.

Monday, July 18, 2011

Compare two files

use File::Compare;

# Function Name : compareFile
# Input : Two files to compare.
# Output : Pass or Fail.
# Description : This function will compare two given files. If both files are same then it will return Pass
# else it will return Fail.
#
sub compareFile($$)
{
my ($file, $exp_file) = @_;

$file = convertSlash($file);
$exp_file = convertSlash($exp_file);

my $result = "";
if (compare($file, $exp_file) == 0)
{
$result = "Pass";
}
else
{
$result = "Fail";
}

return $result;
}

Getting OS Name

my $os = $^O;
print "$os\n";

Getting Current working Directory

use Cwd;

my $pwd = getcwd();
print "$pwd\n";

Convert unix path to windows path by converting frontslash to backslash

# Function Name : windowsSlash
# Input : String with or without slash
# Output : Path with double backslash
# Description : This function will convert all forward slash (/) to double backward slash(\\). Also,
# extra slashes will be removed.
#
sub windowsSlash($)
{
my ($str) = @_;
$str =~ s/\/\//\//g;
$str =~ s/\//\\\\/g;
return $str;
}

Convert windows path to unix path by converting backslash to frontslash

# Function Name : convertSlash
# Input : String with or without backslash
# Output : String without backslash
# Description : This function will convert all backslash (\) to front slash(/). Also, extr slashes
# will be removed. For Ex: Suppose given string is
# C://rinkesh//bansal//abc.pl then it will be converted to C:/rinkesh/bansal/abc.pl
#
sub convertSlash($)
{
my $str = $_;
$str =~ s/\\/\//g;
$str =~ s/\/\//\//g;
return $str;
}

Renaming a file

use File::Copy;

# Function Name : renameFile
# Input : Name of the Source file and destination file name
# Output : None
# Description : Renames source file to destination file name
# See my other blogs for FileExists function
sub renameFile()
{
my ($source, $destination) = @_;
FileExists($source);
rename($source,$destination);
}

Check if File Exists

# Function Name : FileExists
# Input : Filename with path
# Output : None
# Description : Check if file exists
sub FileExists($)
{
my $filename = $_;
unless (-e $filename)
{
die "Error Could not find $filename\n";
}
}

Logging message in log file

# Function Name : testlog
# Input : message and date. Date is optional
# Output : None
# Description : logs time and message and appends \n to non-empty strings
sub testlog($$)
{
my ($message,$logdate) = @_;
my $datestring = $logdate ? &getDateTime() . " :: " : "";
return unless $message; # we do not log empty lines
open (FILE, ">>$testlog") or die("Could not log to $testlog");
print FILE $datestring . "$message\n";
print $datestring . "$message\n";
close FILE;
}

Perl trim function

# Function Name : trim
# Input : String with or without spaces around it
# Output : String without spaces around it
# Description : This function would remove whitespace from the start and end of the string
#
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}