4

I am developing app like sshdroid.

i want to open ssh connection on android os, and i want to connect app from pc.

I used JSCH lib , but this lib is used to connect android to pc. and my requirement is pc to android, any one know any lib or any source code is available.

I already tried.

connectbot.(it is unmaintained lib).
JSCH lib (it is connect android to pc).
SSHelper_source (not help to me).
SSHJ ( tried not helpful).
Yogesh Rathi
  • 5,684
  • 4
  • 42
  • 71

3 Answers3

3
public void startSSHServer() {
    int port = 8888;
    SshServer sshd = SshServer.setUpDefaultServer();
    sshd.setPort(port);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(
            "src/test/resources/hostkey.ser"));
    sshd.setSubsystemFactories(Arrays
            .<NamedFactory<Command>> asList(new SftpSubsystem.Factory()));
    sshd.setCommandFactory(new ScpCommandFactory());
    sshd.setShellFactory(new ProcessShellFactory(new String[] { "/system/bin/sh", "-i", "-l" })); // necessary if you want to type commands over ssh
    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {

        @Override
        public boolean authenticate(String u, String p, ServerSession s) {
            return ("sftptest".equals(u) && "sftptest".equals(p));
        }
    });

    try {
        sshd.start();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

Answer from amard Developer. (Thanks amar to given proper answer)

Check example on github

If you get below error

Error:Execution failed for task >

':app:transformResourcesWithMergeJavaResForDebug'. > com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/DEPENDENCIES File1: /home/yogesh/.gradle/caches/modules-2/files-2.1/org.apache.mina/mina-core/2.0.2/‌​e365a84cc76a64bf1508af83da0ea852c35e79c8/mina-core-2.0.2.jar File2: /home/yogesh/.gradle/caches/modules-2/files-2.1/org.apache.sshd/sshd-core/0.6.0/‌​2b9a119dd77a1decec78b0c511ba400c8655e96e/sshd-core-0.6.0.jar

then try using this in your apps build.gradle to solve above exception.

apply plugin: 'com.android.model.application'
model {
 android {
   ...
 }
 android.packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
 }
}
Community
  • 1
  • 1
Yogesh Rathi
  • 5,684
  • 4
  • 42
  • 71
2

Check example on github https://github.com/stepinto/android-sshd

If you get this error

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. >
com.android.build.api.transform.TransformException:
com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/DEPENDENCIES
File1: /home/yogesh/.gradle/caches/modules-2/files-2.1/org.apache.mina/mina-core/2.0.2/‌​e365a84cc76a64bf1508af83da0ea852c35e79c8/mina-core-2.0.2.jar
File2: /home/yogesh/.gradle/caches/modules-2/files-2.1/org.apache.sshd/sshd-core/0.6.0/‌​2b9a119dd77a1decec78b0c511ba400c8655e96e/sshd-core-0.6.0.jar

then try using this in your apps build.gradle to solve above exception.

apply plugin: 'com.android.model.application'
model {
 android {
   ...
 }
 android.packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
 }
}
kenny_k
  • 3,325
  • 4
  • 24
  • 36
amard
  • 21
  • 2
0

I got this to work as a standalone program so far, it should be relatively straightforward to put it into android.

build.gradle

android {
   . . .
       packagingOptions {
          exclude 'META-INF/DEPENDENCIES'
       }
}

dependencies {
   . . .
   compile group: 'org.apache.sshd', name: 'sshd-core', version: '0.6.0'
   compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.6.2'
   compile "org.bouncycastle:bcprov-jdk16:1.46" // only needed if you're in android

}

java class

public class Server {

static public void main(String []argv) {
    System.out.println("Hello Server");

    Server server = new Server();
    server.doAll();

    System.out.println("Goodbye Server");
}

public void doAll()  {

    startSSHServer();

    try {
        Thread.sleep(50000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public void startSSHServer() {
    int port = 8888;
    SshServer sshd = SshServer.setUpDefaultServer();
    sshd.setPort(port);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(
            "src/test/resources/hostkey.ser"));
    sshd.setSubsystemFactories(Arrays
            .<NamedFactory<Command>> asList(new SftpSubsystem.Factory()));
    sshd.setCommandFactory(new ScpCommandFactory());
    sshd.setShellFactory(new ProcessShellFactory(new String[] { "/system/bin/sh", "-i", "-l" })); // necessary if you want to type commands over ssh
    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {

        @Override
        public boolean authenticate(String u, String p, ServerSession s) {
            return ("sftptest".equals(u) && "sftptest".equals(p));
        }
    });

    try {
        sshd.start();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

The server will start and run for 50 seconds, given by the sleep time. Connect to it using ssh sftptest@localhost -p 8888

Putting it into an activity would look like this

public class MainActivity extends AppCompatActivity {


    private Button startButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startButton = (Button) findViewById(R.id.startButton);

        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startServer();
            }
        });
    }

    private void startServer() {

        Thread th = new Thread(new Runnable() {
            @Override
            public void run() {

                Server server = new Server();

                Log.i("SSHD","Start ssh");
                server.startSSHServer();

                try {
                    Thread.sleep(60000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                Log.i("SSHD","End ssh");
            }
        });

        th.start();
    }
}

The server may run for 60 seconds here. To discover the ip address of the phone use adb shell 'ip addr'

To connect to android it's a slightly longer command line ssh sftptest@ip.address.of.phone -o UserKnownHostsFile=/dev/null -p 8888 -T

Fabio
  • 2,519
  • 12
  • 29
  • Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. > com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/DEPENDENCIES File1: /home/yogesh/.gradle/caches/modules-2/files-2.1/org.apache.mina/mina-core/2.0.2/e365a84cc76a64bf1508af83da0ea852c35e79c8/mina-core-2.0.2.jar File2: /home/yogesh/.gradle/caches/modules-2/files-2.1/org.apache.sshd/sshd-core/0.6.0/2b9a119dd77a1decec78b0c511ba400c8655e96e/sshd-core-0.6.0.jar – Yogesh Rathi Aug 01 '16 at 13:45
  • pls see edited response, I included a fix for that error and also for a missing class – Fabio Aug 02 '16 at 01:11
  • For some reason I could log into my real Nexus 6P with Marshmallow but not on my emulated Nexus 6P with Marshmallow. Maybe it was something to do with phone's port being closed? – Fabio Aug 02 '16 at 01:22
  • It was fun creating git@github.com:fmatosqg/android_sshd.git – Fabio Aug 02 '16 at 01:41
  • can you give ans with how to set username and password..like sshdroid because if sys connect also done then ssh will ask password then which password i will enter please describe more. – Yogesh Rathi Aug 02 '16 at 04:22
  • You have full control of that using setPasswordAuthenticator() and using your own code. Currently this snippet is checking for user=sftptest and password sftptest: `return ("sftptest".equals(u) && "sftptest".equals(p));` – Fabio Aug 02 '16 at 04:26
  • shell request failed on channel 0 is came – Yogesh Rathi Aug 02 '16 at 04:44
  • if you're getting this message `[NioProcessor-3] INFO org.apache.sshd.server.channel.ChannelSession - Closing channel 0 immediately` it probably means that your shell is in another place instead of `/system/bin/sh`. Try `adb shell` and then `which sh`. – Fabio Aug 02 '16 at 05:31
  • can you update ans i am new in this area..so i am not understand – Yogesh Rathi Aug 02 '16 at 05:33
  • can you jump on a chat later? I'll probably be available in about 2 or 3h – Fabio Aug 02 '16 at 08:13
  • Apparently the chat doesn't work on the phone. Can you confirm whether your device has the /system.../sh file? – Fabio Aug 02 '16 at 10:26
  • how can test please give me instruction – Yogesh Rathi Aug 02 '16 at 10:35
  • On your PC type 'adb shell' . Then type 'which sh ; ls /system/bin'. – Fabio Aug 02 '16 at 10:41
  • Btw which phone and android do you use? – Fabio Aug 02 '16 at 10:41
  • ok that rules out problems with the shell. Can you share the full error message? You can use http://pastebin.com/ or something similar – Fabio Aug 02 '16 at 12:17
  • shell request failed on channel 0 is came – Yogesh Rathi Aug 02 '16 at 12:44
  • I'm sorry but I need more information to know what's happening. You can see if it's similar to http://stackoverflow.com/questions/27021641/how-to-fix-request-failed-on-channel-0 – Fabio Aug 03 '16 at 01:20