1

I'm trying to send continious commands to raspi from my android phone. I've used the code from this link but I'm getting an error.

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

main :

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");

}

}

connection class:

        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 host class: 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;
    }
    }

Where am i doing wrong ? Thank you

ps: error mesage

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)
Community
  • 1
  • 1
Boreddo
  • 83
  • 1
  • 9

2 Answers2

1

Pretty sure it's just a missing library in your classpath, based on all the exceptions stating at some point in the stack trace:

Caused by: java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory

sshj has a dependency on the slf4j library for logging and I'm assuming you should just need to download it and put it in your classpath.

slf4j is available from: http://www.slf4j.org/download.html

You may wish to check what other dependencies are required for sshj and ensure that they're installed too, not sure what version of sshj you're using, but you can find a link the pom.xml for the latest version, which lists all of the current dependencies for sshj below: https://github.com/hierynomus/sshj/blob/master/pom.xml

Hope this helps!

Dave Birch
  • 469
  • 4
  • 10
0

Try to do some changes : in your xml layout ,in the code of 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" /> 

And in the main :

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

Do this and let me know what next.

Amina
  • 723
  • 8
  • 20