forked from raguks/pwin2lin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwin2lin.pl
120 lines (103 loc) · 3.07 KB
/
pwin2lin.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/perl -w
# Dan Witt
# 5/28/2010
# Convert Windows PuTTY sessions to Linux
# Ragu only edited Dan Witt's script to work on KiTTY session registry
#How to use pwin2lin.pl
sub help {
print<<HELP
Export the PuTTY registry settings from Windows:
Start->Run->regedit
HKEY_CURRENT_USER\\Software\\9bis.com\\
Right click PuTTY and choose 'Export'. Save the registry file to your Linux machine and run this script from a shell (chmod 755 first):
./pwin2lin.pl
Examples:
Running the script alone:
foo\@bar:~\$ ./pwin2lin.pl
Specify files from the command line:
foo\@bar:~\$ ./pwin2lin.pl myPuttySessions.reg /home/foo/.putty
HELP
}
# Includes
use Encode;
use File::Path;
use strict;
# Globals
my $winRegFile = "";
my $puttyPath = "";
if($#ARGV + 1) { # If any command line arguments are specified try to parse
if($ARGV[0] eq "--help") {
&help();
exit;
} elsif($ARGV[0] && $ARGV[1]) {
$winRegFile = $ARGV[0];
chomp $winRegFile;
$puttyPath = $ARGV[1];
chomp $puttyPath;
} else {
print "Usage:\n ./pwin2lin.pl [Windows Reg File] [Path to save PuTTY sessions]\n ./pwin2lin.pl --help\n";
exit;
}
} else { # Ask them where the registry file is and where they'd like to store the converted sessions.
print "Specify the path and file to convert:\n";
$winRegFile = <STDIN>;
chomp $winRegFile;
print "Specify the path to store the converted sessions:\n";
$puttyPath = <STDIN>;
chomp $puttyPath;
$puttyPath = "./" if !$puttyPath;
}
# Open the file and convert it from UTF-16LE
open(FILE, "<:encoding(UTF-16LE):crlf", $winRegFile) or die $!;
my @lines = <FILE>;
close FILE;
mkpath "$puttyPath/sessions", 0740 unless -d "$puttyPath/sessions";
my $linesLen = scalar @lines;
# Parse the registry file, try to guess some settings
my $i = 0;
while($i < $linesLen) {
chomp $lines[$i];
if($lines[$i] =~
m/^\[HKEY_CURRENT_USER\\Software\\9bis.com\\KiTTY\\Sessions\\(.+)\]/)
{
my $hostname = $1;
$i++;
next if $hostname =~ m/^Default.+Settings$/; # Use Linux Defaults
#print "$hostname\n";
open HOST, ">$puttyPath/sessions/$hostname" or die $!; # Write out the session
while(1) {
chomp $lines[$i];
last if $lines[$i] eq "";
$lines[$i] =~ s/\"//g;
if($lines[$i] =~ m/dword:(.+)/) {
my $dec = $1;
if($dec eq "ffffffff") {
$dec = -1;
} else {
$dec = hex($dec);
}
$lines[$i] =~ s/dword:.+/$dec/g;
}
$lines[$i] =~ s/^Font\=.+/FontName=server:fixed/g;
$lines[$i] =~ s/^NoRemoteQTitle\=(.+)/RemoteQTitleAction\=$1/g;
$lines[$i] =~ s/^SerialLine\=.+/SerialLine\=\/dev\/ttyS0/g;
$lines[$i] =~ s/^FontVTMode\=.+/FontVTMode\=4/g;
#print "$lines[$i]\n";
print HOST "$lines[$i]\n";
$i++;
}
close HOST;
} elsif($lines[$i] =~
m/\[HKEY_CURRENT_USER\\Software\\9bis.com\\KiTTY\\SshHostKeys\]/) {
$i++;
open SSH, ">$puttyPath/sshhostkeys" or die $!;
while($i < $linesLen) {
chomp($lines[$i]);
$lines[$i] =~ s/\"//g;
print SSH "$lines[$i]\n";
$i++;
}
close SSH;
}
$i++;
}