728x90

안드로이드 폰에서 SSH 어플을 사용하고자 한다면, 검색해보면 가장 추천하는 앱은 JuiceSSH 이고, open source project로 진행되는 ConnectBot 이 있다는 걸 알았다.  좀 더 실력이 키워야 분석 및 활용이 가능할 것 같다.


Java 언어로 개발된 SSH2 라이브러리는 JSCH(Java Secure Channel) 를 확인할 수 있다.

구글링하면 예제 많이 나오는데 제대로 동작되는 걸 해보려면 쉽지 않더라.

http://www.jcraft.com/jsch/ 에 가면 jsch-0.1.55.jar 파일을 다운로드할 수 있다.


https://github.com/jonghough/AndroidSSH 에서 소스 코드를 다운로드 받아서 어플을 실행해 볼 수 있다.


테스트 결과 LG G5 안드로이드 6.0 운영체제에서는 잘 동작을 했다.

그런데 삼성 갤럭시 S7 안드로이드 8.0, 삼성 갤럭시 S10 안드로이드 9.0 운영체제 폰에서는 동작이 제대로 안되더라.

코드가 오래전에 올려진 것이라서 위험권한을 추가하고 동작시켜도 동작이 안된다.

쓰레드 처리에 대한 학습을 좀 하고 나서 몇가지 사항을 수정해서 동작되도록 했다.


앱 build.gradle

android {
    compileSdkVersion 28


   // 중간 생략


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'gun0912.ted:tedpermission:2.0.0'
    implementation 'androidx.multidex:multidex:2.0.0' // minSdkVersion 이 21 이하인 경우 사용
    implementation 'com.jcraft:jsch:0.1.55'
    implementation 'org.bouncycastle:bcprov-jdk16:1.46'
}


MainActivity.java 수정사항

- 위험권한 설정 처리코드 추가 구현 필요


ShellController.java 수정사항

    public void writeToOutput(final String command) {
        if (mDataOutputStream != null) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        mDataOutputStream.writeBytes(command + "\r\n");
                        mDataOutputStream.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();

        }
    }


   public void openShell(final Session session, Handler handler, EditText editText) throws JSchException, IOException {
        Log.e(TAG, "openShell start!");
        if (session == null) throw new NullPointerException("Session cannot be null!");
        if (!session.isConnected()) throw new IllegalStateException("Session must be connected.");
        final Handler myHandler = handler;
        final EditText myEditText = editText;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    mChannel = session.openChannel("shell");
                    mChannel.connect();
                    try {
                        mBufferedReader = new BufferedReader(new InputStreamReader(mChannel.getInputStream()));
                        mDataOutputStream = new DataOutputStream(mChannel.getOutputStream());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                } catch (JSchException e) {
                    e.printStackTrace();
                }

                try {
                    String line;
                    while (true) {
                        while ((line = mBufferedReader.readLine()) != null) {
                            final String result = line;
                            if (mSshText == null) mSshText = result;
                            Log.e(TAG, "result : " + result);
                            myHandler.post(new Runnable() {
                                public void run() {
                                    synchronized (myEditText) {
                                        ((SshEditText)myEditText).setPrompt(result); //set the prompt to be the current line, so eventually it will be the last line.
                                        myEditText.setText(myEditText.getText().toString() + "\r\n" + result + "\r\n"+fetchPrompt(result));
                                        Log.e(TAG, "LINE : " + result);
                                    }
                                }
                            });
                        }

                    }
                } catch (Exception e) {
                    Log.e(TAG, " Exception " + e.getMessage() + "." + e.getCause() + "," + e.getClass().toString());
                }
            }
        }).start();
    }
 


내가 테스트한 GitHub 소스는 칼라 코드 지원은 안된다.

SFTP 기능 구현은 테스트 하지 않았다.



참고하면 도움될 게시글
싱글톤(Singleton) 개념 이해 : https://link2me.tistory.com/1528
Java Thread 이해 및 Thread Life Cycle : https://link2me.tistory.com/1730
Java Thread 상태 제어 : https://link2me.tistory.com/1731
Java Thread 동기화 : https://link2me.tistory.com/1732


블로그 이미지

Link2Me

,