Java 在android应用程序中实现JSch运行ssh命令
我目前正在尝试编写一个android应用程序,通过ssh向远程机器发送一个简单的“ls”命令。我尝试过使用JSch和,但它似乎不起作用。我环顾四周,似乎必须实现一些异步任务才能运行代码。以下是当前主要活动的完整代码:Java 在android应用程序中实现JSch运行ssh命令,java,android,ssh,android-asynctask,jsch,Java,Android,Ssh,Android Asynctask,Jsch,我目前正在尝试编写一个android应用程序,通过ssh向远程机器发送一个简单的“ls”命令。我尝试过使用JSch和,但它似乎不起作用。我环顾四周,似乎必须实现一些异步任务才能运行代码。以下是当前主要活动的完整代码: package com.sshtest.sshtest; import android.app.Activity; import android.os.Bundle; import android.os.AsyncTask; import android.view.Menu; i
package com.sshtest.sshtest;
import android.app.Activity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import java.util.Properties;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.*;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.lang.String;
public class MainActivity extends Activity {
String string;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private class sshconnection extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession("name", "ipaddress", 22);
session.setPassword("password");
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
ChannelExec channel = (ChannelExec)session.openChannel("exec");
channel.setCommand("ls");
channel.connect();
InputStream input = channel.getInputStream();
channel.disconnect();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String result) {
TextView txt = (TextView) findViewById(R.id.txtipinfo);
txt.setText("Executed");
}
}
package com.sshtest.sshtest;
导入android.app.Activity;
导入android.os.Bundle;
导入android.os.AsyncTask;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.TextView;
导入java.util.Properties;
导入com.jcraft.jsch.jsch;
导入com.jcraft.jsch.Session;
导入com.jcraft.jsch.Channel;
导入com.jcraft.jsch.ChannelExec;
导入com.jcraft.jsch.*;
导入java.io.InputStream;
导入java.io.ByteArrayOutputStream;
导入java.lang.String;
公共类MainActivity扩展了活动{
字符串字符串;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
私有类sshconnection扩展异步任务{
受保护的字符串doInBackground(字符串…参数){
试一试{
JSch JSch=新的JSch();
Session Session=jsch.getSession(“name”,“ipaddress”,22);
session.setPassword(“密码”);
//避免要求钥匙确认
Properties prop=新属性();
道具放置(“检查”、“否”);
session.setConfig(prop);
session.connect();
ChannelExec channel=(ChannelExec)session.openChannel(“exec”);
channel.setCommand(“ls”);
channel.connect();
InputStream输入=channel.getInputStream();
通道断开();
}捕获(例外e){
System.out.println(e.getMessage());
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
TextView txt=(TextView)findViewById(R.id.txtipinfo);
txt.setText(“已执行”);
}
}
- 添加使用权限android:name=“android.permission.INTERNET”到AndroidManifest.xml
这是我第一次尝试任何严肃的编码,如果有人能帮忙的话,我将不胜感激。谢谢!如果你只是想进行测试。尽量不要使用AsyncTask,而只是一个线程…就像这样
new Thread(new Runnable() {
@Override
public void run() {
Session session;
JSch jsch;
try {
jsch = new JSch();
jsch.addIdentity(privateKey, "yourprivatekey");
session = jsch.getSession("git", "github.com", 22);
session.setPassword("yourpass");
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
if(session.isConnected()){
System.out.println(this.getClass().getSimpleName() + " CONNECTED");
System.out.println(this.getClass().getSimpleName() + " YOO " + jsch.getIdentityRepository().getName()+" "+session.getClientVersion() + " " + session.isConnected());
}else{
System.out.println(this.getClass().getSimpleName() + " NOT CONNECTED");
}
}catch (Exception e){
e.printStackTrace();
}
}
}).start();
在主活动的onCreate()方法中调用它。
希望能有帮助!你把你的
异步任务称为哪里?
?啊,它在onCreate“sshconnection.execute();”中,但我得到了一个非静态引用错误,我一直在努力解决它。