Commit c9455de3 by huyi

修改shh登录结果返回信息的对象

parent 44634a1f
No preview for this file type
connectionTimeout=60000 connectionTimeout=60000
returnTimeout=1000L returnTimeout=1000
\ No newline at end of file \ No newline at end of file
package com.fhkj.oltinspection.controller;
import com.fhkj.oltinspection.service.FaultLocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping (value = "/faultLocation")
public class FaultLocationController {
@Autowired
private FaultLocationService faultLocationService;
@GetMapping (value = "test")
public void testConnection () {
System.out.println("有请求访问ssh连接");
System.out.println(faultLocationService.test());
}
}
\ No newline at end of file
...@@ -12,14 +12,16 @@ public class Result implements Serializable { ...@@ -12,14 +12,16 @@ public class Result implements Serializable {
private Integer code; private Integer code;
private String msg; private String msg;
private Object data; private Object data;
private boolean state;
public Result () { public Result() {
super (); super();
this.code = null; this.code = null;
this.msg = ""; this.msg = "";
this.data = null; this.data = null;
} }
public Result (Integer code) {
public Result(Integer code) {
this.code = code; this.code = code;
} }
public Result (String msg) { public Result (String msg) {
...@@ -28,6 +30,9 @@ public class Result implements Serializable { ...@@ -28,6 +30,9 @@ public class Result implements Serializable {
public Result (Object data) { public Result (Object data) {
this.data = data; this.data = data;
} }
public Result (boolean state) {
this.state = state;
}
public Result (Integer code, String msg) { public Result (Integer code, String msg) {
this.code = code; this.code = code;
this.msg = msg; this.msg = msg;
...@@ -40,12 +45,20 @@ public class Result implements Serializable { ...@@ -40,12 +45,20 @@ public class Result implements Serializable {
this.msg = msg; this.msg = msg;
this.data = data; this.data = data;
} }
public Result (String msg, boolean state) {
this.msg = msg;
this.state = state;
}
public Result (Integer code, String msg, Object data) { public Result (Integer code, String msg, Object data) {
this.code = code; this.code = code;
this.msg = msg; this.msg = msg;
this.data = data; this.data = data;
} }
public Result (String msg, Object data, boolean state) {
this.msg = msg;
this.data = data;
this.state = state;
}
/** /**
* @return the code * @return the code
*/ */
...@@ -83,10 +96,23 @@ public class Result implements Serializable { ...@@ -83,10 +96,23 @@ public class Result implements Serializable {
public void setData(Object data) { public void setData(Object data) {
this.data = data; this.data = data;
} }
/**
* @return the state
*/
public boolean getState() {
return state;
}
/**
* @param state the state to set
*/
public void setState(boolean state) {
this.state = state;
}
@Override @Override
public String toString () { public String toString () {
return "code:" + this.code + ", msg:" + this.msg + ", data:" + this.data; return "code:" + this.code + ", msg:" + this.msg + ", data:" + this.data + ",state:" + this.state;
} }
} }
\ No newline at end of file
package com.fhkj.oltinspection.service;
import com.fhkj.oltinspection.pojo.Result;
public interface FaultLocationService {
public Result test ();
}
\ No newline at end of file
package com.fhkj.oltinspection.service.impl;
import javax.transaction.Transactional;
import com.fhkj.oltinspection.pojo.Result;
import com.fhkj.oltinspection.pojo.SshConnection;
import com.fhkj.oltinspection.service.FaultLocationService;
import com.fhkj.oltinspection.util.SSHUtil;
import org.springframework.stereotype.Service;
@Service
@Transactional
public class FaultLocationServiceImpl implements FaultLocationService {
@Override
public Result test() {
SshConnection sshConnection = new SshConnection("192.168.21.8", 22, "root", "root");
SSHUtil sshUtil = new SSHUtil(sshConnection);
Result result = null;
String data = null;
result = sshUtil.initConnection();
if (!(boolean) result.getState()) {
return result;
}
result = sshUtil.initInputAndOutput();
if ((boolean) result.getState()) {
return result;
}
result = sshUtil.sendCommand("showcard", true);
if ((boolean) result.getState()) {
return result;
}
data = (String) result.getData();
result = sshUtil.sshExit();
if ((boolean) result.getState()) {
return result;
}
System.out.println(data);
return result;
}
}
\ No newline at end of file
...@@ -47,7 +47,7 @@ public class FtpServiceImpl implements FtpService { ...@@ -47,7 +47,7 @@ public class FtpServiceImpl implements FtpService {
return new Result ("保存成功", true); return new Result ("保存成功", true);
} }
} catch (Exception e) { } catch (Exception e) {
return new Result ("保存失败", PackageExceptionUtil.exceptionToString(e)); return new Result ("保存失败", PackageExceptionUtil.exceptionToString(e), false);
} }
} }
......
...@@ -59,7 +59,7 @@ public class FTPUtil { ...@@ -59,7 +59,7 @@ public class FTPUtil {
return new Result("在数据库中未能获取到ftp的连接配置信息", false); return new Result("在数据库中未能获取到ftp的连接配置信息", false);
} else { } else {
Result result = validateFtpParameter(ftpConfigEntity); Result result = validateFtpParameter(ftpConfigEntity);
if (!(boolean)result.getData()) { if (!(boolean)result.getState()) {
return result; return result;
} }
} }
...@@ -78,8 +78,8 @@ public class FTPUtil { ...@@ -78,8 +78,8 @@ public class FTPUtil {
ftpClient.disconnect(); // 关闭连接 ftpClient.disconnect(); // 关闭连接
} }
} catch (IOException e) { } catch (IOException e) {
return new Result("出现连接异常", PackageExceptionUtil.exceptionToString(e)); return new Result("出现连接异常", PackageExceptionUtil.exceptionToString(e), false);
} }
return new Result(); return new Result("连接成功", true);
} }
} }
\ No newline at end of file
...@@ -4,16 +4,17 @@ import java.io.BufferedReader; ...@@ -4,16 +4,17 @@ import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
public class ReadProperties { public class ReadProperties {
public static Map<String, Object> sshConfigMap = null; public static Map<String, Object> sshConfigMap = new HashMap<String, Object> ();
static { static {
try { try {
File file = ResourceUtils.getFile("ptnConfig/performancetime.properties"); File file = ResourceUtils.getFile("oltConfig/sshConnection.properties");
Properties properties = new Properties(); Properties properties = new Properties();
BufferedReader bufferedReader; BufferedReader bufferedReader;
bufferedReader = new BufferedReader(new FileReader(file)); bufferedReader = new BufferedReader(new FileReader(file));
......
...@@ -37,7 +37,7 @@ public class SSHUtil { ...@@ -37,7 +37,7 @@ public class SSHUtil {
public SSHUtil(SshConnection sshConnection) { public SSHUtil(SshConnection sshConnection) {
super(); super();
Result result = validateSshConnectionParameter(sshConnection); Result result = validateSshConnectionParameter(sshConnection);
if (!(boolean) result.getData()) { if (!(boolean) result.getState()) {
throw new OltException(result.getMsg()); throw new OltException(result.getMsg());
} }
this.sshConnection = sshConnection; this.sshConnection = sshConnection;
...@@ -115,7 +115,7 @@ public class SSHUtil { ...@@ -115,7 +115,7 @@ public class SSHUtil {
((ChannelShell) channel).setPtyType("vt100", 80, 100, 640, 960); ((ChannelShell) channel).setPtyType("vt100", 80, 100, 640, 960);
channel.connect(); channel.connect();
} catch (JSchException e) { } catch (JSchException e) {
return new Result("连接异常", PackageExceptionUtil.exceptionToString(e)); return new Result("连接异常", PackageExceptionUtil.exceptionToString(e), false);
} }
return new Result("连接成功", true); return new Result("连接成功", true);
} }
...@@ -131,7 +131,7 @@ public class SSHUtil { ...@@ -131,7 +131,7 @@ public class SSHUtil {
this.ins = channel.getInputStream(); this.ins = channel.getInputStream();
this.dos = new DataOutputStream(channel.getOutputStream()); this.dos = new DataOutputStream(channel.getOutputStream());
} catch (IOException e) { } catch (IOException e) {
return new Result("开启成功", PackageExceptionUtil.exceptionToString(e)); return new Result("开启成功", PackageExceptionUtil.exceptionToString(e), false);
} }
return new Result("开启成功", true); return new Result("开启成功", true);
} }
...@@ -150,7 +150,7 @@ public class SSHUtil { ...@@ -150,7 +150,7 @@ public class SSHUtil {
this.dos.writeBytes(command + "\r\n"); // 输入命令 this.dos.writeBytes(command + "\r\n"); // 输入命令
this.dos.flush(); this.dos.flush();
} catch (IOException e) { } catch (IOException e) {
return new Result("命令输入异常", PackageExceptionUtil.exceptionToString(e)); return new Result("命令输入异常", PackageExceptionUtil.exceptionToString(e), false);
} }
if (isRead) { // isRead是ture时读取设备返回的内容 if (isRead) { // isRead是ture时读取设备返回的内容
...@@ -179,9 +179,9 @@ public class SSHUtil { ...@@ -179,9 +179,9 @@ public class SSHUtil {
} }
} }
} catch (TimeoutException | InterruptedException | IOException e) { } catch (TimeoutException | InterruptedException | IOException e) {
return new Result("读取失败", PackageExceptionUtil.exceptionToString(e)); return new Result("读取失败", PackageExceptionUtil.exceptionToString(e), false);
} }
return new Result("读取成功", sb.toString()); return new Result("读取成功", sb.toString(), false);
} else { } else {
return new Result("输入成功", true); return new Result("输入成功", true);
} }
...@@ -204,7 +204,7 @@ public class SSHUtil { ...@@ -204,7 +204,7 @@ public class SSHUtil {
System.gc(); System.gc();
ClearUtil.openExe(); ClearUtil.openExe();
} catch (IOException e) { } catch (IOException e) {
return new Result("退出失败", PackageExceptionUtil.exceptionToString(e)); return new Result("退出失败", PackageExceptionUtil.exceptionToString(e), false);
} }
return new Result("退出成功", true); return new Result("退出成功", true);
} }
......
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