Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
oltinspection
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
胡懿
oltinspection
Commits
707266d6
Commit
707266d6
authored
Mar 12, 2019
by
huyi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复ssh读取出错
parent
9f548f13
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
124 additions
and
25 deletions
+124
-25
sshConnection.properties
oltConfig/sshConnection.properties
+2
-3
pom.xml
pom.xml
+5
-0
FaultLocationController.java
...hkj/oltinspection/controller/FaultLocationController.java
+3
-2
TelnetConnection.java
...in/java/com/fhkj/oltinspection/pojo/TelnetConnection.java
+69
-0
FaultLocationServiceImpl.java
.../oltinspection/service/impl/FaultLocationServiceImpl.java
+15
-6
ReadProperties.java
...main/java/com/fhkj/oltinspection/util/ReadProperties.java
+0
-3
SSHUtil.java
src/main/java/com/fhkj/oltinspection/util/SSHUtil.java
+14
-11
TelnetUtil.java
src/main/java/com/fhkj/oltinspection/util/TelnetUtil.java
+16
-0
No files found.
oltConfig/sshConnection.properties
View file @
707266d6
connectionTimeout
=
60000
returnTimeout
=
1000
\ No newline at end of file
connectionTimeout
=
60000
\ No newline at end of file
pom.xml
View file @
707266d6
...
...
@@ -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>
...
...
src/main/java/com/fhkj/oltinspection/controller/FaultLocationController.java
View file @
707266d6
...
...
@@ -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
src/main/java/com/fhkj/oltinspection/pojo/TelnetConnection.java
0 → 100644
View file @
707266d6
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
src/main/java/com/fhkj/oltinspection/service/impl/FaultLocationServiceImpl.java
View file @
707266d6
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
(
"1
92.168.21.8"
,
22
,
"root"
,
"root
"
);
SshConnection
sshConnection
=
new
SshConnection
(
"1
0.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
;
}
...
...
src/main/java/com/fhkj/oltinspection/util/ReadProperties.java
View file @
707266d6
...
...
@@ -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
();
}
}
...
...
src/main/java/com/fhkj/oltinspection/util/SSHUtil.java
View file @
707266d6
...
...
@@ -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
+
time
out
)
{
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
(
100
0
);
}
}
}
catch
(
TimeoutException
|
InterruptedException
|
IO
Exception
e
)
{
}
catch
(
IOException
|
InterruptedException
|
Timeout
Exception
e
)
{
return
new
Result
(
"读取失败"
,
PackageExceptionUtil
.
exceptionToString
(
e
),
false
);
}
return
new
Result
(
"读取成功"
,
sb
.
toString
(),
fals
e
);
return
new
Result
(
"读取成功"
,
sb
.
toString
(),
tru
e
);
}
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
)
{
...
...
src/main/java/com/fhkj/oltinspection/util/TelnetUtil.java
0 → 100644
View file @
707266d6
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment