我有以下代码:
JSch jsch = new JSch();
jsch.setKnownHosts(dotSshDir + "/known_hosts");
jsch.addIdentity(dotSshDir + "/id_rsa");
Session session = jsch.getSession(userName, hostname, 22);
session.connect();
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
channel.setInputStream(null);
channel.setErrStream(System.err);
Reader reader = new InputStreamReader(channel.getInputStream());
char[] buf = new char[1024];
int numRead;
while ((numRead = reader.read(buf)) != -1) {
String readData = String.valueOf(buf, 0, numRead);
result.append(readData);
buf = new char[1024];
}
Run Code Online (Sandbox Code Playgroud)
它试图从读者那里读取.我该如何解决?我该怎么做才能找到正在发生的事情?
正如来自这个问题和本教程的用户所解释的那样,我正在尝试设置一个简单的 ssh 连接以在应用程序上执行单个命令。它甚至不需要等待任何响应。
这是代码:
主要活动:包 com.example.lucas.shutdown;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onCLick(View v){
new AsyncTask<Integer, Void, Void>(){
@Override
protected Void doInBackground(Integer... params) {
try {
executeRemoteShutdown();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(1);
}
public void executeRemoteShutdown(){
String user = "ssh";
String password = …Run Code Online (Sandbox Code Playgroud)