如何使用JSCH连接到SSH服务器? [英] How to connect to a SSH server using JSCH?

查看:73
本文介绍了如何使用JSCH连接到SSH服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jsch连接到打开的ssh ubuntu服务器我只想看看我是否可以使用SSH从我的Android手机连接到Linux服务器,但是它不起作用.当我在android手机上启动该应用程序时,该应用程序崩溃了我已经附上了代码和LogCat文件我是Java和Eclipse的新手

I am trying to connect to my open ssh ubuntu server using jsch I just want to see if I can connect from my android phone to a linux server using SSH but it doesn't work. when i launch the app on the android phone the app crashes i have attached the code and the LogCat file I am new to java and eclipse

这是我的代码

package com.example.please;

import java.util.Properties;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    Button button;
    EditText usr_nm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        System.out.println("working");
        button=(Button) findViewById(R.id.btn);
        //button.setOnClickListener(handle);
        usr_nm = (EditText)findViewById(R.id.editText1);
        String host="x.x.x.x";

        new loadsomestuff().execute(host);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public class loadsomestuff extends AsyncTask<String, Integer, String>{

        String asd;
        @Override
        protected String doInBackground(String... arg0) {

            JSch jsch=new JSch();
            Properties props = new Properties(); 
            props.put("StrictHostKeyChecking", "no");

            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            config.put("compression.s2c", "zlib,none");
            config.put("compression.c2s", "zlib,none");

            Session session;
            try {
                session = jsch.getSession("user", "x.x.x.x",22);
                session.setConfig(config);
                session.setPassword("xxxxxxx");
                session.connect();
            } catch (JSchException e) {
                asd = "NOT_Executed";
                System.out.println("NOT_executed");
                e.printStackTrace();
                return "NOT_Executed";
            }          


            Context context = getApplicationContext();
            CharSequence text = "Connected to Pi";
            int duration = android.widget.Toast.LENGTH_SHORT;
            android.widget.Toast toast = android.widget.Toast.makeText(context, text, duration);
            toast.show();
            System.out.println("executed");
            asd = "executed";
            return "Executed";

            //return null;
        }

        @Override
        protected void onPostExecute(String abc){
            usr_nm.setText(asd);
        }

    }

}

LogCat显示以下内容

The LogCat shows the following

07-31 16:21:53.298: E/Trace(781): error opening trace file: No such file or directory (2)
07-31 16:21:53.858: I/System.out(781): working
07-31 16:21:54.028: I/Choreographer(781): Skipped 38 frames!  The application may be doing too much work on its main thread.
07-31 16:21:54.078: D/gralloc_goldfish(781): Emulator without GPU emulation detected.
07-31 16:21:54.177: I/Choreographer(781): Skipped 46 frames!  The application may be doing too much work on its main thread.
07-31 16:21:54.547: I/System.out(781): NOT_executed
07-31 16:21:54.547: W/System.err(781): com.jcraft.jsch.JSchException: java.net.SocketException: socket failed: EACCES (Permission denied)
07-31 16:21:54.557: W/System.err(781):  at com.jcraft.jsch.Util.createSocket(Util.java:344)
07-31 16:21:54.557: W/System.err(781):  at com.jcraft.jsch.Session.connect(Session.java:215)
07-31 16:21:54.557: W/System.err(781):  at com.jcraft.jsch.Session.connect(Session.java:183)
07-31 16:21:54.557: W/System.err(781):  at com.example.please.MainActivity$loadsomestuff.doInBackground(MainActivity.java:64)
07-31 16:21:54.557: W/System.err(781):  at com.example.please.MainActivity$loadsomestuff.doInBackground(MainActivity.java:1)
07-31 16:21:54.557: W/System.err(781):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-31 16:21:54.557: W/System.err(781):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-31 16:21:54.567: W/System.err(781):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-31 16:21:54.567: W/System.err(781):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-31 16:21:54.577: W/System.err(781):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-31 16:21:54.577: W/System.err(781):  at java.lang.Thread.run(Thread.java:856)
07-31 16:21:54.577: W/System.err(781): Caused by: java.net.SocketException: socket failed: EACCES (Permission denied)
07-31 16:21:54.577: W/System.err(781):  at libcore.io.IoBridge.socket(IoBridge.java:583)
07-31 16:21:54.588: W/System.err(781):  at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
07-31 16:21:54.588: W/System.err(781):  at java.net.Socket.startupSocket(Socket.java:559)
07-31 16:21:54.588: W/System.err(781):  at java.net.Socket.tryAllAddresses(Socket.java:127)
07-31 16:21:54.588: W/System.err(781):  at java.net.Socket.<init>(Socket.java:177)
07-31 16:21:54.588: W/System.err(781):  at java.net.Socket.<init>(Socket.java:149)
07-31 16:21:54.598: W/System.err(781):  at com.jcraft.jsch.Util.createSocket(Util.java:338)
07-31 16:21:54.608: W/System.err(781):  ... 10 more
07-31 16:21:54.608: W/System.err(781): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
07-31 16:21:54.618: W/System.err(781):  at libcore.io.Posix.socket(Native Method)
07-31 16:21:54.618: W/System.err(781):  at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
07-31 16:21:54.618: W/System.err(781):  at libcore.io.IoBridge.socket(IoBridge.java:568)
07-31 16:21:54.618: W/System.err(781):  ... 16 more

请帮助

推荐答案

您的应用程序必须在其 AndroidManifest.xml 中具有以下权限:

Your application must have the following permission in its AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET"/>

这篇关于如何使用JSCH连接到SSH服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆