您好我想编写一个程序,通过Ssh协议将手机连接到路由器并使用jsch库。
但连接时会显示此错误。
05-06 05:53:58.754 3985-4006/com.example.amir133.tel W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe3031c00, error=EGL_SUCCESS
05-06 05:54:08.692 3985-4046/com.example.amir133.tel W/System.err: com.jcraft.jsch.JSchException: timeout: socket is not established
at com.jcraft.jsch.Util.createSocket(Util.java:394)
at com.jcraft.jsch.Session.connect(Session.java:215)
at com.jcraft.jsch.Session.connect(Session.java:183)
at com.example.amir133.tel.SSHManager.execute(SSHManager.java:89)
at com.example.amir133.tel.MainActivity$1$1.doInBackground(MainActivity.java:52)
at com.example.amir133.tel.MainActivity$1$1.doInBackground(MainActivity.java:47)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
我检查了与this和this相同的问题,但没有一个问题解决了我的问题。
我加入了显示互联网访问。
通过putty,我可以连接到此路由器,因此路由器没有问题。
有人可以帮忙吗?
MainClass:
connect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AsyncTask<Integer, Void, Void>(){
@Override
protected Void doInBackground(Integer... params) {
try {
SSHManager ssh=new SSHManager("amir","123","192.168.66.120");
ssh.execute();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(1);
}
});
SSHManager类:
public class SSHManager {
private JSch jsch;
private String hostname = null;
private String username;
private String password;
private Session session;
private ChannelExec channel;
private int connectioPort = 22;
private void SSHManager (String userName,
String password, String connectionIP) {
this.username = userName;
this.password = password;
this.hostname = connectionIP;
}
public String execute() {
try {
jsch = new JSch();
session = jsch.getSession(username, hostname, 22);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.setTimeout(10000);
session.connect();
channel = (ChannelExec) session.openChannel("exec");
channel.setCommand("en\n123\nconf t\nexit");
channel.connect();
channel.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
答案 0 :(得分:0)
试试这个:
jsch = new JSch();
session = jsch.getSession(UserName, HostName, PortNumber);
session.setPassword(PassWord);
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
channel = (Channel) session.openChannel("shell");
channel.connect();
channel.disconnect();
更新:
有时您应该设置“属性”值,然后将其添加到会话中。