We are implementing the SSHClient and SSHChannel into our application. We currently use an old SSClient that is no longer supported so we switched to your Securebridge components.
We added the below code and it will not connect to the SSHServer we are currently testing with. We can connect to our test SSHServer using Putty and our old component. I have images of the connection using putty and our old component but could not insert them.
Here is the code we added:
Code: Select all
const
maxrecords=10;
type
tsshcsocket=class(TScSSHChannel)
public
devs:integer;
port:integer;
procedure ChannelSocketAsyncerror(Sender:tobject; E:Exception);
procedure ChannelSocketAsyncRead(Sender:tobject);
end;
tsshcclient=class(TScSSHClient)
public
devs:integer;
port:integer;
procedure ClientKeyValidation(Sender:tobject; Newserverkey:TSckey; var Accept:boolean);
end;
var
ssclint:array[1..maxrecords] of tsshcclient;
ssteln:array[1..maxrecords] of tsshcsocket;
ssfiles:TScFileStorage;
function createssclient(whdev,whindx:longint):integer;
var tcpipdtb_rec:dtbdefrec;
begin
with portvars^ do
begin
init_dtb_rec(tcpipdtb_rec);
read_dtb_rec(tcpipdtb_rec,portvars^.indxmap[whindx]);
createssclient:=create_sstelnet_socket(tcpipdtb_rec,portvars^.ip_strs[whdev]);
end;
end;
function create_sstelnet_socket(whportdtb:dtbdefrec; ipprt:string):integer;
var crteans:integer; ipstr:string;
begin
with portvars^ do
begin
ipstr:=ipprt;
delete(ipstr,1,6);
if ssfiles=nil then ssfiles:=TScFileStorage.create(nil);
if ssfiles<>nil then
begin
ssfiles.Path:=wavepath;
ssclint[whportdtb.prtid]:=tsshcclient.Create(nil);
if ssclint[whportdtb.prtid]<>nil then
begin
ssclint[whportdtb.prtid].KeyStorage:=ssfiles;
ssclint[whportdtb.prtid].HostKeyAlgorithms.AsString:='ssh-rsa,ssh-dss';
ssclint[whportdtb.prtid].Authentication:=atpassword;
ssclint[whportdtb.prtid].HostName:=get_str_item(ipstr,1,';');
ssclint[whportdtb.prtid].Port:=strtoint(get_str_item(ipstr,2,';'));
ssclint[whportdtb.prtid].User:=get_str_item(ipstr,3,';');
ssclint[whportdtb.prtid].Password:=get_str_item(ipstr,4,';');
//
ssclint[whportdtb.prtid].OnServerKeyValidate:=ssclint[whportdtb.prtid].ClientKeyValidation;
//
debuginfo(wavepath,' Create SSHChannel',0);
sstelnt[whportdtb.prtid]:=tsshcsocket.Create(nil);
if (ssfiles<>nil) and (ssclint[whportdtb.prtid]<>nil) and (sstelnt[whportdtb.prtid]<>nil) then
begin
sstelnt[whportdtb.prtid].onasyncerror:=sstelnt[whportdtb.prtid].ChannelSocketAsyncerror;
sstelnt[whportdtb.prtid].onasyncreceive:=sstelnt[whportdtb.prtid].ChannelSocketAsyncRead;
//
sstelnt[whportdtb.prtid].Client:=ssclint[whportdtb.prtid];
sstelnt[whportdtb.prtid].Direct:=false;
crteans:=0;
try
if initmsg.Visible then
begin
inc(portset);
initmsg.prtset.caption:=inttostr(portset);
initmsg.prtset.update;
inc(portpass);
initmsg.prtpass.caption:=inttostr(portpass);
initmsg.prtpass.update;
end;
except
end;
end else
begin
crteans:=1;
end;
end else
begin
crteans:=1;
end;
end else
begin
crteans:=1;
debuginfo(wavepath,' File Storage Not Created',0);
end;
if crteans=1 then
begin
if portok=0 then
begin
portok:=2;
init_lstr:='Error: Connecting to Port '+decode_portid(whportdtb.prtid)+' at SSTelnet Client '+ipstr+':'+#13+#13+
'Use the Device Configuration Dialog Inside the Device Manager''s Menu to Correct the Configuration.';
writeto_opencomm_dtb('SSHTelnet Client at Port '+decode_portid(whportdtb.prtid)+' for IP address: '+ipstr+' Error:Connecting-C');
end;
end;
end;
create_sstelnet_socket:=crteans;
end;
find_sstelnetsocket:=scktfnd;
end;
Procedure Connect_to_SSClient;
begin
if (portvars^.sstelnt[whport]<>nil) then
begin
if sstelnt[whport].connected then sstelnt[whport].Disconnect;
freeandnil(sstelnt[whport]);
sstelnt[whport]:=nil;
end;
if (portvars^.ssclint[whport]<>nil) then
begin
if ssclint[whport].Connected then ssclint[whport].Disconnect;
freeandnil(ssclint[whport]);
ssclint[whport]:=nil;
end;
if ssfiles=nil then ssfiles:=TScFileStorage.create(nil);
if ssfiles<>nil then
begin
ssfiles.Path:=wavepath;
connans:=createssclient(whport,whdev);
if connans=0 then
begin
try
ssclint[whport].Connect; //It constantly fails when trying to connect to the SSHServer
except showmessage('Connection to SSHClient Failed'); end;
if ssclint[whport].Connected then
begin
with sstelnt[whport] do
begin
try
set_varval('IPCONNECT',whdev,'NO',0);
connect;
if connected then
begin
set_ethernet_connect(whdev);
connans:=0;
end;
except showmessage('Connection to SSHChannel Failed'); end;
end;
end;
end;
end;
end;
end;
function find_sstelnetsocket(hoststr:string; hostprt:integer):integer;
var ert,scktfnd:integer;
begin
scktfnd:=-1;
with portvars^ do
begin
ert:=0;
while ((ert<=maxrecords) and (scktfnd=-1)) do
begin
try
if ((ssclint[ert]<>nil) and (hoststr=ssclint[ert].HostName) and (hostprt=ssclint[ert].Port)) then
begin
scktfnd:=ert;
end;
except
on e:exception do scktfnd:=-1;
end;
ert:=ert+1;
end;
end;
procedure tsshcsocket.ChannelSocketAsyncerror(Sender:tobject; E:Exception);
var fnddev,ssd,azm:integer;
begin
fnddev:=find_sstelnetsocket(TScSSHChannel(sender).client.HostName,TScSSHChannel(sender).client.Port);
if (fnddev>-1) then debuginfo(wavepath,'Found SSHChannel - dev = '+inttostr(fnddev),0);
end;
procedure tsshcsocket.ChannelSocketAsyncRead(Sender:tobject);
var fnddev,ssd,azm:integer; et_txtbuf:wrkrchr; indata:tbytes; movechar:dword;
begin
with portvars^ do
begin
fnddev:=find_sstelnetsocket(TScSSHChannel(sender).client.HostName,TScSSHChannel(sender).client.Port);
if (fnddev>-1) and (TScSSHChannel(sender).InCount>0) then
begin
with sender as TScSSHChannel do
begin
setlength(indata,portvars^.sstelnt[devword[ithport]].InCount);
movechar:=portvars^.sstelnt[devword[ithport]].ReadBuffer(indata,0,portvars^.sstelnt[devword[ithport]].InCount);
if movechar>0 then
begin
azm:=0;
for ssd:=1 to movechar do
begin
et_txtbuf[azm]:=char(indata[ssd]);
inc(azm);
if ((ssd=movechar) or (azm>1022)) then
begin
log_et_entry(portvars^.prtsarr[fnddev],et_txtbuf,azm);
azm:=0;
end;
end;
end;
end;
end;
end;
end;
procedure tsshcclient.ClientKeyValidation(Sender:tobject; Newserverkey:TSckey; var Accept:boolean);
begin
Accept:=true; //This call back procedure is never called
end;
Thanks for all your help.