Sunday, November 17, 2013

Configuring VNC server to display Gnome/KDE desktop when connected through VNC client

We all are tired and irritated with the default UI shown by VNC client and would love to have UI showing more menu's and user friendly UI when connected through VNC client.  Here are the simple steps which will show you user friendly Gnome desktop when connected through VNC.

Few basic assumptions:
  • VNC server is already installed on the machine 
  • User can find xstartup file of VNC in /root/.vnc folder on Redhat Linux boxes. User needs to find corresponding folder for other operating systems.

Steps to change the Desktop
  • Go to .vnc folder
  • Edit xstartup file using editors like vi
  • Initial xstartup file will look something like:
    #!/bin/sh

    # Uncomment the following two lines for normal desktop:
    # unset SESSION_MANAGER
    # exec /etc/X11/xinit/xinitrc

    [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
    [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
    xsetroot -solid grey
    vncconfig -iconic &
    xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
    twm &
  • Change it to below to start gnome desktop
     #!/bin/sh

    # Uncomment the following two lines for normal desktop:
    # unset SESSION_MANAGER
    # exec /etc/X11/xinit/xinitrc

    [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
    [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
    xsetroot -solid grey
    vncconfig -iconic &
    xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
    #twm &
    exec gnome-session &
  • Change it to below to start KDE desktop
     #!/bin/sh

    # Uncomment the following two lines for normal desktop:
    # unset SESSION_MANAGER
    # exec /etc/X11/xinit/xinitrc

    [ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
    [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
    #xsetroot -solid grey
    #vncconfig -iconic &
    #xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
    #twm & 
    startkde &
  • Kill all the VNC server sessions which are running using command
    vncserver -kill :1
  • Start VNC server 

Tuesday, October 9, 2012

Array to Hash

my @arr = ("Hundred", 100, "Ten", 10, "Thousand", 1000);
my %denomination = map { $_->{key} => $_->{value} } @arr;

while (($key, $value) = each(%denomination))
{
     print "$key, $value\n";
}

Output:
Thousand, 1000
Hundred, 100
Ten, 10 

Wednesday, September 26, 2012

Deletes property in property file if property value matches the given value

# Function Name    :: deleteParameterForValue($$$$)
# Input            : 1. name of the property file
#                  2. Parameter name
#                  3. Parameter Value
#                    4. Delimiter
# Output        : none. Deletes the given parameter from a given file based on value
# Description    : Deletes given property from a given file if its value matches the given value
sub deleteParameterForValue($$$$)
{
    my ($file, $parameter_name, $parameter_value, $delimiter) = @_;
    $parameter_name = &trim($parameter_name);
    $parameter_value = &trim($parameter_value);
    $delimiter= &trim($delimiter);

    $file = &convertSlash(&trim($file));
    my $new_str = "";
    open (FI, $file) or do {  return -1;  };

    while ()
    {
        my ($str) = $_;
        chomp ($str);
        $str = &trim($str);

        unless (($str eq "") || (substr($str, 0, 1) eq "#"))
        {
            my $key = &trim(substr($str, 0, index($str, $delimiter)));
            my $value = &trim(substr($str, index($str, $delimiter) + 1));
           
            if (($key eq $parameter_name) && ($value eq $parameter_value))
            {
                next;
            }
            $str = join($delimiter, $key, $value);
        }
       
        $new_str .= "$str\n";
    }
    close (FI);

    open (FO, ">$file") or do { return -1 };
    print FO $new_str;
    close(FO);
}

Deletes property in property file if property name matches the given parameter

# Function Name    :: deleteParameterForKey($$$)
# Input            : 1. name of the property file
#                 2. Parameter name
#                 3. Delimiter
# Output        : none. Deletes the given parameter from a given file
# Description    : Deletes given property from a given file
sub deleteParameterForKey($$$)
{
    my ($file, $parameter_name, $delimiter) = @_;
    $parameter_name = &trim($parameter_name);
    $delimiter= &trim($delimiter);

    $file = &convertSlash($file);
    my $new_str = "";
    open (FI, $file) or do {  return -1;  };

    while ()
    {
        my ($str) = $_;
        chomp ($str);
        $str = &trim($str);

        unless (($str eq "") || (substr($str, 0, 1) eq "#"))
        {
            my $key = substr($str, 0, index($str, $delimiter));
            my $value = substr($str, index($str, $delimiter) + 1);
           
            if ($key eq $parameter_name)
            {
                next;
            }
            $str = join($delimiter, $key, $value);
        }
       
        $new_str .= "$str\n";
    }
    close (FI);

    open (FO, ">$file") or do { return -1 };
    print FO $new_str;
    close(FO);
}

Update value of a parameter in a property file

# Function Name    :: updateDataFile($$$$)
# Input            : 1. name of the property file
#                 2. Parameter name
#                 3. Parameter value
#                 4. Delimiter
# Output        : none
# Description    : Opens the given property file and update the value of given prameter
sub updateDataFile($$$$)
{
    my ($file, $parameter_name, $parameter_value, $delimiter) = @_;
    my $found_flag = 0;

    $file = &convertSlash($file);
    my $new_str = "";
    if (not -e $file)
    {
        open (FO, ">$file") or do { return -1 };
        close (FO);
    }
    open (FI, $file) or do {  return -1;  };

    while ()
    {
        my ($str) = $_;
        chomp ($str);
        $str = &trim($str);

        unless(($str eq "") || (substr($str, 0, 1) eq "#"))
        {
            my $key = substr($str, 0, index($str, $delimiter));
            my $value = substr($str, index($str, $delimiter) + 1);
           
            if ($key eq $parameter_name)
            {
                $value = $parameter_value;
                $found_flag = 1;
            }
            $str = join($delimiter, $key, $value);
        }
        $new_str .= "$str\n";
    }
    close (FI);

    if ($found_flag == 0)
    {
        my $str = join($delimiter, $parameter_name, $parameter_value);
        $new_str .= "$str\n";
    }

    open (FO, ">$file") or do { return -1 };
    print FO $new_str;
    close(FO);
}

Reading complete property file and return hash

# Function Name    : readInputFile
# Input                    : 1. Input file name
#                              2. Delimiter.  if not specified then = will be used
# Output                : None
# Description            : This subroutine will parse input file having key-value pair and create hash.  # as first character in file will be ignored
sub readInputFile($$)
{
    my ($file, $delim) = @_;
    $file = &convertSlash(&trim($file));
    $delim = &trim($delim);

    my %list;

    if ($delim eq "")
    {
        $delim = "=";
    }

    open (FI, $file);
    while()
    {
        my $line = $_;
        chomp $line;
        $line = &trim($line);

        unless ((substr($line, 0, 1) eq "#")  || ($line eq ""))
        {
            my ($key, $value) = split($delim, $line);
            $key = &trim($key);
            $value = &trim($value);

            if (exists $list{$key})
            {
                $value =  $list{$key}."::$value";
            }

            $list{$key} = $value;
        }
    }
    close (FI);
    return %list;
}

Getting value of a parameter from a property file

# Function Name    : parseDataFile
# Input            : filename with path and parameter whose value is required
# Output        : value of the the parameter from file
# Description    : This funtion will parse given file and return the value of the parameter
sub parseDataFile($$)
{
    my ($file, $parameter) = @_;
    $file = &convertSlash($file);
    open (FI, $file) or die "\nError :: :: Could not open file-${file}!!!\n";;

    while ()
    {
        my ($str) = $_;
        chomp ($str);
        $str = &trim($str);

        if ((substr($str, 0, 1) eq "#") || ($str eq ""))
        { # do nothing.  This means this line is commented or empty.
        }
        else
        {
            my $key = substr($str, 0, index($str, "="));
            my $value = substr($str, index($str, "=") + 1);
           
            if ($key eq $parameter)
            {
                return &trim($value);
            }
        }
    }
    close (FI);
    return "";
}