DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Create Shortcuts To PuTTY Sessions...

03.09.2010
| 4098 views |
  • submit to reddit
        I developed this under Linux, but have adapted it to work with Strawberry Perl.

I make a folder:

%USERPROFILE%\Start Menu\Programs\sessions

And run this in there.

#!/usr/bin/perl
#
# Create a Windows shortcut to each session stored in PuTTY
#
use warnings;
use strict;

use Win32::TieRegistry( Delimiter => '/' );
my $sessions_key = "HKEY_CURRENT_USER/Software/SimonTatham/PuTTY/Sessions";

my $sessions = $Registry->{$sessions_key}
    or die "$0: can't open $sessions_key: $^E\n";

for my $session ( sort keys %$sessions ) {

    $session =~ s{/$}{};
    $session =~ s{%20}{ }g;

    my $file;
    if ( $session =~ /^prod / ) {
        mkdir "prod" if ! -d "prod";
        $file = "prod/$session.lnk";
    }
    elsif ( $session =~ /^dev / ) {
        mkdir "dev" if ! -d "dev";
        $file = "dev/$session.lnk";
    }
    elsif ( $session =~ /^uat / ) {
        mkdir "uat" if ! -d "uat";
        $file = "uat/$session.lnk";
    }
    else {
        mkdir "other" if ! -d "other";
        $file = "other/$session.lnk";
    }

    open (my $file_fh, '>', $file) || die "Can't write to '$file': $!\n";

    for my $byte (
      0x4c, 0x00, 0x00, 0x00, 0x01, 0x14, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
      0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0xbb, 0x00, 0x00, 0x00,
      0x20, 0x00, 0x00, 0x00, 0x00, 0xdf, 0xfc, 0x5d, 0x85, 0x9b, 0xcb, 0x01,
      0xd3, 0x91, 0x47, 0x30, 0x90, 0xef, 0xcb, 0x01, 0x00, 0xdf, 0xfc, 0x5d,
      0x85, 0x9b, 0xcb, 0x01, 0x00, 0xf0, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0xf1, 0x00, 0x14, 0x00, 0x1f, 0x50, 0xe0, 0x4f,
      0xd0, 0x20, 0xea, 0x3a, 0x69, 0x10, 0xa2, 0xd8, 0x08, 0x00, 0x2b, 0x30,
      0x30, 0x9d, 0x19, 0x00, 0x2f, 0x43, 0x3a, 0x5c, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x4a, 0x00, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x72,
      0x3e, 0x97, 0x83, 0x11, 0x00, 0x50, 0x52, 0x4f, 0x47, 0x52, 0x41, 0x7e,
      0x31, 0x00, 0x00, 0x32, 0x00, 0x03, 0x00, 0x04, 0x00, 0xef, 0xbe, 0xee,
      0x3a, 0x8f, 0x74, 0x7f, 0x3e, 0xe0, 0x51, 0x14, 0x00, 0x00, 0x00, 0x50,
      0x00, 0x72, 0x00, 0x6f, 0x00, 0x67, 0x00, 0x72, 0x00, 0x61, 0x00, 0x6d,
      0x00, 0x20, 0x00, 0x46, 0x00, 0x69, 0x00, 0x6c, 0x00, 0x65, 0x00, 0x73,
      0x00, 0x00, 0x00, 0x18, 0x00, 0x36, 0x00, 0x31, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x7f, 0x3e, 0xa2, 0x55, 0x10, 0x00, 0x50, 0x75, 0x54, 0x54, 0x59,
      0x00, 0x22, 0x00, 0x03, 0x00, 0x04, 0x00, 0xef, 0xbe, 0x8d, 0x3d, 0xa5,
      0x98, 0x7f, 0x3e, 0x23, 0x56, 0x14, 0x00, 0x00, 0x00, 0x50, 0x00, 0x75,
      0x00, 0x54, 0x00, 0x54, 0x00, 0x59, 0x00, 0x00, 0x00, 0x14, 0x00, 0x42,
      0x00, 0x32, 0x00, 0x00, 0xf0, 0x06, 0x00, 0x8e, 0x3d, 0x8b, 0x5e, 0x20,
      0x00, 0x50, 0x55, 0x54, 0x54, 0x59, 0x2e, 0x45, 0x58, 0x45, 0x00, 0x2a,
      0x00, 0x03, 0x00, 0x04, 0x00, 0xef, 0xbe, 0x8e, 0x3d, 0x8b, 0x5e, 0x7f,
      0x3e, 0xa9, 0x56, 0x14, 0x00, 0x00, 0x00, 0x50, 0x00, 0x55, 0x00, 0x54,
      0x00, 0x54, 0x00, 0x59, 0x00, 0x2e, 0x00, 0x45, 0x00, 0x58, 0x00, 0x45,
      0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00, 0x1c,
      0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x33,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00, 0x17,
      0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x28, 0x1c, 0xc5, 0xf2, 0x10,
      0x00, 0x00, 0x00, 0x53, 0x59, 0x53, 0x54, 0x45, 0x4d, 0x00, 0x43, 0x3a,
      0x5c, 0x50, 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x20, 0x46, 0x69, 0x6c,
      0x65, 0x73, 0x5c, 0x50, 0x75, 0x54, 0x54, 0x59, 0x5c, 0x50, 0x55, 0x54,
      0x54, 0x59, 0x2e, 0x45, 0x58, 0x45, 0x00, 0x00, 0x20, 0x00, 0x2e, 0x00,
      0x2e, 0x00, 0x5c, 0x00, 0x50, 0x00, 0x72, 0x00, 0x6f, 0x00, 0x67, 0x00,
      0x72, 0x00, 0x61, 0x00, 0x6d, 0x00, 0x20, 0x00, 0x46, 0x00, 0x69, 0x00,
      0x6c, 0x00, 0x65, 0x00, 0x73, 0x00, 0x5c, 0x00, 0x50, 0x00, 0x75, 0x00,
      0x54, 0x00, 0x54, 0x00, 0x59, 0x00, 0x5c, 0x00, 0x50, 0x00, 0x55, 0x00,
      0x54, 0x00, 0x54, 0x00, 0x59, 0x00, 0x2e, 0x00, 0x45, 0x00, 0x58, 0x00,
      0x45, 0x00, 0x16, 0x00, 0x43, 0x00, 0x3a, 0x00, 0x5c, 0x00, 0x50, 0x00,
      0x72, 0x00, 0x6f, 0x00, 0x67, 0x00, 0x72, 0x00, 0x61, 0x00, 0x6d, 0x00,
      0x20, 0x00, 0x46, 0x00, 0x69, 0x00, 0x6c, 0x00, 0x65, 0x00, 0x73, 0x00,
      0x5c, 0x00, 0x50, 0x00, 0x75, 0x00, 0x54, 0x00, 0x54, 0x00, 0x59, 0x00,
      0x1d, 0x00, 0x20, 0x00, 0x2d, 0x00, 0x6c, 0x00, 0x6f, 0x00, 0x61, 0x00,
      0x64, 0x00, 0x20, 0x00, 0x22, 0x00
    ) {
        printf $file_fh "%c", $byte;
    }

    my @string = split(//, $session);

    print $file_fh join "\0", @string;

    for my $byte ( 0x00, 0x22, 0x00, 0x00 ) {
        printf $file_fh "%c", $byte;
    }

    close $file_fh;

}

Running it under Linux I exported the registry keys with:

regedit.exe /e sessions.reg HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions 

then used this as the start of the program:

my $file = 'sessions.reg';

my @sessions;
open (my $file_fh, $file) || die "Can't read '$file': $!\n";
while (defined (my $line = <$file_fh>)) {

    #
    next if $line !~ /\[/;
    $line =~ s/\0//g;
    $line =~ s/%20/ /g;
    $line =~ s{/}{-}g;
    # print $line;
    if ( $line =~ /\[HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions\\(.*)\]/ ) {
        push @sessions, $1;
    }

}
close $file_fh;

for my $session ( @sessions ) {