Commit 707266d6 by huyi

修复ssh读取出错

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