Commit 707266d6 by huyi

修复ssh读取出错

parent 9f548f13
connectionTimeout=60000
returnTimeout=1000
\ No newline at end of file
connectionTimeout=60000
\ No newline at end of file
......@@ -74,6 +74,11 @@
<artifactId>jsch</artifactId>
<version>0.1.51</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
</dependencies>
<build>
......
......@@ -13,9 +13,9 @@ public class FaultLocationController {
@Autowired
private FaultLocationService faultLocationService;
@GetMapping (value = "test")
@GetMapping (value = "/test")
public void testConnection () {
System.out.println("有请求访问ssh连接");
System.out.println(faultLocationService.test());
faultLocationService.test();
}
}
\ No newline at end of file
package com.fhkj.oltinspection.pojo;
public class TelnetConnection {
private String ip;
private Integer port;
private String username;
private String password;
public TelnetConnection() {
super();
}
/**
* @return the ip
*/
public String getIp() {
return ip;
}
/**
* @param ip the ip to set
*/
public void setIp(String ip) {
this.ip = ip;
}
/**
* @return the port
*/
public Integer getPort() {
return port;
}
/**
* @param port the port to set
*/
public void setPort(Integer port) {
this.port = port;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
\ No newline at end of file
package com.fhkj.oltinspection.service.impl;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import javax.transaction.Transactional;
import com.fhkj.oltinspection.pojo.Result;
......@@ -15,7 +18,7 @@ public class FaultLocationServiceImpl implements FaultLocationService {
@Override
public Result test() {
SshConnection sshConnection = new SshConnection("192.168.21.8", 22, "root", "root");
SshConnection sshConnection = new SshConnection("10.109.21.8", 22, "GEPON", "GEPON");
SSHUtil sshUtil = new SSHUtil(sshConnection);
Result result = null;
String data = null;
......@@ -24,19 +27,25 @@ public class FaultLocationServiceImpl implements FaultLocationService {
return result;
}
result = sshUtil.initInputAndOutput();
if ((boolean) result.getState()) {
if (!(boolean) result.getState()) {
return result;
}
result = sshUtil.sendCommand("showcard", true);
if ((boolean) result.getState()) {
result = sshUtil.sendCommand("showcard", true, 5000L);
if (!(boolean) result.getState()) {
return result;
}
data = (String) result.getData();
result = sshUtil.sshExit();
if ((boolean) result.getState()) {
System.out.println(data);
result = sshUtil.sendCommand("show version", true, 5000L);
if (!(boolean) result.getState()) {
return result;
}
data = (String) result.getData();
System.out.println(data);
result = sshUtil.sshExit();
if (!(boolean) result.getState()) {
return result;
}
return result;
}
......
......@@ -20,11 +20,8 @@ public class ReadProperties {
bufferedReader = new BufferedReader(new FileReader(file));
properties.load(bufferedReader);
int connectionTimeout = Integer.parseInt(properties.getProperty("connectionTimeout"));
long returnTimeout = Long.parseLong(properties.getProperty("returnTimeout"));
sshConfigMap.put ("connectionTimeout", connectionTimeout);
sshConfigMap.put ("returnTimeout", returnTimeout);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
......
......@@ -130,6 +130,9 @@ public class SSHUtil {
try {
this.ins = channel.getInputStream();
this.dos = new DataOutputStream(channel.getOutputStream());
this.dos.writeBytes("cd service" + "\r\n");
this.dos.writeBytes("terminal length 0" + "\r\n");
this.dos.writeBytes("cd .." + "\r\n");
} catch (IOException e) {
return new Result("开启成功", PackageExceptionUtil.exceptionToString(e), false);
}
......@@ -137,19 +140,20 @@ public class SSHUtil {
}
/**
* @param command(命令),isRead(是否读取设备对该命令的返回)
* @param command(命令),isRead(是否读取设备对该命令的返回),timeout(该命令最长等待放回时间)
* @return Result
* @see 用这个方法向设备输入命令,参数command是输入的命令,不需要加\r\n,已经加上了,
* isRead代表是否读取设备的返回,true的时候会返回
* isRead代表是否读取设备的返回,true的时候会返回,timeout代表最长等待时间
*/
public Result sendCommand(String command, boolean isRead) {
public Result sendCommand(String command, boolean isRead, long timeout) {
if (null == this.ins || null == this.dos) {
throw new OltException("请先调用initInputAndOutput()方法初始化输入输出流");
}
try {
this.dos.writeBytes(command + "\r\n"); // 输入命令
this.dos.flush();
} catch (IOException e) {
Thread.sleep(2000);
} catch (IOException | InterruptedException e) {
return new Result("命令输入异常", PackageExceptionUtil.exceptionToString(e), false);
}
......@@ -158,10 +162,9 @@ public class SSHUtil {
try {
byte[] buff = new byte[1024];
int resRead = 0;
long time = (long) sshConfigMap.get("returnTimeout");
long begin = System.currentTimeMillis();
while (true) {
if (System.currentTimeMillis() > begin + time) {
if (System.currentTimeMillis() > begin + timeout) {
throw new TimeoutException("read time out");
}
int len = this.ins.available();
......@@ -171,17 +174,19 @@ public class SSHUtil {
String str = new String(buff, 0, resRead);
sb.append(str);
if (sb.toString().trim().endsWith("#")) {
this.dos.writeBytes("clear" + "\r\n"); // 输入命令
this.dos.flush();
break;
}
}
} else {
Thread.sleep(100);
Thread.sleep(1000);
}
}
} catch (TimeoutException | InterruptedException | IOException e) {
} catch (IOException | InterruptedException | TimeoutException e) {
return new Result("读取失败", PackageExceptionUtil.exceptionToString(e), false);
}
return new Result("读取成功", sb.toString(), false);
return new Result("读取成功", sb.toString(), true);
} else {
return new Result("输入成功", true);
}
......@@ -199,8 +204,6 @@ public class SSHUtil {
this.ins.close();
this.channel = null;
this.sshConnection = null;
this.sshConfigMap.clear();
this.sshConfigMap = null;
System.gc();
ClearUtil.openExe();
} catch (IOException e) {
......
package com.fhkj.oltinspection.util;
import com.fhkj.oltinspection.pojo.Result;
import org.apache.commons.net.telnet.TelnetClient;
public class TelnetUtil {
private TelnetClient telnetClient;
public Result loginTelnet () {
return new Result ();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment