Hi,

 

We use a script to send commands to various external HLR vendors using SSH.

The scenario is as follows: connect to SSH server, receive the prompt, send command, receive response and analyze it.

With one specific vendor, only the prompt on connection is received.

SSH server doesn't get the command, only the login request, and my client receives back only the command which was sent without any response.

 

I tried it both with jsch-0.1.20 and jsch-0.1.49 jar.

I can execute the command from the command line using, but it fails with jsch and the same code works for another vendor.

I can send the snoop both for manual and script execution.

 

Please assist.

 

The code is as follows:

 

public class HLRTest {

 

    private Channel _sshChannel = null;

    private Session _sshSession = null;

 

    protected OutputStream _out;

    protected InputStream _in;

 

    public HLRTest() {

        try {

            JSch jsch = new JSch();

            _sshSession = jsch.getSession(config.getUser(), config.getHlrAddress(), config.getHlrPort());

 

            _sshSession.setPassword(config.getPassword());

 

            Hashtable<String, String> sessionConfig = new Hashtable<String, String> ();

 

            sessionConfig.put("StrictHostKeyChecking", "no");

            _sshSession.setConfig(sessionConfig);

 

            String ver = _sshSession.getClientVersion() ;

            System.out.println("HLRClient Client SSH version =" + ver);

 

            _sshSession.setTimeout(config.getReadTimeoutMS());

            _sshSession.connect(config.getConnectTimeoutMS());

            System.out.println("HLRClient Session Connected" );

 

            _sshChannel = _sshSession.openChannel("shell");

 

            // Must get the streams after  openChannel

            _in  = _sshChannel.getInputStream();

            _out = _sshChannel.getOutputStream();

 

//            _sshChannel.connect(config.getConnectTimeoutMS());

            _sshChannel.connect();

 

            // Advance to a prompt

            readUntil(config.getPrompt(), config.getReadTimeoutMS());

            System.out.println("HLRClient  SSH Login succeeded ");

            System.out.println("HLRClient Server SSH version =" + _sshSession.getServerVersion());

        }

        catch(JSchException ex){

            System.out.println("Could not connect to HLR: " + ex);

            throw new RuntimeException(ex);

        }

        catch(Exception ex){

            System.out.println("Could not connect to HLR: " + ex);

            throw new RuntimeException(ex);

        }

    }

 

 

    // Read from the HLR server until pattern match or until timeout

    protected String readUntil(String pattern,

                               int readTimeoutMS) {

        System.out.println("HLRClient readUntil function start looking for pattern = " + pattern);

 

        StringBuffer sResponse = new StringBuffer();

 

        try {

            System.out.println("HLRClient readUntil wait " + readTimeoutMS + " if nothing available on the socket");

            waitForAvailable(_in, readTimeoutMS);

 

            System.out.println("HLRClient readUntil start the socket read loop ");

 

            // if read did not time out, still need to see if a pattern match within a timeout

            long until = System.currentTimeMillis() + readTimeoutMS;

            while (true) {

                if (System.currentTimeMillis() > until) {

                    System.out.println("HLRClient - input read timed out- pattern wasn't received from telnet server :" + pattern);

                    throw new SocketTimeoutException("HLRClient - input read timed out- pattern wasn't received from telnet server");

                }

 

// ###

                int bytesAvailable = _in.available();

                if (bytesAvailable > 0) {

                    System.out.println("\n### bytesAvailable: " + bytesAvailable);

 

                    byte[] tmp = new byte[bytesAvailable];

                    int read = _in.read(tmp);

                    String lastLine = new String(tmp, 0, read);

                    sResponse.append(lastLine);

 

                    bytesAvailable = _in.available();

                    System.out.println("\n*** read: " + read);

                    System.out.println("\n*** _in.available(): " + bytesAvailable);

                    System.out.println("\n*** lastLine: " + lastLine);

 

                    if (bytesAvailable <= 0 && lastLine.indexOf( pattern ) > -1) {

                        System.out.println("HLRClient Pattern found = " + sResponse);

                        return sResponse.toString();

                    }

                }

            }

       } catch (SocketTimeoutException ex) {

            System.out.println("HLRClient error SocketTimeoutException on read");

            System.out.println("HLRClient ERROR FROM SERVER" + sResponse.toString());

            disconnect() ;

            throw new RuntimeException(ex);

        } catch(Exception ex) {

            System.out.println("HLRClient Exception on read");

            System.out.println("HLRClient ERROR FROM SERVER : " + sResponse.toString());

            disconnect() ;

            throw new RuntimeException(ex);

        }

    }

 

 

    // wait readTimeout for the read on an input stream.

    // if nothing comes in for a readTimeout throw SocketTimeoutException

    private void waitForAvailable(InputStream in,

                                  int readTimeoutMS) throws IOException {

        long until = System.currentTimeMillis() + readTimeoutMS;

 

        while (in.available() == 0) {

            if (System.currentTimeMillis() > until) {

                throw new SocketTimeoutException("HLRClient - input read timed out- nothing to read");

            }

            try {

                Thread.sleep(100);

            } catch (InterruptedException ignore) {

            }

        }

    }

 

 

    // Write string to the telnet Server

    protected void write(String value) {

        try {

            _out.write(value.getBytes());

//            _out.flush();

            _out.write(_endOfLineBytes);

            _out.flush();

            System.out.println("Now sent to HLR: " + value);

        } catch(IOException e) {

            System.out.println("HLRClient IOException occured: " + e);

            throw new RuntimeException(e);

        }

    }

 

 

    protected void disconnect() {

        if (_sshChannel != null && _sshChannel.isConnected()) {

            _sshChannel.disconnect();

            _sshSession.disconnect();

        }

    }

 

 

    public static void main(String[] args) {

        try {

            HLRTest hlrClient = new HLRTest();

 

            // Write the request into the socket

            hlrClient.write(sHlrCommand);

 

            String sReceivedStringFromHLR = hlrClient.readUntil(_config.getPrompt(), _config.getReadTimeoutMS());

 

            System.out.println("HLRClient FROM SERVER: " + sReceivedStringFromHLR);

 

            if ( sReceivedStringFromHLR.contains(_config.getErrorNone())) {

                System.out.println("HLRClient - HLR provision SUCCESS !");

            } else {

                throw new RuntimeException("UNKNOWN ERROR");

            }

 

        } catch (Exception ex) {

            System.out.println("HLRClient Exception: " + ex);

        }

    }

}



“This e-mail message may contain confidential, commercial or privileged information that constitutes proprietary information of Comverse Technology or its subsidiaries. If you are not the intended recipient of this message, you are hereby notified that any review, use or distribution of this information is absolutely prohibited and we request that you delete all copies and contact us by e-mailing to: security@comverse.com. Thank You.”