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
/
dot-ssh-config-socks.pl
166 lines (141 loc) · 3.35 KB
/
dot-ssh-config-socks.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
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/perl -w
#
# Generate SSH config files with tunnels support
# This version makes use of dynamic (socks) forwards.
# If you find that you have trouble with any tools that use
# your ssh config files (eg. programming libraries such
# as fabric) then try the static ports version of this program.
#
# This program relies on netcat being available at /bin/nc
# on any jump hosts you will establish tunnels through.
#
# Alexander Else. aelse@github
use Data::Dumper;
my $host_tree = {};
my @parent = ($host_tree);
my $indent = 0;
my $line = 0;
my %global_port_register;
my $tunport = 20000;
my $socksport = 1081;
my $socks_stack = ();
sub add_host(@)
{
my ($host, $addr, $extra, $socks_listen) = @_;
my $attrs = {
Host => $host,
HostKeyAlias => $host,
HostName => $addr,
TCPKeepAlive => 'yes',
ServerAliveInterval => '180',
ServerAliveCountMax => '2',
User => $ENV{'USER'},
};
if ($socks_listen)
{
$attrs->{'DynamicForward'} = $socks_listen;
}
if ($#socks_stack != -1)
{
$attrs->{'ProxyCommand'} =
"/bin/nc -x localhost:" . $socks_stack[-1] . " %h %p";
}
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};
}
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 ||= '';
if (/\{$/)
{
add_host($host, $addr, $extra, $socksport);
push(@socks_stack, $socksport);
$socksport++;
unshift(@parent, $parent[0]->{$host});
$indent++;
}
else
{
add_host($host, $addr, $extra, 0);
}
}
if (/\}$/)
{
shift(@parent);
$indent--;
die "line $line: neg indent?" if ($indent < 0);
pop(@socks_stack);
}
}
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";
}
}
print "\n";
map { dump_config($_) } (values %{$hash_ref});
}
foreach my $key (keys %{$host_tree})
{
dump_config($host_tree->{$key});
}