feat: 项目初始化
This commit is contained in:
53
cmd/dhcp.go
Normal file
53
cmd/dhcp.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
stdnet "net"
|
||||
|
||||
"netcli/internal/constants"
|
||||
"netcli/internal/net"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// dhcpCmd represents the dhcp command
|
||||
var dhcpCmd = &cobra.Command{
|
||||
Use: "dhcp",
|
||||
Short: "将指定网卡改为 DHCP 自动获取 IP",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
// 检查管理员权限
|
||||
if !net.IsAdmin() {
|
||||
fmt.Println("程序需要管理员权限,尝试提升...")
|
||||
return net.RunAsAdmin()
|
||||
}
|
||||
|
||||
// 获取所有网卡
|
||||
ifaces, err := stdnet.Interfaces()
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取网卡失败: %w", err)
|
||||
}
|
||||
|
||||
var nics []net.NetIfWrap
|
||||
for _, i := range ifaces {
|
||||
nics = append(nics, net.NetIfWrap{i})
|
||||
}
|
||||
|
||||
// 用户选择网卡
|
||||
nic, err := net.ChooseNic(nics, constants.DefaultDHCPMsg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 执行 netsh 命令设置 DHCP
|
||||
if err := net.SetDHCP(nic); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("网卡 %s 已设置为 DHCP 自动获取 IP\n", nic.Name)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(dhcpCmd)
|
||||
}
|
||||
Reference in New Issue
Block a user