-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.pas
More file actions
286 lines (238 loc) · 7.27 KB
/
example.pas
File metadata and controls
286 lines (238 loc) · 7.27 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
unit Unit3;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Spin,
synautil, blcksock, libssh2, sockets;
type
{ Tformblackhole }
Tformblackhole = class(TForm)
Button1: TButton;
Button2: TButton;
editusername: TEdit;
editpassword: TEdit;
Editmac: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
SpinEditvlan: TSpinEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
function ConvertMacAddress(const MacAddress: string): string;
private
Session: PLIBSSH2_SESSION;
Socket: Integer;
Channel: PLIBSSH2_CHANNEL;
Commands: TStringList;
Response: string;
procedure ConnectSSH;
procedure DisconnectSSH;
procedure ExecuteCommands;
public
{ Public declarations }
end;
var
formblackhole: Tformblackhole;
implementation
{$R *.lfm}
procedure Tformblackhole.Button1Click(Sender: TObject);
var
MacAddress, VlanID: string;
begin
// Assign values to MacAddress and VlanID based on user input
MacAddress := ConvertMacAddress(Editmac.Text);
VlanID := IntToStr(SpinEditvlan.Value);
// Initialize the list of commands
Commands := TStringList.Create;
Commands.Add('system-view');
Commands.Add('undo mac-address blackhole ' + MacAddress + ' vlan ' + VlanID);
Commands.Add('quit');
Commands.Add('save');
Commands.Add('y');
Commands.Add('');
Commands.Add('y');
Commands.Add('');
try
// Connect to the SSH server
ConnectSSH;
// Execute commands
ExecuteCommands;
// Disconnect from the SSH server
DisconnectSSH;
except
on E: Exception do
ShowMessage('Error: ' + E.Message);
end;
end;
procedure Tformblackhole.Button2Click(Sender: TObject);
var
MacAddress, VlanID: string;
begin
// Assign values to MacAddress and VlanID based on user input
MacAddress := ConvertMacAddress(Editmac.Text);
VlanID := IntToStr(SpinEditvlan.Value);
// Initialize the list of commands
Commands := TStringList.Create;
Commands.Add('system-view');
Commands.Add('mac-address blackhole ' + MacAddress + ' vlan ' + VlanID);
Commands.Add('quit');
Commands.Add('save');
Commands.Add('y');
Commands.Add('');
Commands.Add('y');
Commands.Add('');
try
// Connect to the SSH server
ConnectSSH;
// Execute commands
ExecuteCommands;
// Disconnect from the SSH server
DisconnectSSH;
except
on E: Exception do
ShowMessage('Error: ' + E.Message);
end;
end;
procedure Tformblackhole.ConnectSSH;
var
SockAddr: TInetSockAddr;
HostAddr: TInAddr;
editusernametext, editpasswordtext: AnsiString;
HostNameStr: AnsiString;
hostname: AnsiString;
SocketHandle: TSocket;
begin
// Assign the values from the edit controls
editusernametext := AnsiString(editusername.Text);
editpasswordtext := AnsiString(editpassword.Text);
hostname := AnsiString('192.168.1.80');
// Initialize libssh2
if libssh2_init(0) <> 0 then
raise Exception.Create('Failed to initialize libssh2');
// Resolve the hostname
HostNameStr := AnsiString(hostname);
HostAddr := StrToNetAddr(HostNameStr);
if HostAddr.S_addr = longword(INADDR_NONE) then
raise Exception.Create('Failed to resolve hostname');
// Create socket and connect to the SSH server
SocketHandle := fpsocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if SocketHandle = INVALID_SOCKET then
raise Exception.Create('Failed to create socket');
SockAddr.sin_family := AF_INET;
SockAddr.sin_port := htons(22);
SockAddr.sin_addr := HostAddr;
if fpconnect(SocketHandle, @SockAddr, SizeOf(SockAddr)) <> 0 then
begin
fpshutdown(SocketHandle, SHUT_RDWR);
CloseSocket(SocketHandle);
raise Exception.Create('Failed to connect to the SSH server');
end;
// Create SSH session
Session := libssh2_session_init();
if not Assigned(Session) then
raise Exception.Create('Failed to create SSH session');
// Start SSH session
if libssh2_session_startup(Session, SocketHandle) <> 0 then
raise Exception.Create('Failed to start SSH session');
// Authenticate with editusername and editpassword
if libssh2_userauth_password(Session, PAnsiChar(editusernametext), PAnsiChar(editpasswordtext)) <> 0 then
raise Exception.Create('Failed to authenticate');
end;
procedure Tformblackhole.DisconnectSSH;
begin
// Disconnect and free the SSH session
libssh2_session_disconnect(Session, 'Disconnecting');
libssh2_session_free(Session);
// Close the socket
CloseSocket(Socket);
// Cleanup libssh2
libssh2_exit();
end;
procedure TFormBlackHole.ExecuteCommands;
var
I: Integer;
Command: AnsiString;
Buffer: array[0..4095] of AnsiChar;
BytesRead: Integer;
TimeoutMs: Integer;
StartTime: Cardinal;
begin
// Set the timeout value in milliseconds
TimeoutMs := 5000; // 5 seconds
// Open SSH channel
Channel := libssh2_channel_open_session(Session);
if Channel = nil then
raise Exception.Create('Failed to open SSH channel');
try
if libssh2_channel_request_pty(Channel, 'vanilla') < 0 then
begin
ShowMessage('Failed to PTY');
Exit;
end;
// Request a shell session
if libssh2_channel_shell(Channel) < 0 then
begin
ShowMessage('Failed to request shell session');
Exit;
end;
libssh2_session_set_blocking(session, 0);
// Execute commands
for I := 0 to Commands.Count - 1 do
begin
Command := AnsiString(Commands[I]);
// Send the command to the server
Command := Command + #13#10; // Add CRLF character sequence
sleep(2500);
if libssh2_channel_write(Channel, PAnsiChar(Command), Length(Command)) < 0 then
begin
ShowMessage('Failed to write command: ' + Command);
exit; // Skip to the next command if writing fails
end;
// Read the response from the server with timeout handling
{ Response := '';
StartTime := GetTickCount64;
repeat
BytesRead := libssh2_channel_read(Channel, @Buffer, SizeOf(Buffer));
if BytesRead > 0 then
begin
//SetString(Response, @Buffer, BytesRead);
//ShowMessage('Server Response: ' + Response);
sleep(2500);
end
else if BytesRead = LIBSSH2_ERROR_EAGAIN then
begin
// Wait for a short period before retrying the read operation
Sleep(100);
end
else if BytesRead < 0 then
begin
ShowMessage('Failed to read response for command: ' + Command);
Break; // Exit loop if read error occurs
end;
until (BytesRead <= 0) or (GetTickCount64 - StartTime > TimeoutMs);}
end;
showmessage('Blackhole Operation Complete');
finally
// Free the SSH channel
// libssh2_channel_free(Channel);
end;
end;
function TFormBlackHole.ConvertMacAddress(const MacAddress: string): string;
var
FormattedMac: string;
i: integer;
begin
// Remove hyphens and colons
FormattedMac := StringReplace(MacAddress, ':', '', [rfReplaceAll]);
FormattedMac := StringReplace(FormattedMac, '-', '', [rfReplaceAll]);
// Construct the formatted MAC address
Result := '';
for i := 1 to Length(FormattedMac) do
begin
Result := Result + FormattedMac[i];
if (i = 4) or (i = 8) then
Result := Result + '-';
end;
end;
end.