Android sshj发送多个命令,收到错误

时间:2014-05-22 12:54:51

标签: android sshj

我试图从我的Android手机向raspi发送连续命令。我已经使用了此链接中的代码,但我收到了错误。

链接:https://stackoverflow.com/questions/23471439/android-sshexecute-multiple-commands-with-sshj/23806942?noredirect=1#comment36621476_23806942

主要:

package com.example.sshjdeneme;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

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

    Button buton1 = (Button) findViewById(R.id.button1);
    buton1.setOnClickListener(this);
}

@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;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Connection conn = new Connection();
    conn.execute("sudo uvccapture -m");

}

}

连接类:

        package com.example.sshjdeneme;

        import java.io.IOException;

        import net.schmizz.sshj.*;
        import net.schmizz.sshj.connection.channel.direct.Session;
        import net.schmizz.sshj.connection.channel.direct.Session.Command;
        import net.schmizz.sshj.transport.TransportException;
        import net.schmizz.sshj.userauth.UserAuthException;
        import android.os.AsyncTask;
        import android.util.Log;

        public class Connection extends AsyncTask<String, String, String> { 
            /**
        * @param args
        * @throws TransportException
        * @throws UserAuthException
        */
          final SSHClient ssh = new SSHClient();
          int pn = 22;
           String ipaddress = "192.168.1.1";
           String username = "pi";//root
           String password = "berkfurkan";//sah
           String result1 ="" ;
           String result2 ="" ;
           String result ="";

        @Override
        protected String doInBackground(String... command) {


          Log.i("doInBackground","doInBackground");
        //final SSHClient ssh = new SSHClient();
          String command1= new String(command[0]);
               // Adds a nullHostKeyVerifier
          ssh.addHostKeyVerifier(new NullHostKeyVerifier());
          try {
              ssh.connect(ipaddress, pn);

          } catch (IOException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
              return "NOT connecting";
          }

          // Authenticate with the password entered
                  try {
                      ssh.authPassword(username, password);
                  } catch (UserAuthException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                      return "NOT Authenticate";

                  }catch (TransportException e) {
                    // TODO: handle exception
                      e.printStackTrace();
                      return "NOT Authenticate";
                }



           // connect to the machine
           try {

               // start a new session
              Session session = ssh.startSession();
              Command cmd = session.exec(command1);



              if (cmd.isOpen())
                  {System.out.println("channel oppen");
                  System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
                  System.out.println("\n** exit status: " + cmd.getExitStatus());}



              System.out.println("output"+IOUtils.readFully(cmd.getInputStream()).toString());
               // reads the output of the command
           result = IOUtils.readFully(cmd.getInputStream()).toString();

                session.close();

           } catch (IOException e) {
               e.printStackTrace();
               return "session" ;

           }

           return result;

            }
        public void SSH() {
          try {
          ssh.addHostKeyVerifier(new NullHostKeyVerifier());
          ssh.connect(ipaddress, pn);
          ssh.authPassword(username, password);
        } catch (IOException e) {
          System.out.println(e.getMessage());
        }
        }

        @Override
        protected void onPostExecute(String result) {
          if(result.equalsIgnoreCase("NOT connecting")){
               Log.i("res1","NOT connecting");
              }
          if(result.equalsIgnoreCase("NOT Authenticate")){
               Log.i("res2","NOT Authenticate");
              }
          if(result.equalsIgnoreCase("session")){
               Log.i("res3","NOT session");
              }
          if (result.equals(""))
          Log.i(" resultat","NULL");
          else
              Log.i(" resultat",result);

        }
        }

null主机类:         package com.example.sshjdeneme;

    import java.security.PublicKey;

    import net.schmizz.sshj.transport.verification.HostKeyVerifier;

    public class NullHostKeyVerifier implements HostKeyVerifier {

    /*
    * This method is used to bypass HostKeyVerification.
    * It returns true for whatever the input is.
    *
    */


    @Override
    public boolean verify(String arg0, int arg1, PublicKey arg2) {
        // TODO Auto-generated method stub
        return false;
    }
    }

我在哪里做错了?谢谢

ps:错误消息

05-22 12:22:10.575: E/AndroidRuntime(379): FATAL EXCEPTION: main
05-22 12:22:10.575: E/AndroidRuntime(379): java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory
05-22 12:22:10.575: E/AndroidRuntime(379):  at net.schmizz.sshj.DefaultConfig.<init>(DefaultConfig.java:97)
05-22 12:22:10.575: E/AndroidRuntime(379):  at net.schmizz.sshj.SSHClient.<init>(SSHClient.java:136)
05-22 12:22:10.575: E/AndroidRuntime(379):  at com.example.sshjdeneme.Connection.<init>(Connection.java:20)
05-22 12:22:10.575: E/AndroidRuntime(379):  at com.example.sshjdeneme.MainActivity.onClick(MainActivity.java:31)
05-22 12:22:10.575: E/AndroidRuntime(379):  at android.view.View.performClick(View.java:2485)
05-22 12:22:10.575: E/AndroidRuntime(379):  at android.view.View$PerformClick.run(View.java:9080)
05-22 12:22:10.575: E/AndroidRuntime(379):  at android.os.Handler.handleCallback(Handler.java:587)
05-22 12:22:10.575: E/AndroidRuntime(379):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-22 12:22:10.575: E/AndroidRuntime(379):  at android.os.Looper.loop(Looper.java:123)
05-22 12:22:10.575: E/AndroidRuntime(379):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-22 12:22:10.575: E/AndroidRuntime(379):  at java.lang.reflect.Method.invokeNative(Native Method)
05-22 12:22:10.575: E/AndroidRuntime(379):  at java.lang.reflect.Method.invoke(Method.java:507)
05-22 12:22:10.575: E/AndroidRuntime(379):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-22 12:22:10.575: E/AndroidRuntime(379):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-22 12:22:10.575: E/AndroidRuntime(379):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

非常确定它只是类路径中缺少的库,基于堆栈跟踪中某些位置的所有异常:

引起:java.lang.NoClassDefFoundError:org.slf4j.LoggerFactory

sshj依赖于slf4j库进行日志记录,我假设您只需下载它并将其放入类路径中。

slf4j可从以下网址获得:http://www.slf4j.org/download.html

您可能希望检查sshj需要哪些其他依赖项,并确保它们也已安装,不确定您使用的是什么版本的sshj,但是您可以找到pom.xml的链接最新版本,列出了sshj的所有当前依赖项: https://github.com/hierynomus/sshj/blob/master/pom.xml

希望这有帮助!

答案 1 :(得分:0)

尝试做一些更改: 在你的xml布局中,在button1的代码中:

<Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1.00"
         android:onClick="configSSH"
        android:text="Config Board" /> 

主要是:

public class MainActivity extends Activity {

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

public void configSSH (View view){
         Context arg0 = null ;
         switch (view.getId()){

            case R.id.configButton :
                new Execute().execute("sudo uvccapture -m");
                 Log.i("Execute","Execute");

                break;
         }
    }
}

这样做,让我知道接下来的事情。