Skip to content

Commit 454571e

Browse files
committed
fix: SSH command line tweaks, patch from jarczakpawel
Refs #2474
1 parent 26b9696 commit 454571e

1 file changed

Lines changed: 29 additions & 24 deletions

File tree

source/dbconnection.pas

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,15 +1069,15 @@ class function TSecureShellCmd.SshpassPath: String;
10691069

10701070
procedure TSecureShellCmd.Connect;
10711071
var
1072-
SshCmd, SshCmdDisplay, DialogTitle: String;
1072+
SshCmd, SshCmdDisplay, DialogTitle, TargetHost: String;
10731073
OutText, ErrorText, AllPipesText, UserInput: String;
10741074
rx: TRegExpr;
10751075
ExitCode: LongWord;
10761076
PortChecks, i: Integer;
10771077
CheckIntervalMs: Integer;
10781078
TimeStartedMs, WaitedMs, TimeOutMs: Int64;
10791079
EnvSshpass: String;
1080-
EnvList, ProcOutput: TStringList;
1080+
ProcOutput: TStringList;
10811081
begin
10821082
// Check if local port is open
10831083
PortChecks := 0;
@@ -1091,38 +1091,43 @@ procedure TSecureShellCmd.Connect;
10911091

10921092
// Build SSH command line
10931093
// plink bob@domain.com -pw myPassw0rd1 -P 22 -i "keyfile.pem" -L 55555:localhost:3306
1094-
SshCmd := FConnection.Parameters.SSHExe;
1095-
if FConnection.Parameters.SshIsPlink then
1096-
SshCmd := SshCmd + ' -ssh';
1097-
SshCmd := SshCmd + ' ';
1094+
TargetHost := '';
10981095
if FConnection.Parameters.SSHUser.Trim <> '' then
1099-
SshCmd := SshCmd + FConnection.Parameters.SSHUser.Trim + '@';
1096+
TargetHost := FConnection.Parameters.SSHUser.Trim + '@';
11001097
if FConnection.Parameters.SSHHost.Trim <> '' then
1101-
SshCmd := SshCmd + FConnection.Parameters.SSHHost.Trim
1098+
TargetHost := TargetHost + FConnection.Parameters.SSHHost.Trim
11021099
else
1103-
SshCmd := SshCmd + FConnection.Parameters.Hostname;
1100+
TargetHost := TargetHost + FConnection.Parameters.Hostname;
1101+
1102+
SshCmd := FConnection.Parameters.SSHExe;
11041103
EnvSshpass := '';
1105-
if FConnection.Parameters.SSHPassword <> '' then begin
1106-
if FConnection.Parameters.SshIsPlink then
1107-
SshCmd := SshCmd + ' -pw "' + StringReplace(FConnection.Parameters.SSHPassword, '"', '\"', [rfReplaceAll]) + '"'
1108-
else
1104+
if FConnection.Parameters.SshIsPlink then begin
1105+
SshCmd := SshCmd + ' -ssh ' + TargetHost;
1106+
if FConnection.Parameters.SSHPassword <> '' then
1107+
SshCmd := SshCmd + ' -pw "' + StringReplace(FConnection.Parameters.SSHPassword, '"', '\"', [rfReplaceAll]) + '"';
1108+
if FConnection.Parameters.SSHPort > 0 then
1109+
SshCmd := SshCmd + ' -P ' + IntToStr(FConnection.Parameters.SSHPort);
1110+
if FConnection.Parameters.SSHPrivateKey <> '' then
1111+
SshCmd := SshCmd + ' -i "' + FConnection.Parameters.SSHPrivateKey + '"';
1112+
SshCmd := SshCmd + ' -N -L ' + IntToStr(FConnection.Parameters.SSHLocalPort) + ':' + FConnection.Parameters.Hostname + ':' + IntToStr(FConnection.Parameters.Port);
1113+
end else begin
1114+
if FConnection.Parameters.SSHPassword <> '' then
11091115
EnvSshpass := 'SSHPASS='+FConnection.Parameters.SSHPassword;
1116+
if FConnection.Parameters.SSHPort > 0 then
1117+
SshCmd := SshCmd + ' -p ' + IntToStr(FConnection.Parameters.SSHPort);
1118+
if FConnection.Parameters.SSHPrivateKey <> '' then
1119+
SshCmd := SshCmd + ' -i "' + FConnection.Parameters.SSHPrivateKey + '"';
1120+
// OpenSSH options must be placed before the destination host.
1121+
SshCmd := SshCmd + ' -o StrictHostKeyChecking=no -o IgnoreUnknown=WarnWeakCrypto -o WarnWeakCrypto=no-pq-kex -o ExitOnForwardFailure=yes -o ServerAliveInterval=60 -o ServerAliveCountMax=3';
1122+
SshCmd := SshCmd + ' -N -L ' + IntToStr(FConnection.Parameters.SSHLocalPort) + ':' + FConnection.Parameters.Hostname + ':' + IntToStr(FConnection.Parameters.Port) + ' ' + TargetHost;
11101123
end;
1111-
if FConnection.Parameters.SSHPort > 0 then
1112-
SshCmd := SshCmd + IfThen(FConnection.Parameters.SshIsPlink, ' -P ', ' -p ') + IntToStr(FConnection.Parameters.SSHPort);
1113-
if FConnection.Parameters.SSHPrivateKey <> '' then
1114-
SshCmd := SshCmd + ' -i "' + FConnection.Parameters.SSHPrivateKey + '"';
1115-
if not FConnection.Parameters.SshIsPlink then
1116-
SshCmd := SshCmd + ' -o StrictHostKeyChecking=no';
1117-
SshCmd := SshCmd + ' -N -L ' + IntToStr(FConnection.Parameters.SSHLocalPort) + ':' + FConnection.Parameters.Hostname + ':' + IntToStr(FConnection.Parameters.Port);
11181124

11191125
if not EnvSshpass.IsEmpty then begin
11201126
SshCmd := SshpassPath + ' -e ' + SshCmd;
1121-
EnvList := TStringList.Create;
1127+
FProcess.Environment.Clear;
11221128
for i := 0 to GetEnvironmentVariableCount - 1 do
1123-
EnvList.Add(GetEnvironmentString(i));
1124-
EnvList.Add(EnvSshpass);
1125-
FProcess.Environment := EnvList;
1129+
FProcess.Environment.Add(GetEnvironmentString(i));
1130+
FProcess.Environment.Add(EnvSshpass);
11261131
end;
11271132

11281133
rx := TRegExpr.Create;

0 commit comments

Comments
 (0)