Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
N
Nse
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Members
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
吴超
Nse
Commits
929c3830
Commit
929c3830
authored
Sep 01, 2025
by
周海峰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化
parent
65cde3f0
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
48 行增加
和
25 行删除
+48
-25
src/main/java/com/chenyang/nse/bussiness/controller/ip/ChangeIPController.java
+45
-22
src/main/resources/config.properties
+3
-3
没有找到文件。
src/main/java/com/chenyang/nse/bussiness/controller/ip/ChangeIPController.java
View file @
929c3830
...
...
@@ -5,6 +5,7 @@ import com.chenyang.nse.bussiness.commmon.json.Response;
import
com.chenyang.nse.bussiness.entity.ip.Ip
;
import
com.chenyang.nse.bussiness.tools.command.CommandTool
;
import
com.chenyang.nse.bussiness.tools.msg.MesUtil
;
import
java.io.BufferedReader
;
import
java.io.BufferedWriter
;
import
java.io.File
;
...
...
@@ -23,6 +24,7 @@ import java.util.Enumeration;
import
java.util.List
;
import
java.util.Map
;
import
java.util.regex.Pattern
;
import
org.apache.commons.lang.StringUtils
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
...
...
@@ -47,17 +49,17 @@ public class ChangeIPController {
@PostMapping
({
"/update"
})
public
Response
update
(
@RequestBody
Map
<
String
,
String
>
param
)
throws
UnknownHostException
{
String
networkInterfaceName
=
(
String
)
param
.
get
(
"networkInterface"
);
String
networkInterfaceName
=
(
String
)
param
.
get
(
"networkInterface"
);
if
(
StringUtils
.
isEmpty
(
networkInterfaceName
))
{
return
RespHelper
.
createResp
(
false
,
"IP_ERR_001"
,
MesUtil
.
getMsg
(
"IP_ERR_001"
,
new
String
[
0
]));
}
else
{
String
newIpAddress
=
(
String
)
param
.
get
(
"inetAddress"
);
String
newIpAddress
=
(
String
)
param
.
get
(
"inetAddress"
);
if
(
StringUtils
.
isEmpty
(
newIpAddress
))
{
return
RespHelper
.
createResp
(
false
,
"IP_ERR_002"
,
MesUtil
.
getMsg
(
"IP_ERR_002"
,
new
String
[
0
]));
}
else
if
(!
this
.
validateIP
(
newIpAddress
))
{
return
RespHelper
.
createResp
(
false
,
"IP_ERR_004"
,
MesUtil
.
getMsg
(
"IP_ERR_004"
,
new
String
[
0
]));
}
else
{
String
newSubnetMask
=
(
String
)
param
.
get
(
"subnetMask"
);
String
newSubnetMask
=
(
String
)
param
.
get
(
"subnetMask"
);
if
(
StringUtils
.
isEmpty
(
newSubnetMask
))
{
return
RespHelper
.
createResp
(
false
,
"IP_ERR_003"
,
MesUtil
.
getMsg
(
"IP_ERR_003"
,
new
String
[
0
]));
}
else
if
(!
this
.
validateSubnetMask
(
newSubnetMask
))
{
...
...
@@ -82,13 +84,13 @@ public class ChangeIPController {
try
{
Enumeration
<
NetworkInterface
>
networkInterfaces
=
NetworkInterface
.
getNetworkInterfaces
();
while
(
networkInterfaces
.
hasMoreElements
())
{
NetworkInterface
networkInterface
=
(
NetworkInterface
)
networkInterfaces
.
nextElement
();
while
(
networkInterfaces
.
hasMoreElements
())
{
NetworkInterface
networkInterface
=
(
NetworkInterface
)
networkInterfaces
.
nextElement
();
if
(!
networkInterface
.
isLoopback
()
&&
!
networkInterface
.
isVirtual
())
{
Enumeration
<
InetAddress
>
inetAddresses
=
networkInterface
.
getInetAddresses
();
while
(
inetAddresses
.
hasMoreElements
())
{
InetAddress
inetAddress
=
(
InetAddress
)
inetAddresses
.
nextElement
();
while
(
inetAddresses
.
hasMoreElements
())
{
InetAddress
inetAddress
=
(
InetAddress
)
inetAddresses
.
nextElement
();
String
subnetMask
=
this
.
getSubnetMask
(
networkInterface
);
if
(
inetAddress
.
getAddress
().
length
==
4
)
{
Ip
ip
=
new
Ip
();
...
...
@@ -112,7 +114,7 @@ public class ChangeIPController {
}
private
String
getSubnetMask
(
NetworkInterface
networkInterface
)
throws
SocketException
{
for
(
InterfaceAddress
address
:
networkInterface
.
getInterfaceAddresses
())
{
for
(
InterfaceAddress
address
:
networkInterface
.
getInterfaceAddresses
())
{
if
(
address
.
getAddress
()
instanceof
Inet4Address
)
{
int
maskLength
=
address
.
getNetworkPrefixLength
();
int
mask
=
-
1
<<
32
-
maskLength
;
...
...
@@ -125,11 +127,11 @@ public class ChangeIPController {
}
public
void
changeIpFromFile
(
Map
<
String
,
String
>
params
)
{
String
filePath
=
"/etc/sysconfig/network-scripts/ifcfg-"
+
(
String
)
params
.
get
(
"networkInterface"
);
String
newIpAddress
=
(
String
)
params
.
get
(
"inetAddress"
);
String
newSubnetMask
=
(
String
)
params
.
get
(
"subnetMask"
);
String
gateway
=
(
String
)
params
.
get
(
"gateway"
);
String
dns
=
(
String
)
params
.
get
(
"dns"
);
String
filePath
=
"/etc/sysconfig/network-scripts/ifcfg-"
+
(
String
)
params
.
get
(
"networkInterface"
);
String
newIpAddress
=
(
String
)
params
.
get
(
"inetAddress"
);
String
newSubnetMask
=
(
String
)
params
.
get
(
"subnetMask"
);
String
gateway
=
(
String
)
params
.
get
(
"gateway"
);
String
dns
=
(
String
)
params
.
get
(
"dns"
);
try
{
CommandTool
.
command
(
"cp "
+
filePath
+
" "
+
filePath
+
".bak"
);
...
...
@@ -139,7 +141,7 @@ public class ChangeIPController {
BufferedWriter
writer
=
new
BufferedWriter
(
new
FileWriter
(
tempFile
));
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
while
((
line
=
reader
.
readLine
())
!=
null
)
{
if
(
line
.
startsWith
(
"IPADDR=\""
))
{
line
=
"IPADDR=\""
+
newIpAddress
+
"\""
;
}
else
if
(
line
.
startsWith
(
"PREFIX=\""
))
{
...
...
@@ -170,8 +172,8 @@ public class ChangeIPController {
byte
[]
addressBytes
=
inetAddress
.
getAddress
();
int
cidrNotation
=
0
;
for
(
byte
b
:
addressBytes
)
{
for
(
int
i
=
7
;
i
>=
0
;
--
i
)
{
for
(
byte
b
:
addressBytes
)
{
for
(
int
i
=
7
;
i
>=
0
;
--
i
)
{
if
((
b
>>
i
&
1
)
==
0
)
{
return
cidrNotation
;
}
...
...
@@ -194,11 +196,32 @@ public class ChangeIPController {
}
private
String
getDefaultGateway
(
NetworkInterface
networkInterface
)
throws
Exception
{
Process
process
=
Runtime
.
getRuntime
().
exec
(
"ip route show dev "
+
networkInterface
.
getName
());
String
os
=
System
.
getProperty
(
"os.name"
).
toLowerCase
();
Process
process
=
null
;
if
(
os
.
contains
(
"mac"
))
{
// macOS 获取默认网关
process
=
Runtime
.
getRuntime
().
exec
(
"netstat -rn"
);
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
process
.
getInputStream
()));
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
while
((
line
=
reader
.
readLine
())
!=
null
)
{
if
(
line
.
startsWith
(
"default"
))
{
String
[]
parts
=
line
.
split
(
"\\s+"
);
if
(
parts
.
length
>=
2
)
{
String
gateway
=
parts
[
1
];
reader
.
close
();
process
.
destroy
();
return
gateway
;
}
}
}
reader
.
close
();
process
.
destroy
();
}
else
{
// Linux 获取默认网关
process
=
Runtime
.
getRuntime
().
exec
(
"ip route show dev "
+
networkInterface
.
getName
());
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
process
.
getInputStream
()));
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
if
(
line
.
startsWith
(
"default"
))
{
String
[]
parts
=
line
.
split
(
"\\s+"
);
String
gateway
=
parts
[
2
];
...
...
@@ -207,9 +230,9 @@ public class ChangeIPController {
return
gateway
;
}
}
reader
.
close
();
process
.
destroy
();
}
return
null
;
}
...
...
@@ -219,7 +242,7 @@ public class ChangeIPController {
StringBuilder
dnsServers
=
new
StringBuilder
();
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
while
((
line
=
reader
.
readLine
())
!=
null
)
{
if
(
line
.
startsWith
(
"nameserver"
))
{
String
[]
parts
=
line
.
split
(
"\\s+"
);
if
(
parts
.
length
>=
2
)
{
...
...
@@ -247,7 +270,7 @@ public class ChangeIPController {
InetAddress
[]
addresses
=
InetAddress
.
getAllByName
(
hostName
);
System
.
out
.
println
(
"DNS IP 地址:"
);
for
(
InetAddress
address
:
addresses
)
{
for
(
InetAddress
address
:
addresses
)
{
System
.
out
.
println
(
address
.
getHostAddress
());
}
}
catch
(
UnknownHostException
e
)
{
...
...
src/main/resources/config.properties
View file @
929c3830
...
...
@@ -14,8 +14,8 @@ jdbc.kingbase.dbname=security
#如果配置数据库类型是H2,则jdbc.dbname的值是public
jdbc.dbname
=
public
#jdbc.dbname=encryption
#
basePath=/home/trustz
basePath
=
D:/trustz
basePath
=
/home/trustz
#
basePath=D:/trustz
maskingPath
=
/home/masking
#jdbc-mysql
#jdbc.driver=com.mysql.cj.jdbc.Driver
...
...
@@ -47,7 +47,7 @@ jdbc.dialect=org.hibernate.dialect.MySQL5Dialect
[全局参数]
#license文件路径
#licensedir=${basePath}/data/infa_file/lic
licensedir
=
D:/lic
licensedir
=
/Users/zhouhaifeng/Desktop
#是否启用license licenseKey 0:否 1 是
isNeedLicenseKey
=
0
#错误登录允许次数
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论