-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuExecLibSsh2.pas
84 lines (72 loc) · 1.63 KB
/
uExecLibSsh2.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
unit uExecLibSsh2;
interface
uses
SysUtils, Classes, AnsiStrings,
//
uBaseLibSsh2, uBaseChannelLibSsh2,
//
libssh2;
type
TExecLibSsh2 = class(TBaseChannelLibSsh2)
private
public
function OpenChannel: Boolean;
function Exec(const ACommand: AnsiString): AnsiString; {$IFDEF UNICODE}overload;{$ENDIF}
{$IFDEF UNICODE}
function Exec(const ACommand: string): string; overload;
{$ENDIF}
end;
implementation
{ TExecLibSsh2 }
function TExecLibSsh2.Exec(const ACommand: AnsiString): AnsiString;
var rc: Integer;
begin
while True do
begin
rc := libssh2_channel_exec(FChannel, PAnsiChar(ACommand));
if rc = LIBSSH2_ERROR_EAGAIN then
begin
WaitSocket();
Continue;
end;
Break;
end;
if (rc <> 0) then
begin
RaiseSshError('libssh2_channel_exec, ');
exit('');
end;
Result := ReadAll();
end;
function TExecLibSsh2.Exec(const ACommand: string): string;
begin
//libssh2_session_set_blocking(FSession, 0);
Result := string(Exec(AnsiString(ACommand)))
end;
function TExecLibSsh2.OpenChannel: Boolean;
var err: Integer;
begin
//libssh2_session_set_blocking(FSession, 0);
// /* Exec non-blocking on the remove host */
while True do
begin
FChannel := libssh2_channel_open_session(Fsession);
if FChannel = nil then
begin
err := GetLastErrorNum();
if err = LIBSSH2_ERROR_EAGAIN then
begin
WaitSocket();
Continue;
end;
end;
Break;
end;
if FChannel = nil then
begin
RaiseSshError('libssh2_channel_open_session, ');
exit(False);
end;
Exit(True);
end;
end.