This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdot-ssh-config-static-ports.pl
156 lines (131 loc) · 3.13 KB
/
dot-ssh-config-static-ports.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/perl -w
#
# Generate SSH config files with tunnels support
# This version uses static port mappings in output config files
# and may play more nicely with some programming libraries that
# make SSH connections (eg. fabric).
#
# Alexander Else. aelse@github
use Data::Dumper;
my $host_tree = {};
my @parent = ($host_tree);
my $indent = 0;
my $line = 0;
my %global_port_register;
sub add_host(@)
{
my ($host, $addr, $extra) = @_;
my $attrs = {
Host => $host,
HostKeyAlias => $host,
HostName => $addr,
TCPKeepAlive => 'yes',
ServerAliveInterval => '180',
ServerAliveCountMax => '2',
User => $ENV{'USER'},
};
foreach (split(/\s+/, $extra))
{
my ($key, $val) = split(/=/, $_);
next unless (defined($val));
if ($key eq 'forward')
{
my @fields = split(/:/, $val);
$attrs->{forwards} ||= {};
if (scalar @fields == 3)
{
$attrs->{forwards}->{$fields[0]} = "$fields[1]:$fields[2]";
if ($global_port_register{$fields[0]})
{
die "line $line: port $fields[0] already allocated";
}
$global_port_register{$fields[0]}++;
}
else
{
# unique key that int() will return 0 for
$attrs->{forwards}->{"x$line"} = "$fields[0]:$fields[1]";
}
}
else
{
$attrs->{$key} = $val;
}
}
$parent[0]->{$host} = {attrs => $attrs};
}
my $tunport = 20000;
while (<>)
{
chomp;
$line++;
next if (/^\s*#/); # skip comments
s/\s*#.*//;
if (/startport\s*=\s*(\d+)/)
{
$tunport = $1;
}
elsif (/(\S+)\s+(\S+)\s*(\S.*)?/)
{
my ($host, $addr, $extra) = ($1, $2, $3);
$extra ||= '';
add_host($1, $2, $3);
if (/\{$/)
{
unshift(@parent, $parent[0]->{$host});
$indent++;
}
}
if (/\}$/)
{
shift(@parent);
$indent--;
die "line $line: neg indent?" if ($indent < 0);
}
}
sub dump_config($)
{
my ($hash_ref) = @_;
my $attrs = $hash_ref->{attrs};
my $forwards = $attrs->{forwards};
my $hostname = $attrs->{Host};
print "Host $hostname\n";
map { delete $hash_ref->{$_} } qw/attrs/;
map { delete $attrs->{$_} } qw/forwards Host/;
map { print " $_ $attrs->{$_}\n" } (sort keys %{$attrs});
if (defined($forwards))
{
foreach my $port (keys %{$forwards})
{
my $lport = int($port);
if (!$lport)
{
while ($global_port_register{$tunport})
{
$tunport++;
}
$lport ||= $tunport;
$tunport++;
}
print " LocalForward $lport $forwards->{$port}\n";
}
}
foreach my $host (sort keys %{$hash_ref})
{
my $child_attrs = $hash_ref->{$host}->{attrs};
$child_attrs->{Port} ||= 22;
print " # $child_attrs->{Host}\n";
print " LocalForward $tunport "
. $child_attrs->{HostName} . ":"
. $child_attrs->{Port} . "\n";
$child_attrs->{HostName} = 'localhost';
$child_attrs->{Port} = $tunport;
$tunport++;
}
print "\n";
map { dump_config($_) } (values %{$hash_ref});
}
foreach my $key (keys %{$host_tree})
{
dump_config($host_tree->{$key});
}