如何以编程方式启动ssh sever android,以及如何获取用于连接设备的用户名和密码

时间:2016-07-23 07:11:23

标签: android ssh jsch

我正在开发类似 sshdroid的应用

我想在android os上打开ssh连接,我想从pc连接app。

我使用了JSCH lib,但这个lib用于将android连接到pc。 我的要求是pc到android, 任何人都知道任何lib或任何源代码可用。

我已经尝试过了。

connectbot.(it is unmaintained lib).
JSCH lib (it is connect android to pc).
SSHelper_source (not help to me).
SSHJ ( tried not helpful).

3 个答案:

答案 0 :(得分:2)

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();
    }

}

来自amard开发人员的回答。 (感谢amar给出了正确答案)

Check example on github

如果你得到以下错误

错误:任务执行失败&gt;

  

&#39;:应用程序:transformResourcesWithMergeJavaResForDebug&#39 ;. &GT;   com.android.build.api.transform.TransformException:   com.android.builder.packaging.DuplicateFileException:在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

然后尝试在您的应用build.gradle中使用此功能来解决上述异常。

apply plugin: 'com.android.model.application'
model {
 android {
   ...
 }
 android.packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
 }
}

答案 1 :(得分:2)

检查github上的示例 https://github.com/stepinto/android-sshd

如果您收到此错误

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

然后尝试在您的应用build.gradle中使用它来解决上述异常。

apply plugin: 'com.android.model.application'
model {
 android {
   ...
 }
 android.packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
 }
}

答案 2 :(得分:0)

到目前为止,我把它作为一个独立的程序工作,把它放到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();
    }

}

}

服务器将启动并运行50秒,由睡眠时间给出。 使用ssh sftptest@localhost -p 8888

连接到它

将其置于活动中将如下所示

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();
    }
}

服务器可能会在此运行60秒。 要使用adb shell 'ip addr'

发现手机的IP地址

要连接到android,它是一个稍长的命令行 ssh sftptest@ip.address.of.phone -o UserKnownHostsFile=/dev/null -p 8888 -T