Page 1 of 1

Blocking SSH to some clients hang

Posted: Tue 14 Apr 2020 02:17
Hi, using latest SecureBridge, purchased last week, Delphi XE4.

It seems I can only get Blocking (NonBlocking=False) mode to 'proper' Linux distros. CentOS & Debian servers work 100%, but our app access hundreds of various brand routers, that all support SSH. However, ANY of the routers hang when I send ExecuteCommand, all devices are Linux based so I'm not sure where I should start to look to find the problem.

Below is (not) my code but illustate the issue. I try to connect to 4 different devices, a server and 3 different types of routers; Mikrotik; Ubiquity; Cambium, all on latest software/firmware.

In NonBlocking=False, servers work 100% but routers just hang when I send the ExecuteCommand.
NonBlocking=True (sshshell.WriteString; sshclient.AsyncReceive+ReadString) the routers also send/reply correctly, but I'd really like to get NonBlocking=False working for all devices.
It is impossible for me to know when a reply is finished, as we sometimes work with unknown hardware, many times with customers that have old, slow routers on unreliable connections that sometimes drop regularly.

Code: Select all

function TForm2.TestSSH(const aIP,aUN,aPW,aCMD:String):String;
var ScSSHClient  : TScSSHClient;
    ScSSHShell   : TScSSHShell;
    ScFileStorage: TScFileStorage;
    ScSSHChannel:TScSSHChannel;
begin
 ScFileStorage:= TScFileStorage.Create(nil);
 ScSSHClient  := TScSSHClient  .Create(nil);
 ScSSHShell   := TScSSHShell   .Create(nil);
 ScSSHClient  .KeyStorage:=ScFileStorage;
 ScSSHShell   .Client    :=ScSSHClient;
 ScSSHClient  .HostName  :=aIP;
 ScSSHClient  .User      :=aUN;
 ScSSHClient  .Password  :=aPW;
 ScSSHClient  .Port      :=22;
 ScSSHClient  .HostKeyAlgorithms.AsString  :='ssh-rsa,ssh-dss,ecdsa';
 ScSSHClient  .OnServerKeyValidate:=ScSSHClient1ServerKeyValidate;
 ScSSHShell   .NonBlocking        :=False;
// ScSSHShell   .OnAsyncReceive     :=ScSSHShell1AsyncReceive;
 try
  ScSSHClient .Connect;
  ScSSHShell  .Connect;
 except
  on E: Exception do begin
                      ScSSHShell.Free;
                      ScSSHClient.Free;
                      ScFileStorage.Free;
                      exit('ERROR:'+E.Message);
                      Exit;
                     end;
 end;

 Result:=ScSSHShell.ExecuteCommand(aCMD);
 //HANGS ON ABOVE LINE, NEVER REACH HERE FOR ROUTERS, SERVERS ARE INSTANT AND 100% CORRECT REPLY
 ScSSHShell.Disconnect;
 ScSSHClient.Disconnect;
 ScSSHShell.Free;
 ScSSHClient.Free;
 ScFileStorage.Free;
end;
I then try to use it like this:

Code: Select all

 Memo1.Lines.Add(TestSSH('10.1.1.10'  ,'root' ,'passw')'     ,'cat /etc/resolv.conf'));       //CentOS 7 works 100%
 Memo1.Lines.Add(TestSSH('192.168.0.1','admin','passw','/system resource print'));  //Mikrotik router, HANGS
 Memo1.Lines.Add(TestSSH('10.1.49.253','admin','passw','cat /proc/cpuinfo') //Ubiquity router HANGS
 Memo1.Lines.Add(TestSSH('10.1.73.254','admin','passw','show dashboard')); //Cambium router HANGS
PS: Eventually this code will have to run in it's own thread. Also I need to work with many routers simultaneously, and as fast as possible; I cannot afford a lot of sleeps+wait-loops, any advice welcome as I'd prefer not to redesign the app workflow.
Greetings
AntonE
Locking down in South Africa

Re: Blocking SSH to some clients hang

Posted: Tue 14 Apr 2020 02:51
Found this snippet, wonder if it's related to my issue:

Warning: If the server does not support pseudo-tty (ssh -T or ssh host command), like mikrotik ssh server, then it is not possible to send multiline commands via SSH.

Re: Blocking SSH to some clients hang

Posted: Tue 14 Apr 2020 11:43
by ViktorV
[email protected] wrote: Tue 14 Apr 2020 02:51 Found this snippet, wonder if it's related to my issue:

Warning: If the server does not support pseudo-tty (ssh -T or ssh host command), like mikrotik ssh server, then it is not possible to send multiline commands via SSH.
It may be true for such a server.
To accomplish your task, try removing the TScSSHShell.Connect method call before TScSSHShell.ExecuteCommand. Note that the client only sends command to the server and isn't aware of how the server executes the commands.

Re: Blocking SSH to some clients hang

Posted: Tue 14 Apr 2020 13:28
Thanks for the reply.
To accomplish your task, try removing the TScSSHShell.Connect method call before TScSSHShell.ExecuteCommand.
Still just hangs, does not time out, just hangs indefinitely.
Regards
Anton

Re: Blocking SSH to some clients hang

Posted: Wed 15 Apr 2020 14:10
by ViktorV
Please check the operation of your router using any third-party tool and let us know the result.