-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrwin2gnomeTerm.pl
executable file
·214 lines (186 loc) · 7.09 KB
/
rwin2gnomeTerm.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/perl
##
## FILE: rwin.pl
##
## PURPOSE: Open a remote window to the named machine using a gnome-terminal.
## Uses gconftool-2 to set foreground text and window background colors
## so that ssh sessions are color coded based on remote destination.
##
## USAGE: rwin.pl -r
## {Set up gnome-terminal profiles, only needed one time after editing the
## %machines list in this file.}
##
## rwin.pl <machine name>
## {Open a new gnome-terminal and ssh to that system}
##
## rwin.pl
## {Present a menu of systems to log into. '0' or CTRL-C exits.}
##
## This script is able to configure existing gnome-terminal color profiles,
## but does not create them. This script will ignore the Default profile.
## Manually create as many additional profiles as there are entries in the
## %machines configuration list.
##
## AUTHOR: Ken Tanaka
## /usr/bin/showrgb | less can help you choose colors.
## Or use https://www.allscoop.com/tools/Web-Colors/web-colors.php
## Commas can be inserted into colors for readability: #ff,e8,e8
## Color examples:
## '#bfe'
## '#bbfef8'
## '#bb,fe,f8'
## 'black'
## 'green2'
## 'misty rose'
## 'MistyRose'
## '#bbbbffffffff'
## '#bbbb,ffff,ffff'
## AKA: Also-Known-As field is just a comment displayed in menu mode.
## You can include an IP address if you want to visually compare to the 'host'
## command output for security or password purposes.
%machines = (
##'example' => { ACCESS => 'example.ngdc.noaa.gov',
## AKA => 'example format, 140.172.180.11',
## COLORS => ['foreground text', 'window background'] },
'lion' => { ACCESS => 'lion',
AKA => '"cron server", 140.172.180.54',
COLORS => ['red4', '#fffff0'] },
'tabby' => { ACCESS => 'tabby',
AKA => 'local workstation',
COLORS => ['black', '#eeddcc'] },
);
$UseTestList = 0;
if ($UseTestList) {
%machines = (
'test' => { ACCESS => 'test',
AKA => 'test uncommented',
COLORS => ['blue1', '#bb,ee,ff'] },
);
}
$Reconfigure = 0;
$Debugging = 0;
if ($ARGV[0] eq '-r') {
$Reconfigure = 1;
print "Reconfigure mode ($ARGV[0])\n";
shift @ARGV;
}
## Number of machines
$machineCount = keys %machines;
print "machineCount = $machineCount\n" if $Debugging;
$DefaultDomain = '.ngdc.noaa.gov';
$hostCmd = '/usr/bin/host';
$sshCmd = '/usr/bin/ssh -Y';
#$Geom = '-geom 80x40+20+20';
$Geom = '--geometry 140x40+20+20';
#$Font = '-font "-adobe-courier-medium-r-*-*-18-*-*-*-*-*-iso8859-1"';
$gconftool = '/usr/bin/gconftool-2';
$gtermCmd = qq'/usr/bin/gnome-terminal $Geom --title="NAME" --tab-with-profile-internal-id="PROFILE" --command="$sshCmd ACCESS" &';
## Get gnome-terminal profile list
## Leave 'Default' alone, use the rest of the Profile0...ProfileN for gnome terminal
## customization to match the machines hash.
$gconftoolCmd = "$gconftool --get /apps/gnome-terminal/global/profile_list";
print "system($gconftoolCmd)\n" if $Debugging;
chomp($profiles = `$gconftoolCmd`);
print "profiles=$profiles\n" if $Debugging;
@profileList = [];
if ($profiles =~ /^\[(.*)\]$/) {
@profileList = sort split(/,/, $1);
shift @profileList; ## drop 'Default' off the beginning
} else {
print "profiles=$profiles\n";
die 'profiles not in expected format of: "[Default,Profile0,Profile1,...ProfileN]"';
}
## Exit if there are not enough Profile# slots
print "profileList=" . join(':', @profileList) . "\n" if $Debugging;
$profileCount = @profileList;
print "profileCount=$profileCount\n" if $Debugging;
if ($profileCount < $machineCount) {
die "There are not enough profile slots. Create profiles for $machineCount terminals";
}
## Set up gnome-terminal parameters: visible_name, foreground_color, background_color
my $gtProfileId;
my @machList = (sort keys %machines);
$i = 0;
foreach $m (@machList) {
$gtProfileId = "Profile$i";
$machines{$m}{PROFILE} = $gtProfileId;
## Set up gnome-terminal parameters:
## visible_name, foreground_color, background_color
if ($Reconfigure) {
print '=' x 22 . " Configuring '$m' in profile $gtProfileId\n" if $Debugging;
my $fgcolor = $machines{$m}{COLORS}[0];
print "fgcolor before: $fgcolor, " if $Debugging;
$fgcolor =~ s/,//g; ## strip out any commas
print "after: $fgcolor\n" if $Debugging;
my $bgcolor = $machines{$m}{COLORS}[1];
$bgcolor =~ s/,//g; ## strip out any commas
$gconftoolCmd = "$gconftool"
. qq' --set "/apps/gnome-terminal/profiles/$gtProfileId/visible_name"'
. qq' --type string "$m"';
print "gconftoolCmd = $gconftoolCmd\n" if $Debugging;
system($gconftoolCmd);
$gconftoolCmd = "$gconftool"
. qq' --set "/apps/gnome-terminal/profiles/$gtProfileId/foreground_color"'
. qq' --type string "$fgcolor"';
print "gconftoolCmd = $gconftoolCmd\n" if $Debugging;
system($gconftoolCmd);
$gconftoolCmd = "$gconftool"
. qq' --set "/apps/gnome-terminal/profiles/$gtProfileId/background_color"'
. qq' --type string "$bgcolor"';
print '-' x 22 if $Debugging;
print "\ngconftoolCmd = $gconftoolCmd\n" if $Debugging;
system($gconftoolCmd);
}
$i++;
}
if ($Reconfigure) {
## Set the name of unused profiles to "..unused #.." so previous names aren't
## left behind if the configured list gets shorter. This avoids duplicate names
## that can be confusing.
if ($i < $profileCount) {
for (; $i < $profileCount; $i++) {
$gtProfileId = "Profile$i";
$gconftoolCmd = "$gconftool"
. qq' --set "/apps/gnome-terminal/profiles/$gtProfileId/visible_name"'
. qq' --type string "..unused $i.."';
print "gconftoolCmd = $gconftoolCmd\n" if $Debugging;
system($gconftoolCmd);
}
}
}
print "gtermCmd = $gtermCmd\n" if $Debugging;
if (scalar(@ARGV) and not exists($machines{$ARGV[0]})) {
print "Unknown system $ARGV[0]. Choose a number\n";
}
if (not scalar(@ARGV) or not exists($machines{$ARGV[0]})) {
$i = 1;
#my @machList = (sort keys %machines);
foreach $m (@machList) {
printf "%2d: %-15s ", $i, $m;
if (defined $machines{$m}{AKA}) {
print "($machines{$m}{AKA})\n";
} else {
print "\n";
}
$i++;
}
print "Choose a system #: ";
$choice = <STDIN>;
exit if ($choice < 1 or $choice > $i);
$choice = $machList[$choice - 1];
} else {
$choice = shift;
}
$lookupHost = $machines{$choice}{ACCESS};
$lookupHost =~ /.*@(\w+)/ and do {
$lookupHost = $1;
};
if ($lookupHost !~ /.(gov|edu|net|org|mil|com)$/) {
$lookupHost .= $DefaultDomain;
}
print `$hostCmd $lookupHost`;
$gtermCmd =~ s/NAME/$choice/;
$gtermCmd =~ s/PROFILE/$machines{$choice}{PROFILE}/;
$gtermCmd =~ s/ACCESS/$machines{$choice}{ACCESS}/;
print "system($gtermCmd)\n" if $Debugging;
system($gtermCmd);