GithubHelp home page GithubHelp logo

Comments (4)

gelmayan avatar gelmayan commented on May 31, 2024

In fact I'm working on a project for an IAAS offer of cloud computing. And It's all about the provisionning of the netscreen ISG 1000 series firewall of JUNIPER.

from sshj.

shikhar avatar shikhar commented on May 31, 2024

Can you describe how to reproduce this?

from sshj.

gelmayan avatar gelmayan commented on May 31, 2024

First of all thank you for replying to my issue. It's really a tough one.
In fact It's simple, you just develop a junit test for example to a host, in able to open , and close, several connexion via a loop.
Here is an the whole code of my testing junit:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Shell;

import org.apache.log4j.Logger;
import org.junit.Ignore;

import com.orange.fcr.exception.TimeOutException;
@Ignore
public class TestSSH2 {

    private static Logger logger = Logger.getLogger(TestSSH2.class);

    String TIMEOUT_ERROR_MESSAGE = "TimeOut: unable to have a response from the device";

    private long send(OutputStream out, String commande) throws IOException {
        long count = 0L;
        count = commande.getBytes().length;
        out.write(commande.getBytes(), 0, commande.getBytes().length);

        out.flush();
        return count;
    }

    public SSHClient sshClient;


    @SuppressWarnings("static-access")
    public String receive(InputStream in, int bufSize, int sshTimeOut) throws IOException,
            InterruptedException, TimeOutException {

        long deadline = 0;
        if (sshTimeOut >= 0){
            deadline = new Date().getTime() + sshTimeOut;
        }
        int read = 0;
        StringBuilder resultat = new StringBuilder();

        Pattern p = Pattern.compile("^*([a-zA-Z0-9()]+)->\\s*$");
        Matcher m;
        int count = 0;//nomber of bytes to read
        //waiting for the first bytes to read
        while ((count=in.available()) < 0) {
            if ((deadline != 0) && new Date().getTime() > deadline) {
                throw new TimeOutException(TIMEOUT_ERROR_MESSAGE);
            }
            try {
                Thread.currentThread().sleep(100);
            }catch (InterruptedException ignored) {
                /* ignore */
            }
        }
        //begin reading the bytes
        while(((count =in.available()) >= 0)){
            byte[] buf = new byte[bufSize];
            if(count > 0){
                read = in.read(buf);
            }
            else{
                read = 0;
            }
            if (read > 0) {
                String res = new String(buf, 0, read);
                resultat.append(res);
            }

            m = p.matcher(resultat);
            //Go out when I find a first read
            if (m.find()) {
                break;
            }//deadline passed: timeout exception finishing to read the message
            else if ((deadline != 0) && (new Date().getTime() > deadline)) {
                throw new TimeOutException(TIMEOUT_ERROR_MESSAGE);
            }
        }
        return resultat.toString();
    }




    public void connect() throws IOException{
        this.sshClient = new SSHClient();
        sshClient.addHostKeyVerifier("c5:c6:59:24:b6:9c:62:31:29:6a:c1:c4:a5:fa:ea:05");
        sshClient.connect("10.1.31.43", 22);
        sshClient.authPassword("dihadmin", "dihadmin");
    }

    public static void main(String... args) throws Exception {


        TestSSH2 testSSH2 = new TestSSH2();
        for(int i = 0; i < 300; i++){
            testSSH2.connect();

            try {

                Session session = testSSH2.sshClient.startSession();
                Shell shell1 = null;

                try {
                    logger.error("begin loop n°:"+i);
                    session.allocateDefaultPTY();

                    shell1 = session.startShell();

                    String resultat1;

                    String resultat2;

                    String resultat3;
                    logger.error("loop n°:"+i + " preambule receive:");
                    testSSH2.receive(shell1.getInputStream(),
                            shell1.getLocalMaxPacketSize(),2000);

                    logger.error("loop n°:"+i + " send n°1");
                    testSSH2.send(shell1.getOutputStream(), "get console\n");

                    logger.error("loop n°:"+i + "receive n°1");
                    resultat1 = testSSH2.receive(shell1.getInputStream(),
                            shell1.getLocalMaxPacketSize(),2000);

                    logger.error("loop n°:"+i + " send n°2");
                    testSSH2.send(shell1.getOutputStream(), "get vrouter\n");

                    logger.error("loop n°:"+i + " receive n°2");
                    resultat2 = testSSH2.receive(shell1.getInputStream(),
                            shell1.getLocalMaxPacketSize(),2000);

                    logger.error("loop n°:"+i + " send n°3");
                    testSSH2.send(shell1.getOutputStream(), "get config\n");

                    logger.error("loop n°:"+i + " receive n°3");
                    resultat3 = testSSH2.receive(shell1.getInputStream(),
                            shell1.getLocalMaxPacketSize(),3000);

                    logger.error("loop n°:"+i + " send n°4");
                    testSSH2.send(shell1.getOutputStream(), "exit\n");

                    logger.error("response for cmd1:"+resultat1);
                    logger.error("response for cmd2:"+resultat2);
                    logger.error("response for cmd3:"+resultat3);

                } finally {
                    shell1.close();
                    session.close();
                }

            } finally {
                testSSH2.sshClient.disconnect();
            }
        }
    }
}

As you can see in this code, I'm in a loop for, I'm creating for each iteration, a new sshClient base on the API of sshj.
I disconnect from it at the end.
I have no errors for about 100 connexions, and then it stops. I'm not able to connect to the device.
Anyway, it is quite random the issue, It happens specially when you try to make connexions one after the other.

Any help can be welcomed. Don't hesitate if you nedd more information!!!

from sshj.

shikhar avatar shikhar commented on May 31, 2024

What SSH server is this? I can't reproduce on my end, using OpenSSH sshd 5.6. Complete debug-level logs would be very helpful.

from sshj.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.