Skip to content

Commit ce2bb7c

Browse files
committed
Change window server command (Get-Content → tail) because of 'Get-Content' only accept in Window powershell
1 parent f30b9e1 commit ce2bb7c

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

src/main/java/root/common/server/implement/JschServer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public String getServerName() {
128128
}
129129

130130
public String executeCommand(String command) throws JSchException, IOException {
131+
log.debug(command);
131132
Channel channel = openExecChannel(command);
132133
InputStream in = connectChannel(channel);
133134
String result = IOUtils.toString(in, "UTF-8");

src/main/java/root/core/repository/implement/WindowServerMonitoringRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public int getAlertLogFileLineCount(AlertLogCommand alc) {
4545
public String checkAlertLog(AlertLogCommand alc) {
4646
String result = "";
4747
try {
48-
String command = String.format("Get-Content %s -Tail %d", alc.getReadFilePath(), alc.getReadLine());
48+
String command = String.format("tail %d %s", alc.getReadLine(), alc.getReadFilePath());
4949
result = jsch.executeCommand(command);
5050
} catch (Exception e) {
5151
log.error(e.getMessage());

src/test/java/root/common/server/implement/JschServerTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
56
import static org.junit.jupiter.api.Assertions.assertNotNull;
67
import static org.junit.jupiter.api.Assertions.assertNull;
78
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -157,11 +158,18 @@ public void testGetServerName() {
157158
String serverName = jsch.getServerName();
158159
assertEquals(serverName, "DKY SERVER");
159160
}
161+
160162
@Test
161-
public void testExecuteCommand() throws JSchException, IOException {
163+
public void testExecuteCommand_EchoCommand() throws JSchException, IOException {
162164
String result = jsch.executeCommand("echo 1");
163165
assertEquals(result, "1");
164166
}
167+
168+
@Test
169+
public void testExecuteCommand_TailCommand() throws JSchException, IOException {
170+
String result = jsch.executeCommand("tail -500 C://Users/aserv/Desktop/alert_DB.log");
171+
assertNotEquals(result, "");
172+
}
165173

166174
@Test
167175
public void testValidateConn_Valid() {

src/test/java/root/core/repository/implement/WindowServerMonitoringRepositoryTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void testCheckAlertLog() throws JSchException, IOException {
103103
alc.setReadLine(10);
104104
alc.setReadFilePath("C:\\alert_DKYDB.log");
105105

106-
String command = String.format("Get-Content %s -Tail %d", alc.getReadFilePath(), alc.getReadLine());
106+
String command = String.format("tail %d %s", alc.getReadLine(), alc.getReadFilePath());
107107
when(jschServer.executeCommand(command)).thenReturn(alertLogString);
108108

109109
// Act
@@ -123,7 +123,7 @@ public void testCheckAlertLogDuringPeriod() throws JSchException, IOException {
123123
String command1 = String.format("find /v /c \"\" %s", alc.getReadFilePath());
124124
when(jschServer.executeCommand(command1)).thenReturn(String.valueOf(alertLogLines.length));
125125

126-
String command2 = String.format("Get-Content %s -Tail %d", alc.getReadFilePath(), alc.getReadLine());
126+
String command2 = String.format("tail %d %s", alc.getReadLine(), alc.getReadFilePath());
127127
when(jschServer.executeCommand(command2)).thenReturn(alertLogString);
128128

129129
// Act
@@ -144,14 +144,14 @@ public void testCheckAlertLogDuringPeriod_ReadLineBiggerThenTotalLineCnt() throw
144144
String command1 = String.format("find /v /c \"\" %s", alc.getReadFilePath());
145145
when(jschServer.executeCommand(command1)).thenReturn("26");
146146

147-
String command2 = String.format("Get-Content %s -Tail %d", alc.getReadFilePath(), alc.getReadLine());
147+
String command2 = String.format("tail %d %s", alc.getReadLine(), alc.getReadFilePath());
148148
StringBuilder builder = new StringBuilder();
149149
for (int i = 0; i < Math.min(alertLogLines.length, alc.getReadLine()); i++) {
150150
builder.append(alertLogLines[i]).append("\n");
151151
}
152152
when(jschServer.executeCommand(command2)).thenReturn(builder.toString());
153153

154-
String command3 = String.format("Get-Content %s -Tail %d", alc.getReadFilePath(), alc.getReadLine() * 2);
154+
String command3 = String.format("tail %d %s", alc.getReadLine() * 2, alc.getReadFilePath());
155155
builder = new StringBuilder();
156156
for (int i = 0; i < Math.min(alertLogLines.length, alc.getReadLine() * 2); i++) {
157157
builder.append(alertLogLines[i]).append("\n");

0 commit comments

Comments
 (0)