suse11配置ip
重点, 网卡名, mac地址, ip地址/协议, 掩码, 网关
方式1. terminal: YaST2 > Network Devices > Network Settings > Routing.Default Gateway.
方式2. 编辑文件,
network目录下编辑routes文件, 添加网关,
default 132.126.19.1 - -
网卡文件里添加ip地址/24,掩码
方式3: 命令, 重启失效, 没试过
用java8写一个程序,输入字符串命令、是否需要抛出异常、超时时间,返回shell命令的执行返回码,命令输出或报错输出。
以下是实现该功能的Java 8代码,该程序执行Shell命令并处理超时及异常情况:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;
public class CommandExecutor {
public static class CommandResult {
private final int exitCode;
private final String output;
public CommandResult(int exitCode, String output) {
this.exitCode = exitCode;
this.output = output;
}
public int getExitCode() {
return exitCode;
}
public String getOutput() {
return output;
}
}
public static class CommandExecutionException extends Exception {
private final int exitCode;
private final String output;
public CommandExecutionException(int exitCode, String output) {
super("Command exited with code " + exitCode);
this.exitCode = exitCode;
this.output = output;
}
public int getExitCode() {
return exitCode;
}
public String getOutput() {
return output;
}
}
public static class CommandTimeoutException extends Exception {
private final CommandResult result;
public CommandTimeoutException(CommandResult result) {
super("Command timed out");
this.result = result;
}
public CommandResult getResult() {
return result;
}
}
public static CommandResult execute(String command, boolean throwOnError, long timeout, TimeUnit unit)
throws IOException, InterruptedException, CommandExecutionException, CommandTimeoutException {
String[] cmdArray = command.split("\\s+");
ProcessBuilder processBuilder = new ProcessBuilder(cmdArray);
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
StringBuilder output = new StringBuilder();
Thread outputThread = new Thread(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
} catch (IOException e) {
// Handle exception if needed
}
});
outputThread.start();
try {
boolean finished = process.waitFor(timeout, unit);
if (!finished) {
process.destroyForcibly();
process.waitFor(); // Ensure process is terminated
outputThread.join();
CommandResult result = new CommandResult(process.exitValue(), output.toString().trim());
throw new CommandTimeoutException(result);
}
outputThread.join();
int exitCode = process.exitValue();
String outputStr = output.toString().trim();
if (throwOnError && exitCode != 0) {
throw new CommandExecutionException(exitCode, outputStr);
}
return new CommandResult(exitCode, outputStr);
} catch (InterruptedException e) {
process.destroyForcibly();
outputThread.interrupt();
throw e;
}
}
public static void main(String[] args) {
try {
CommandResult result = execute("echo Hello World", false, 5, TimeUnit.SECONDS);
System.out.println("Exit Code: " + result.getExitCode());
System.out.println("Output: " + result.getOutput());
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
### 代码说明:
1. **CommandResult 类**:封装命令执行的返回码和输出结果。
2. **异常类**:
- `CommandExecutionException`:当命令返回非零状态且`throwOnError`为`true`时抛出。
- `CommandTimeoutException`:当命令执行超时时抛出,包含已捕获的输出和返回码。
3. **execute 方法**:
- 使用`ProcessBuilder`启动进程,并合并标准错误流到标准输出流。
- 创建线程异步读取命令输出,避免阻塞。
- 使用`waitFor(timeout, unit)`等待命令执行,超时后强制终止进程。
- 根据参数决定是否在非零返回码时抛出异常。
### 使用示例:
```java
try {
CommandResult result = CommandExecutor.execute("ls -l", true, 2, TimeUnit.SECONDS);
System.out.println(result.getOutput());
} catch (CommandTimeoutException e) {
System.err.println("Timeout: " + e.getResult().getOutput());
} catch (
浙公网安备 33010602011771号