-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pas
823 lines (766 loc) · 21.7 KB
/
main.pas
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
// Copyright 2017, Kaj Mikkelsen
// This software is distributed under the GPL 3 license
// The full text of the license can be found in the aboutbox
// as well as in the file "Copying"
// Version History
// 0.01 Initial release
// 0.02 Error in Regular Expression happened for all records.
// Changed so filter is discarded on RegEx error
// 20170209
// Corrected anchors for filter panel/group box
// 20170211
// Colrorizing online items
// added option for scanning all intefaces
//
unit Main;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
ActnList, StdCtrls, DBGrids, ExtCtrls, Buttons, upreferences, mylib,
BufDataset, DB,
netdb, laz2_DOM
, laz2_XMLRead
, laz2_XMLUtils, Grids, IniPropStorage;
type
{ TMainForm }
TMainForm = class(TForm)
AClear: TAction;
AAbout: TAction;
AWriteAll: TAction;
AWriteFile_ports: TAction;
AGetHostNames: TAction;
AgroupEdit: TAction;
AGetOpenPorts: TAction;
AVersions: TAction;
AOS: TAction;
AGroups: TAction;
ASave: TAction;
AOpen: TAction;
BufDataset1: TBufDataset;
Button1: TButton;
Button2: TButton;
CB1: TComboBox;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Edit1: TEdit;
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Memo1: TMemo;
MenuItem10: TMenuItem;
MenuItem2: TMenuItem;
MenuItem3: TMenuItem;
MenuItem4: TMenuItem;
MenuItem5: TMenuItem;
MClear: TMenuItem;
MenuItem6: TMenuItem;
MenuItem7: TMenuItem;
MenuItem8: TMenuItem;
MenuItem9: TMenuItem;
MGetHost: TMenuItem;
MVersions: TMenuItem;
MOS: TMenuItem;
MGroups: TMenuItem;
OD1: TOpenDialog;
Panel1: TPanel;
Panel2: TPanel;
Panel3: TPanel;
Panel5: TPanel;
Panel6: TPanel;
Panel7: TPanel;
SD1: TSaveDialog;
Scan: TAction;
APreferences: TAction;
Quit: TAction;
AL1: TActionList;
MainMenu1: TMainMenu;
MenuItem1: TMenuItem;
MPreferences: TMenuItem;
MExit1: TMenuItem;
MSave: TMenuItem;
MLoad: TMenuItem;
MFiles: TMenuItem;
MEdit: TMenuItem;
MExit: TMenuItem;
SD2: TSaveDialog;
procedure AAboutExecute(Sender: TObject);
procedure AClearExecute(Sender: TObject);
procedure AGetHostNamesExecute(Sender: TObject);
procedure AGetOpenPortsExecute(Sender: TObject);
procedure AgroupEditExecute(Sender: TObject);
procedure AGroupsExecute(Sender: TObject);
procedure AOSExecute(Sender: TObject);
procedure ASaveExecute(Sender: TObject);
procedure AOpenExecute(Sender: TObject);
procedure APreferencesExecute(Sender: TObject);
procedure AVersionsExecute(Sender: TObject);
procedure AWriteAllExecute(Sender: TObject);
procedure AWriteFile_portsExecute(Sender: TObject);
procedure BufDataset1FilterRecord(DataSet: TDataSet; var Accept: boolean);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure DataSource1DataChange(Sender: TObject; Field: TField);
procedure DBGrid1ColumnSized(Sender: TObject);
procedure DBGrid1DblClick(Sender: TObject);
procedure DBGrid1PrepareCanvas(Sender: TObject; DataCol: integer;
Column: TColumn; AState: TGridDrawState);
procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure MenuItem10Click(Sender: TObject);
procedure MExitClick(Sender: TObject);
procedure QuitExecute(Sender: TObject);
procedure ScanExecute(Sender: TObject);
private
{ private declarations }
procedure DoTheXML(FlNm: string);
procedure LoadDataBase(FlNm: string);
procedure EmptyDatabase;
procedure ShowMyMessage(Message: string);
procedure DoScan(InterF: string);
function InterfaceIsUp(Interf: string): boolean;
public
OnlineFontColor, OnlineColor: TColor;
{ public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.lfm}
uses
Unix,UEdit, UAbout, UGroups, UGroupEdit, UWrite_Port, sockets, UMyShowMessage, RegExpr;
var
DataBaseChanged: boolean;
GlMAC: TStringList;
{ TMainForm }
function TMainForm.InterfaceIsUp(Interf: string): boolean;
var
fL: TextFile;
FlNm, St: string;
begin
FlNm := StringReplace('/sys/class/net/<Interface>/operstate', '<Interface>',Interf, []);
AssignFile(fl, Flnm);
Reset(fl);
Read(fl, St);
CloseFile(fl);
if St = 'up' then
Result := True
else
Result := False;
end;
procedure TMainForm.DoScan(InterF: string);
var
Str: TStringList;
Cmd, St: string;
i: integer;
begin
Cmd := GetStdIni('Commands', 'ScanCmd',
'arp-scan -I <Interface> -l -q | tail -n +3 | head -n -3');
// Cmd := 'arp-scan -I'+Interf+' -l -q | tail -n +3 | head -n -3';
Cmd := StringReplace(Cmd, '<Interface>', Interf, []);
ShowMyMessage('This may take a while');
Application.ProcessMessages;
Str := TStringList.Create;
// DoCommand('/bin/bash -c "sudo ls -l"',Str);
if DoCommand('/bin/bash -c "' + Cmd + '"', Str) = 0 then
begin
with BufDataSet1 do
begin
if not BufDataSet1.Active then
CreateDataSet;
if Str.Count < 4 then
ShowMessage('To few devices')
else
begin
for i := 0 to Str.Count - 1 do
begin
try
St := UniformMac(GetFieldByDelimiter(1, Str[i], #9), ':');
GlMAC.Add(St);
if not BufDataset1.Locate('Mac', St, []) then
begin
Append;
FieldByName('Mac').AsString :=
UniformMac(GetFieldByDelimiter(1, Str[i], #9), ':');
FieldByName('Hostname').AsString := '';
FieldByName('OS').AsString := '';
FieldByName('Groups').AsString := '';
FieldByName('SSH').AsBoolean := False;
FieldByName('HTTP').AsBoolean := False;
FieldByName('HTTPS').AsBoolean := False;
FieldByName('FTP').AsBoolean := False;
FieldByName('SNMP').AsBoolean := False;
FieldByName('Nagios').AsBoolean := False;
Memo1.Lines.Add(St);
end
else
Edit;
FieldByName('IP').AsString := GetFieldByDelimiter(0, Str[i], #9);
post;
except
Memo1.Lines.Add('Error in line: ' + Str[i]);
end;
Application.ProcessMessages;
end;
end;
end;
end
else
begin
Memo1.Lines.Add('Error in executing command ' + cmd);
end;
Str.Free;
end;
procedure TMainForm.ScanExecute(Sender: TObject);
var
SavedCursor: TCursor;
Interf, IfCmd: string;
InterList: TStringList;
i: integer;
Filtered: Boolean;
begin
GlMac.Clear;
Memo1.Lines.Clear;
Memo1.Lines.Add('New:');
InterList := TStringList.Create;
SavedCursor := Cursor;
Cursor := crHourGlass;
Filtered := BufDataSet1.Filtered;
If Filtered Then
BufDataSet1.Filtered := False;
Panel6.Caption := 'Scanning';
Memo1.Clear;
Interf := GetStdIni('Settings', 'IFcmd', 'eth0');
if Interf = 'All' then
begin
IfCmd := GetStdIni('Commands', 'GetIfCmd',
'ifconfig -a | grep Link | grep -v inet6| awk '' { print $1 } ''');
IFcmd := '/bin/bash -c "' + IfCmd + '"';
DoCommand(IFcmd, InterList);
end
else
InterList.Add(Interf);
for i := 0 to InterList.Count - 1 do
if InterfaceIsUp(InterList[i]) then
DoSCan(InterList[i])
Else
Memo1.Lines.Add('Interface '+InterList[i]+' is not active');
BufDataSet1.Filtered := Filtered;
InterList.Free;
Panel6.Caption := '';
Cursor := SavedCursor;
end;
procedure TMainForm.QuitExecute(Sender: TObject);
begin
if DataBaseChanged then
begin
if MessageDlg('Warning', 'Database not saved, do you really want to exit',
mtConfirmation, [mbYes, mbNo], 0) = mrYes then
Application.terminate;
end
else
Application.Terminate;
end;
procedure TMainForm.APreferencesExecute(Sender: TObject);
begin
FPref.ShowModal;
Panel5.Caption := 'Interface: ' + GetStdIni('Settings', 'IFcmd', 'eth0');
end;
procedure TMainForm.AVersionsExecute(Sender: TObject);
begin
FGroups.Typ := 'Versions';
FGroups.ShowModal;
end;
procedure TMainForm.AWriteAllExecute(Sender: TObject);
var
Fl: TextFile;
begin
SD2.InitialDir := GetStdIni('Settings', 'WriteSaveDir', '~/.Inventory');
SD2.FileName := GetStdIni('Settings', 'WriteSaveFile', '');
if SD2.Execute then
begin
PutStdIni('Settings', 'WriteSaveDir', SD2.InitialDir);
PutStdIni('Settings', 'WriteSaveFile', SD2.FileName);
try
AssignFile(fl, SD2.FileName);
Rewrite(Fl);
except
ShowMessage('Could not write to file' + SD2.FileName);
Exit;
end;
with BufDataset1 do
begin
First;
while not EOF do
begin
if FieldByName('Hostname').AsString <> '' then
WriteLn(fl, FieldByName('Hostname').AsString);
Next;
end;
end;
CloseFile(Fl);
end;
end;
procedure TMainForm.AWriteFile_portsExecute(Sender: TObject);
begin
FWrite_Port.ShowModal;
end;
procedure TMainForm.BufDataset1FilterRecord(DataSet: TDataSet; var Accept: boolean);
var
RegEx: TRegExpr;
St: string;
begin
RegEx := TRegExpr.Create;
try
try
RegEx.Expression := Edit1.Text;
St := BufDataSet1.FieldByName(CB1.Text).AsString;
if RegEx.Exec(St) then
Accept := True
else
Accept := False;
except
ShowMessage('Error in Regular Expression, filter discarded ');
BufDataSet1.Filtered := False;
end;
finally
RegEx.Free;
end;
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
BufDataSet1.Filtered := False;
BufDataSet1.Filtered := True;
end;
procedure TMainForm.Button2Click(Sender: TObject);
begin
BufDataSet1.Filtered := False;
Edit1.Text := '';
end;
procedure TMainForm.AOpenExecute(Sender: TObject);
var
Fl: TextFile;
begin
OD1.InitialDir := GetStdIni('Settings', 'SaveDir', '~/.Inventory');
OD1.FileName := GetStdIni('Settings', 'SaveFile', 'Inventory.csv');
if OD1.Execute then
begin
PutStdIni('Settings', 'SaveDir', OD1.InitialDir);
PutStdIni('Settings', 'SaveFile', OD1.FileName);
LoadDataBase(OD1.FileName);
end;
end;
procedure TMainForm.ASaveExecute(Sender: TObject);
var
Fl: TextFile;
begin
if BufDataSet1.Filtered then
begin
if MessageDlg('Clear filter before saving', mtConfirmation, [mbYes, mbNo], 0) =
mrYes then
begin
BufDataSet1.Filtered := False;
Edit1.Text := '';
end;
end;
Sd1.InitialDir := GetStdIni('Settings', 'SaveDir', '~/.Inventory');
SD1.FileName := GetStdIni('Settings', 'SaveFile', 'Inventory.csv');
if SD1.Execute then
begin
PutStdIni('Settings', 'SaveDir', SD1.InitialDir);
PutStdIni('Settings', 'SaveFile', SD1.FileName);
AssignFile(Fl, SD1.FileName);
ReWrite(fl);
with BufDataSet1 do
begin
First;
while not EOF do
begin
WriteLn(Fl,
FieldByName('Mac').AsString + ';' + FieldByName('IP').AsString +
';' + FieldByName('Hostname').AsString + ';' +
FieldByName('OS').AsString + ';' + FieldByName('Groups').AsString +
';' + FieldByName('SSH').AsString + ';' + FieldByName('HTTP').AsString +
';' + FieldByName('HTTPS').AsString + ';' + FieldByName('FTP').AsString +
';' + FieldByName('SNMP').AsString + ';' + FieldByName('Nagios').AsString);
Next;
end;
end;
CloseFile(Fl);
DataBaseChanged := False;
Memo1.Lines.Clear;
Panel1.Caption := 'Database unchanged';
end;
end;
procedure TMainForm.AGroupsExecute(Sender: TObject);
begin
FGroups.Typ := 'Group';
FGroups.ShowModal;
end;
procedure TMainForm.AGetOpenPortsExecute(Sender: TObject);
var
St, St1, Cmd: string;
Str1: TStringList;
Fl: TextFile;
SavedCursor: TCursor;
begin
SavedCursor := Cursor;
Cursor := crHourGlass;
Panel6.Caption := 'Scanning';
St := '/tmp/' + IntToStr(GetProcessId) + '_files.txt';
St1 := '/tmp/' + IntToStr(GetProcessId) + '_out.txt';
AssignFile(fl, St);
Rewrite(fl);
with BufDataSet1 do
begin
First;
while not EOF do
begin
WriteLn(Fl, FieldByName('IP').AsString);
Next;
Application.ProcessMessages;
end;
end;
CloseFile(Fl);
Cmd := GetSTdIni('Commands', 'NmapCmd',
'nmap -iL <FileName1> -p 80,20,22,443,161 -oX <FileName2>');
Cmd := StringReplace(Cmd, '<FileName1>', St, []);
Cmd := StringReplace(Cmd, '<FileName2>', St1, []);
ShowMyMessage('This may take a while');
Application.ProcessMessages;
Str1 := TStringList.Create;
if (DoCommand('/bin/bash -c "' + Cmd + '"', Str1) = 0) then
DoTheXML(St1)
else
ShowMessage('Could not execute ' + Cmd);
Str1.Free;
DeleteFile(St);
DeleteFile(st1);
Cursor := SavedCursor;
Panel6.Caption := '';
end;
procedure TMainForm.AgroupEditExecute(Sender: TObject);
begin
FGroupEdit.Groups := BufDataSet1.FieldByName('Groups').AsString;
if FGroupEdit.ShowModal = mrOk then
begin
BufDataSet1.FieldByName('Groups').AsString := FGroupEdit.Groups;
end;
end;
procedure TMainForm.AClearExecute(Sender: TObject);
begin
EmptyDataBase;
end;
procedure TMainForm.AAboutExecute(Sender: TObject);
begin
FAbout.ShowModal;
end;
procedure TMainForm.AGetHostNamesExecute(Sender: TObject);
var
H: THostEntry;
SavedCursor: TCursor;
begin
SavedCursor := Cursor;
Cursor := crHourGlass;
Panel6.Caption := 'Scanning';
with BufDataSet1 do
begin
First;
while not EOF do
begin
if ResolveHostByAddr(StrToHostAddr(FieldByName('IP').AsString), H) then
begin
Edit;
FieldByName('Hostname').AsString := H.Name;
Post;
end;
Application.ProcessMessages;
Next;
end;
end;
Panel6.Caption := '';
Cursor := SavedCursor;
end;
procedure TMainForm.AOSExecute(Sender: TObject);
begin
FGroups.Typ := 'OS';
FGroups.ShowModal;
end;
procedure TMainForm.DataSource1DataChange(Sender: TObject; Field: TField);
begin
DataBaseChanged := True;
Panel1.Caption := 'Database modified';
Panel2.Caption := 'Items: ' + IntToStr(BufDataSet1.RecordCount);
end;
procedure TMainForm.DBGrid1ColumnSized(Sender: TObject);
var
i: integer;
St: string;
begin
for i := 0 to DBGrid1.Columns.Count - 1 do
begin
if i < 10 then
St := 'Col0' + IntToStr(i)
else
St := 'Col' + IntToStr(i);
PutStdIni('Datagrid', st, IntToStr(DBGrid1.Columns[i].Width));
end;
end;
procedure TMainForm.DBGrid1DblClick(Sender: TObject);
begin
EditForm.SHowModal;
end;
procedure TMainForm.DBGrid1PrepareCanvas(Sender: TObject; DataCol: integer;
Column: TColumn; AState: TGridDrawState);
begin
if (GlMac.IndexOf(BufDataSet1.FieldByName('Mac').AsString) >= 0) then
with (Sender as TDBGrid) do
begin
Canvas.Font.Color := OnlineFontColor;
Canvas.Font.STyle := [fsBold];
Canvas.Brush.Color := OnlineColor;
end;
end;
procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: boolean);
begin
if DataBaseChanged then
if MessageDlg('Warning', 'Database not saved, do you really want to exit',
mtConfirmation, [mbYes, mbNo], 0) = mrNo then
CanClose := False
else
CanClose := True;
end;
procedure TMainForm.FormCreate(Sender: TObject);
var
i: integer;
Flnm, St: string;
Str: TStringList;
begin
HomeDir := GetUserDir + '/.inventory/';
if not DirectoryExists(HomeDir) then
begin
CreateDir(HomeDir);
end;
RestoreForm(MainForm);
BufDataSet1.CreateDataset;
for i := 0 to DBGrid1.Columns.Count - 1 do
begin
if i < 10 then
St := 'Col0' + IntToStr(i)
else
St := 'Col' + IntToStr(i);
DBGrid1.Columns[i].Width :=
StrToInt(GetStdIni('Datagrid', st, IntToStr(DBGrid1.Columns[i].Width)));
end;
Panel5.Caption := 'Interface: ' + GetStdIni('Settings', 'IFcmd', 'eth0');
if GetEnvironmentVariable('USER') <> 'root' then
ShowMessage('To run the scan options, this program needs to be run as root');
Str := TStringList.Create;
if DoCommand('/bin/bash -c "which arp-scan"', Str) <> 0 then
begin
ShowMessage('Cannot find the arp-scan application. If not installed, you cannot do any scanning.');
Scan.Enabled := False;
end;
if DoCommand('/bin/bash -c "which nmap"', Str) <> 0 then
begin
ShowMessage('Cannot find the nmap application. If not installed, you cannot do any scanning for names and open ports');
AGetOpenPorts.Enabled := False;
end;
Str.Free;
if GetSTdIni('Settings', 'LoadLast', 'True') = 'True' then
Flnm := GetStdIni('Settings', 'SaveFile', '');
if Flnm <> '' then
LoadDataBase(Flnm);
DataBaseChanged := False;
Panel6.Caption := '';
CB1.Items.Clear;
for i := 0 to BufDataSet1.FieldCount - 1 do
CB1.Items.Add(BufDataSet1.Fields[i].FieldName);
CB1.ItemIndex := 0;
GlMac := TStringList.Create;
OnlineFontColor := StrToInt(GetStdIni('Settings', 'OnlineTextColor',
IntToStr(ClBlack)));
OnlineCOlor := StrToInt(GetStdIni('Settings', 'OnlineTextBackGround',
IntToStr(ClDefault)));
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
SaveForm(MainForm);
GlMac.Free;
end;
procedure TMainForm.MenuItem10Click(Sender: TObject);
begin
AL1.ActionByName('AAbout');
end;
procedure TMainForm.MExitClick(Sender: TObject);
begin
AL1.ActionByName('Quit');
end;
procedure TMainForm.DoTheXML(FlNm: string);
var
Doc: TXMLDocument;
Node, node1, node2, node3: TDomNode;
mac, port, portstatus, hostname: string;
Count: integer;
begin
Memo1.Lines.Add('Nmap xml file: ' + FlNm);
Count := 0;
try
ReadXMLFile(Doc, FlNm);
Node := Doc.DocumentElement.FindNode('host');
while Node <> nil do
begin
Hostname := '';
Inc(Count);
Node1 := Node.FirstChild;
while Node1 <> nil do
begin
if Node1.Nodename = 'hostnames' then
begin
Node2 := Node1.FirstChild;
if Node2 <> nil then
if Node2.HasAttributes then
hostname := node2.Attributes.GetNamedItem('name').NodeValue
else
hostname := '';
end;
if Node1.Nodename = 'address' then
begin
if Node1.HasAttributes then
begin
if node1.Attributes.GetNamedItem('addrtype').NodeValue = 'mac' then
begin
mac := uniformmac(node1.Attributes.GetNamedItem('addr').NodeValue, ':');
if BufDataSet1.Locate('Mac', Mac, []) then
begin
BufDataSet1.Edit;
end;
end;
end;
end;
if Node1.NodeName = 'ports' then
begin
Node2 := Node1.FirstChild;
while Node2 <> nil do
begin
if node2.NodeName = 'port' then
Port := node2.Attributes.GetNamedItem('portid').NodeValue;
Node3 := Node2.FirstChild;
if node3.NodeName = 'state' then
PortStatus := node3.Attributes.GetNamedItem('state').NodeValue;
if port = '20' then
BufDataSet1.FieldByName('FTP').AsBoolean := (PortStatus = 'open');
if port = '22' then
BufDataSet1.FieldByName('SSH').AsBoolean := (PortStatus = 'open');
if port = '80' then
BufDataSet1.FieldByName('HTTP').AsBoolean := (PortStatus = 'open');
if port = '161' then
BufDataSet1.FieldByName('SNMP').AsBoolean := (PortStatus = 'open');
if port = '443' then
BufDataSet1.FieldByName('HTTPS').AsBoolean := (PortStatus = 'open');
Node2 := node2.Nextsibling;
end;
end;
Node1 := Node1.NextSibling;
end;
if BufDataSet1.State = dsEdit then
begin
BufDataSet1.FieldByName('Hostname').AsString := Hostname;
BufDataSet1.Post;
end;
Application.ProcessMessages;
Node := Node.NextSibling;
end;
finally
Doc.Free;
end;
Memo1.Lines.Add(IntToStr(Count) + ' IP addresses was scanned');
end;
procedure TMainForm.LoadDataBase(FlNm: string);
var
Fl: TextFile;
St: string;
StL: TStringList;
begin
if BufDataSet1.Active then
if BufDataSet1.RecordCount > 0 then
if MessageDlg('Do you want to ovwerwrite the database?', mtWarning,
[mbYes, mbNo], 0) = mrYes then
EmptyDataBase
else
Exit;
StL := TStringList.Create;
try
AssignFile(Fl, FlNm);
Reset(Fl);
ReadLn(Fl, St);
Split(St, ';', Stl);
CloseFile(Fl);
except
ShowMessage('Cannot open ' + Flnm);
Exit;
end;
if StL.Count <> 11 then
begin
ShowMessage(OD1.Filename + ' has a wrong format');
end
else
begin
if not BufDataSet1.Active then
BufDataSet1.CreateDataSet;
Reset(Fl);
while not EOF(Fl) do
begin
ReadLn(Fl, St);
Split(St, ';', Stl);
try
with BufDataSet1 do
begin
Append;
FieldByName('Mac').AsString := StL[0];
FieldByName('IP').AsString := StL[1];
FieldByName('Hostname').AsString := StL[2];
FieldByName('OS').AsString := Stl[3];
FieldByName('Groups').AsString := StL[4];
FieldByName('SSH').AsString := StL[5];
FieldByName('HTTP').AsString := StL[6];
FieldByName('HTTPS').AsString := StL[7];
FieldByName('FTP').AsString := StL[8];
FieldByName('SNMP').AsString := StL[9];
FieldByName('Nagios').AsString := StL[10];
Post;
end;
except
ShowMessage('Could not read the line: ' + St);
end;
end;
CloseFile(Fl);
end;
StL.Free;
end;
procedure TMainForm.EmptyDatabase;
begin
with BufDataset1 do
begin
First;
while not EOF do
begin
Edit;
Delete;
First;
end;
end;
end;
procedure TMainForm.ShowMyMessage(Message: string);
var
ShowIt: string;
begin
FMyShowMessage.Message.Caption := Message;
ShowIt := GetStdIni('Messages', Message, 'True');
if ShowIt = 'True' then
FMyShowMessage.ShowModal;
end;
end.