-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidrac-kvm.rb
executable file
·265 lines (217 loc) · 6.6 KB
/
idrac-kvm.rb
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env ruby
# From https://github.com/jasongill/idrac-kvm
# Jason Gill <[email protected]>
# Get the required gems with:
# sudo gem install rest-client net-ssh-gateway slop
require 'rubygems'
require 'rest_client'
require 'net/ssh/gateway'
require 'slop'
def cyantext(string)
puts "\033[0;36m -- " + string + "\033[0m"
end
def redtext(string)
puts "\033[0;31m !! " + string + "\033[0m"
end
begin
opts = Slop.parse do
banner "Usage: #{$0} [options]"
on :h, :help, 'Print this help message', :tail => true do
puts help
exit
end
on :b, :bounce, "Bounce server (optional)", :required => false, :optional => false
on :l, :login, "Your username on bounce server (optional; defaults to #{ENV['USER']})", :default => ENV['USER'], :required => false, :optional => false
on :s, :server, "Remote server IP (required)", :required => true, :optional => false
on :u, :user, "Remote username (optional; defaults to root)", :default => "root", :required => false
on :p, :password, "Remote password (required)", :required => true, :optional => false
end
bounceServer, bouncePort = opts[:bounce].split(':')
bounceUser = opts[:login]
remoteIP = opts[:server]
remoteUser = opts[:user]
remotePassword = opts[:password]
serverPortHTTPS = 443
serverPortVNC = 5900
serverDomain = remoteIP
if RUBY_PLATFORM.downcase.include?("linux")
cyantext "Building Linux keycode hack"
keycodec = Tempfile.new('keycodehack.c')
keycodec.write(DATA.read)
keycodec.close
keycodeso = Tempfile.new('keycodehack.so')
keycodeso.close
system('gcc', "-o", keycodeso.path, "-xc", keycodec.path, "-shared", "-s", "-ldl", "-fPIC")
ENV['LD_PRELOAD'] = keycodeso.path
end
if bounceServer && bounceUser
serverPortHTTPS = 1443
serverPortVNC = 15900
serverDomain = "localhost"
cyantext "Creating SSH tunnel via #{bounceUser}@#{bounceServer}:#{bouncePort} for ports 443 and 5900"
gateway = Net::SSH::Gateway.new(bounceServer, bounceUser, :port => bouncePort)
gateway.open(remoteIP, 443, 1443)
gateway.open(remoteIP, 5900, 15900)
end
serverURL = "https://#{serverDomain}:#{serverPortHTTPS}"
cyantext "Connecting to #{serverURL} and generating a session ID"
loginsession = RestClient.post(
serverURL + '/data/login',
{:user => remoteUser, :password => remotePassword}
)
cookie = loginsession.cookies["_appwebSessionId_"].to_s
cyantext "Logging in to #{serverURL} with session ID #{cookie}"
redirectsession = RestClient.get(
serverURL + '/index.html',
{:cookies => {:_appwebSessionId_ => cookie}}
)
jnlpfile = Tempfile.new('idrac.jnlp')
cyantext "Receiving KVM viewer JNLP file and writing to #{jnlpfile.path}"
viewersession = RestClient.get(
serverURL + '/viewer.jnlp(localhost@0@' + remoteIP + '@' + Time.now.to_i.to_s + ')',
{:cookies => {:_appwebSessionId_ => cookie}}
)
sessionfiledata = viewersession.to_s
sessionfiledata.gsub!(/port=5900/, "port=#{serverPortVNC}")
sessionfiledata.gsub!(/#{serverDomain}:443/, "#{serverDomain}:#{serverPortHTTPS}")
jnlpfile.write(sessionfiledata)
jnlpfile.close
cyantext "Starting Java viewer with tempfile #{jnlpfile.path}"
system("javaws", "-wait", jnlpfile.path)
rescue Errno::ECONNREFUSED => error
redtext "Error when attempting to open SSH tunnel: #{error.to_s}"
redtext "Did you verify that you are able to make a key-based connection to the bounce server?"
exit 2
rescue => error
redtext "Error: #{error.to_s}"
redtext "Try #{$0} --help for more information"
exit 1
ensure
unless cookie.nil?
cyantext "Logging out of session #{cookie} to prevent future login errors"
RestClient.get(
serverURL + '/data/logout',
{:cookies => {:_appwebSessionId_ => cookie}}
)
end
unless gateway.nil?
cyantext "Stopping SSH tunnel connection via #{bounceServer}"
gateway.shutdown!
end
keycodec.unlink unless keycodec.nil?
keycodeso.unlink unless keycodeso.nil?
jnlpfile.unlink unless jnlpfile.nil?
end
__END__
/*
* Shared library hack to translate evdev keycodes to old style keycodes.
*/
#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
static int (*real_XNextEvent)(Display *, XEvent *) = NULL;
static KeyCode (*real_XKeysymToKeycode)(Display *, KeySym) = NULL;
static KeySym (*sym_XKeycodeToKeysym)(Display *, KeyCode, int) = NULL;
static int hack_initialised = 0;
#define DEBUG 0
static int enabled = 1;
#ifdef DEBUG
static FILE *fd = NULL;
#endif
static void
hack_init(void)
{
void *h;
h = dlopen("libX11.so", RTLD_LAZY);
if (h == NULL) {
fprintf(stderr, "Unable to open libX11\n");
_exit(1);
}
real_XNextEvent = dlsym(h, "XNextEvent");
if (real_XNextEvent == NULL) {
fprintf(stderr, "Unable to find symbol\n");
_exit(1);
}
real_XKeysymToKeycode = dlsym(h, "XKeysymToKeycode");
if (real_XKeysymToKeycode == NULL) {
fprintf(stderr, "Unable to find symbol\n");
_exit(1);
}
sym_XKeycodeToKeysym = dlsym(h, "XKeycodeToKeysym");
if (sym_XKeycodeToKeysym == NULL) {
fprintf(stderr, "Unable to find symbol\n");
_exit(1);
}
#ifdef DEBUG
if (fd == NULL) {
fd = fopen("/tmp/keycode-log", "a");
if (fd == NULL)
fprintf(stderr, "Unable to open key-log\n");
}
#endif
hack_initialised = 1;
}
int
XNextEvent(Display *display, XEvent *event)
{
int r;
int keycode_new;
if (!hack_initialised)
hack_init();
r = real_XNextEvent(display, event);
if (event->type == KeyPress || event->type == KeyRelease) {
XKeyEvent *keyevent;
KeySym keysym;
keyevent = (XKeyEvent *)event;
#ifdef DEBUG
fprintf(fd, "KeyEvent: %d\n", keyevent->keycode);
fflush(fd);
#endif
keysym = sym_XKeycodeToKeysym(display, keyevent->keycode, 0);
keycode_new = keyevent->keycode;
switch (keysym) {
case XK_Up:
keycode_new = 98;
break;
case XK_Down:
keycode_new = 104;
break;
case XK_Left:
keycode_new = 100;
break;
case XK_Right:
keycode_new = 102;
break;
case XK_Print:
keycode_new = 111;
break;
case XK_Num_Lock:
if (event->type == KeyPress) {
enabled = ( enabled == 1 ? 0 : 1 );
#ifdef DEBUG
fprintf(fd, "Toggle enabled to %d\n", enabled);
fflush(fd);
#endif
}
break;
}
if (enabled == 1)
keyevent->keycode = keycode_new;
}
return r;
}
#ifdef DEBUG
KeyCode
XKeysymToKeycode(Display *display, KeySym keysym)
{
KeyCode keycode;
if (!hack_initialised)
hack_init();
keycode = real_XKeysymToKeycode(display, keysym);
fprintf(fd, "XKeysymToKeycode: %d\n", keycode);
fflush(fd);
return keycode;
}
#endif