fix: snmpv3 in ipv6
This commit is contained in:
@@ -44,7 +44,7 @@ type YamlConfig struct {
|
|||||||
ListenAddr string `yaml:"listenAddr"`
|
ListenAddr string `yaml:"listenAddr"`
|
||||||
ListenPort uint16 `yaml:"listenPort"`
|
ListenPort uint16 `yaml:"listenPort"`
|
||||||
PrivateKey string `yaml:"privateKey"`
|
PrivateKey string `yaml:"privateKey"`
|
||||||
MaxConnNum uint8 `yaml:"maxConnNum"`
|
MaxConnNum int `yaml:"maxConnNum"`
|
||||||
Timeout uint16 `yaml:"timeout"`
|
Timeout uint16 `yaml:"timeout"`
|
||||||
Session string `yaml:"session"`
|
Session string `yaml:"session"`
|
||||||
MmlHome string `yaml:"mmlHome"`
|
MmlHome string `yaml:"mmlHome"`
|
||||||
@@ -57,7 +57,7 @@ type YamlConfig struct {
|
|||||||
TelnetServer struct {
|
TelnetServer struct {
|
||||||
ListenAddr string `yaml:"listenAddr"`
|
ListenAddr string `yaml:"listenAddr"`
|
||||||
ListenPort uint16 `yaml:"listenPort"`
|
ListenPort uint16 `yaml:"listenPort"`
|
||||||
MaxConnNum uint8 `yaml:"maxConnNum"`
|
MaxConnNum int `yaml:"maxConnNum"`
|
||||||
Timeout uint16 `yaml:"timeout"`
|
Timeout uint16 `yaml:"timeout"`
|
||||||
Session string `yaml:"session"`
|
Session string `yaml:"session"`
|
||||||
MmlHome string `yaml:"mmlHome"`
|
MmlHome string `yaml:"mmlHome"`
|
||||||
|
|||||||
@@ -1,19 +1,22 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
TargetIP="[2001:db8::9166]"
|
||||||
|
PORT="34957"
|
||||||
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
1)
|
1)
|
||||||
echo -n "Set HLR state link down ... "
|
echo -n "Set HLR state link down ... "
|
||||||
snmpset -v3 -l noAuthNoPriv -u manager 192.168.2.219:34957 .1.3.6.1.4.1.1379.2.3.3.3.1.1.9.0 i 1 >/dev/null
|
snmpset -v3 -l noAuthNoPriv -u manager ${TargetIP}:${PORT} .1.3.6.1.4.1.1379.2.3.3.3.1.1.9.0 i 1 >/dev/null
|
||||||
echo "done"
|
echo "done"
|
||||||
;;
|
;;
|
||||||
2)
|
2)
|
||||||
echo -n "Set HLR state link up ... "
|
echo -n "Set HLR state link up ... "
|
||||||
snmpset -v3 -l noAuthNoPriv -u manager 192.168.2.219:34957 .1.3.6.1.4.1.1379.2.3.3.3.1.1.9.0 i 2 >/dev/null
|
snmpset -v3 -l noAuthNoPriv -u manager ${TargetIP}:${PORT} .1.3.6.1.4.1.1379.2.3.3.3.1.1.9.0 i 2 >/dev/null
|
||||||
echo "done"
|
echo "done"
|
||||||
;;
|
;;
|
||||||
3)
|
3)
|
||||||
echo -n "Set HLR state authentication failure ... "
|
echo -n "Set HLR state authentication failure ... "
|
||||||
snmpset -v3 -l noAuthNoPriv -u manager 192.168.2.219:34957 .1.3.6.1.4.1.1379.2.3.3.3.1.1.9.0 i 3 >/dev/null
|
snmpset -v3 -l noAuthNoPriv -u manager ${TargetIP}:${PORT} .1.3.6.1.4.1.1379.2.3.3.3.1.1.9.0 i 3 >/dev/null
|
||||||
echo "done"
|
echo "done"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gosnmp/gosnmp"
|
|
||||||
g "github.com/gosnmp/gosnmp"
|
g "github.com/gosnmp/gosnmp"
|
||||||
"github.com/slayercat/GoSNMPServer"
|
"github.com/slayercat/GoSNMPServer"
|
||||||
"github.com/slayercat/GoSNMPServer/mibImps"
|
"github.com/slayercat/GoSNMPServer/mibImps"
|
||||||
@@ -243,6 +242,19 @@ func (s *SNMPService) handleOIDs() []*GoSNMPServer.PDUValueControlItem {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
// 为 GETBULK 新增处理 OIDs
|
||||||
|
bulkOIDs := &GoSNMPServer.PDUValueControlItem{
|
||||||
|
OID: ".1.3.6.1.4.1.1379.2.3.3.3.1.1", // 这里是您想要支持 GETBULK 的 OID 前缀
|
||||||
|
Type: g.OctetString,
|
||||||
|
OnGet: func() (value interface{}, err error) {
|
||||||
|
// 假设我们返回一百度值,您可以根据您的实现进行调整
|
||||||
|
values := []interface{}{s.SysName, s.SysStatus, s.SysDescr, s.SysLocation, s.SysContact, uint32(time.Now().Unix()), s.SysService} // 可以从其他结构中获取真实值
|
||||||
|
return values, nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
customOIDs = append(customOIDs, bulkOIDs)
|
||||||
|
|
||||||
// 获取mibImps.All()返回的OID列表
|
// 获取mibImps.All()返回的OID列表
|
||||||
mibOIDs := mibImps.All()
|
mibOIDs := mibImps.All()
|
||||||
|
|
||||||
@@ -405,7 +417,7 @@ func (s *SNMPService) sendLinkDownTrap(gs *g.GoSNMP, ifIndex int, ifDescr string
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: ".1.3.6.1.4.1.1379.2.3.3.3.4", // severity OID
|
Name: ".1.3.6.1.4.1.1379.2.3.3.3.4", // severity OID
|
||||||
Type: gosnmp.Integer,
|
Type: g.Integer,
|
||||||
Value: 2, // event
|
Value: 2, // event
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -446,7 +458,7 @@ func (s *SNMPService) sendLinkUpTrap(gs *g.GoSNMP, ifIndex int, ifDescr string)
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: ".1.3.6.1.4.1.1379.2.3.3.3.4", // severity OID
|
Name: ".1.3.6.1.4.1.1379.2.3.3.3.4", // severity OID
|
||||||
Type: gosnmp.Integer,
|
Type: g.Integer,
|
||||||
Value: 5, // event
|
Value: 5, // event
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -487,7 +499,7 @@ func (s *SNMPService) sendAuthFailureTrap(gs *g.GoSNMP, username, descr string)
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: ".1.3.6.1.4.1.1379.2.3.3.3.4", // severity OID
|
Name: ".1.3.6.1.4.1.1379.2.3.3.3.4", // severity OID
|
||||||
Type: gosnmp.Integer,
|
Type: g.Integer,
|
||||||
Value: 4, // event
|
Value: 4, // event
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -379,7 +379,7 @@ func handleSSHConnection(conn net.Conn, serverConfig *ssh.ServerConfig) {
|
|||||||
if sshCC > int(conf.Sshd.MaxConnNum) {
|
if sshCC > int(conf.Sshd.MaxConnNum) {
|
||||||
sshMu.Unlock()
|
sshMu.Unlock()
|
||||||
log.Error("Maximum number of connections exceeded")
|
log.Error("Maximum number of connections exceeded")
|
||||||
//conn.Write([]byte("Reach max connections"))
|
channel.Write([]byte(fmt.Sprintf("Connection limit reached (limit=%d). Try again later.\r\n", conf.Sshd.MaxConnNum)))
|
||||||
conn.Close()
|
conn.Close()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -446,15 +446,19 @@ func handleSSHShell(sshConn *ssh.ServerConn, channel ssh.Channel) {
|
|||||||
Version: global.Version,
|
Version: global.Version,
|
||||||
Output: mmlp.DefaultFormatType,
|
Output: mmlp.DefaultFormatType,
|
||||||
MmlHome: conf.Sshd.MmlHome,
|
MmlHome: conf.Sshd.MmlHome,
|
||||||
Limit: 50,
|
Limit: conf.Sshd.MaxConnNum,
|
||||||
User: sshConn.User(),
|
User: sshConn.User(),
|
||||||
SessionToken: fmt.Sprintf("%x", sshConn.SessionID()),
|
SessionToken: fmt.Sprintf("%x", sshConn.SessionID()),
|
||||||
HttpUri: conf.OMC.HttpUri,
|
HttpUri: conf.OMC.HttpUri,
|
||||||
UserAgent: config.GetDefaultUserAgent(),
|
UserAgent: config.GetDefaultUserAgent(),
|
||||||
TagNE: conf.Sshd.TagNE,
|
TagNE: conf.Sshd.TagNE,
|
||||||
}
|
}
|
||||||
|
|
||||||
term := term.NewTerminal(channel, fmt.Sprintf("[%s@%s]> ", omcMmlVar.User, omcMmlVar.TagNE))
|
term := term.NewTerminal(channel, fmt.Sprintf("[%s@%s]> ", omcMmlVar.User, omcMmlVar.TagNE))
|
||||||
|
msg := fmt.Sprintf("\r\nWelcome to the %s server!\r\n", strings.ToUpper(omcMmlVar.TagNE))
|
||||||
|
term.Write([]byte(msg))
|
||||||
|
msg = fmt.Sprintf("Last login: %s from %s \r\n\r\n", time.Now().Format(time.RFC1123), sshConn.RemoteAddr())
|
||||||
|
term.Write([]byte(msg))
|
||||||
|
|
||||||
// 启动交互式shell会话
|
// 启动交互式shell会话
|
||||||
for {
|
for {
|
||||||
line, err := term.ReadLine()
|
line, err := term.ReadLine()
|
||||||
@@ -472,6 +476,13 @@ func handleSSHShell(sshConn *ssh.ServerConn, channel ssh.Channel) {
|
|||||||
}
|
}
|
||||||
var response string
|
var response string
|
||||||
switch cmdline {
|
switch cmdline {
|
||||||
|
case "hello":
|
||||||
|
term.Write([]byte("Hello, world!\r\n"))
|
||||||
|
goto continueLoop
|
||||||
|
case "time":
|
||||||
|
response = fmt.Sprintf("Current time: %s\r\n", time.Now().Format(time.RFC1123))
|
||||||
|
term.Write([]byte(response))
|
||||||
|
goto continueLoop
|
||||||
case "exit", "quit":
|
case "exit", "quit":
|
||||||
goto exitLoop
|
goto exitLoop
|
||||||
case "":
|
case "":
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ type TelnetHandler struct {
|
|||||||
UserName string
|
UserName string
|
||||||
Password string
|
Password string
|
||||||
AuthType string
|
AuthType string
|
||||||
MaxConnNum uint8
|
MaxConnNum int
|
||||||
TagNE string
|
TagNE string
|
||||||
ListenHost string
|
ListenHost string
|
||||||
|
|
||||||
@@ -65,7 +65,8 @@ func (t *TelnetHandler) StartTelnetServer() {
|
|||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
if t.connCount >= int(t.MaxConnNum) {
|
if t.connCount >= int(t.MaxConnNum) {
|
||||||
t.mu.Unlock()
|
t.mu.Unlock()
|
||||||
io.WriteString(conn, "Connection limit reached. Try again later.\r\n")
|
msg := fmt.Sprintf("Connection limit reached (limit=%d). Try again later.\r\n", t.MaxConnNum)
|
||||||
|
io.WriteString(conn, msg)
|
||||||
conn.Close()
|
conn.Close()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -88,7 +89,8 @@ func (t *TelnetHandler) handleTelnetConnection(conn net.Conn) {
|
|||||||
writer := bufio.NewWriter(conn)
|
writer := bufio.NewWriter(conn)
|
||||||
|
|
||||||
// 发送欢迎信息
|
// 发送欢迎信息
|
||||||
writer.WriteString("Welcome to the Telnet server!\r\n")
|
|
||||||
|
writer.WriteString(fmt.Sprintf("\r\nWelcome to the %s server!\r\n", strings.ToUpper(t.TagNE)))
|
||||||
writer.Flush()
|
writer.Flush()
|
||||||
|
|
||||||
// 请求用户名
|
// 请求用户名
|
||||||
@@ -129,7 +131,8 @@ func (t *TelnetHandler) handleTelnetConnection(conn net.Conn) {
|
|||||||
writer.Flush()
|
writer.Flush()
|
||||||
|
|
||||||
if t.handleTelnetAuth(t.AuthType, user, pass) {
|
if t.handleTelnetAuth(t.AuthType, user, pass) {
|
||||||
writer.WriteString("\r\nAuthentication successful!\r\n")
|
msg := fmt.Sprintf("\r\n\r\nLast login: %s from %s \r\n\r\n", time.Now().Format(time.RFC1123), conn.RemoteAddr())
|
||||||
|
writer.WriteString(msg)
|
||||||
writer.Flush()
|
writer.Flush()
|
||||||
t.HandleCommands(user, t.TagNE, reader, writer)
|
t.HandleCommands(user, t.TagNE, reader, writer)
|
||||||
} else {
|
} else {
|
||||||
@@ -181,7 +184,7 @@ func (t *TelnetHandler) HandleCommands(user, tag string, reader *bufio.Reader, w
|
|||||||
case "time":
|
case "time":
|
||||||
writer.WriteString(fmt.Sprintf("\r\nCurrent time: %s\r\n", time.Now().Format(time.RFC1123)))
|
writer.WriteString(fmt.Sprintf("\r\nCurrent time: %s\r\n", time.Now().Format(time.RFC1123)))
|
||||||
case "exit", "quit":
|
case "exit", "quit":
|
||||||
writer.WriteString("\r\nGoodbye!\r\n")
|
writer.WriteString("\r\n\r\nGoodbye!\r\n")
|
||||||
writer.Flush()
|
writer.Flush()
|
||||||
return
|
return
|
||||||
case "":
|
case "":
|
||||||
|
|||||||
Reference in New Issue
Block a user