0

I am using JSch in my Android app and I'd like to create a SSH connect without initially providing a username and password. I can connect if I provide a username and password, but I get a username is not given" error from JSch if I do not provide a username.

This is what I have tried to connect without providing the username and password.

private class ConnectSSH extends AsyncTask<Void, Void, String> {
    @Override
    protected String doInBackground(Void... a) {
        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession("192.168.1.42");
            session.setPort(22);
            session.connect();
            Channel channelssh = (Channel)session.openChannel("exec");
            StringBuilder sb = new StringBuilder();
            InputStream commandOutput = channelssh.getInputStream();
            channelssh.connect();
            int readByte = commandOutput.read();
            while(readByte != 0xffffffff){
               sb.append((char)readByte);
               readByte = commandOutput.read();
            }
            channelssh.disconnect();
            return sb.toString();
        } catch (Exception e) {
            return "Error - " + e;
        }
    }
    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
    }
}

Will my only option be to change the source code of JSch to allow this, compile to a jar, and then import that?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • AFAIK, the username requirement comes as much from `sshd` as anything else. How have you configured your SSH daemon such that it does not require a username? – CommonsWare Jan 13 '16 at 22:37
  • In theory, you can assign a default username for your ssh daemon (see http://stackoverflow.com/questions/10197559/ssh-configuration-override-the-default-username) so you wouldn't have to provide it. But in this case, it's a sshd configuration. Another thing is the situation where someone starts a ssh connection and provide the username after that. In this case, I think JSch won't support this option. – Leo Jan 13 '16 at 22:46
  • Thanks Leo. I'm referring to terminal emulators like Putty, that require a username and password after the connection is made. Or is the connection not actually made until a username and password is given? – ImTroyMiller Jan 14 '16 at 02:03

1 Answers1

0

The JSch Session class constructor needs to have the username filled.

But it's the UserAuth implementation that ultimately decides what username is used.

So you can extend for example UserAuthPassword to assign the actual username to .username field at the start of .start() method.

class UserAuthUsernamePassword extends UserAuthPassword {

  public boolean start(Session session) throws Exception {
    this.username = "real_username";
    return super.start(session);
  }

}

And you just pass a dummy value to username argument of Session constructor.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992