#!/usr/bin/perl # # putty-to-midpssh.pl - Copyright (C) Mike Cathey # # v0.1 - 05/15/2008 - initial hack # # This script attempts to convert an exported a Registry export of # putty sessions into a format that MidpSSH can use. # # Use this at your own risk! # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # my $lines = ""; my $record = ""; my $protocol = ""; my $port = ""; my $username = ""; my $host = ""; my $sessionName = ""; my $file = $ARGV[0]; if ( $file eq "" || !-e $file || !-f $file || !-r $file ) { print "\nUsage: $0 putty_registry_export\n"; exit; } # so registry exports are in UTF-16, btw open ( PUTTY, '<:encoding(utf16)', $file) or die("\nERROR! Can't open $file: $!\n"); while ( ) { chomp; # the start of a new entry # [HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\YOURSERVER] if ( $_ =~ /^\[.*\\Sessions\\(.*)\]/ ) { $sessionName = $1; } # port - it's in hex, so we convert it # "PortNumber"=dword:00000017 if ( $_ =~ /PortNumber.*\:([0-9a-f]+)/i ) { $port = hex($1); } # protocol # "Protocol"="telnet" if ( $_ =~ /"Protocol"="(.*)"/i ) { # lowercase the protocol just in case $protocol = lc($1); } # username # "UserName"="johndoe" if ( $_ =~ /"UserName"="(.*)"/i ) { $username = $1; } # host/ip # "HostName"="172.31.65.1" if ( $_ =~ /"HostName"="(.*)"/i ) { # lowercase the protocol just in case $host = $1; } # if it's the end of a record, we add the record to $lines if ( $_ =~ /^ / ) { $record = ""; if ($protocol eq "ssh" ) { $record = "ssh "; if ($username ne "") { $record .= $username . "@"; } $record .= $host; if ($port != 22) { $record .= ":" . $port; } $record .= " " . $sessionName; } elsif ( $protocol eq "telnet" ) { $record = "telnet "; $record .= $host; if ($port != 23) { $record .= ":" . $port; } $record .= " " . $sessionName; } # convert %20 to lines $record =~ s/%20/ /g; # add it to the list $lines .= $record . "\n"; # reset variables $sessionName = ""; $username = ""; $protocol = ""; $port = ""; $host = ""; } } close PUTTY; print $lines;