1
0
mirror of https://shylinux.com/x/ContextOS synced 2025-04-25 16:58:06 +08:00

mac opt ssh.send

This commit is contained in:
shaoying 2018-10-11 09:27:41 +08:00
parent 0a01fe3f0c
commit ec6d35cc17

View File

@ -120,25 +120,27 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
"send": &ctx.Command{Name: "send [domain str] cmd arg...", Help: "远程执行", "send": &ctx.Command{Name: "send [domain str] cmd arg...", Help: "远程执行",
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if ssh, ok := m.Target().Server.(*SSH); m.Assert(ok) { if ssh, ok := m.Target().Server.(*SSH); m.Assert(ok) {
domain := "" origin, domain := "", ""
if len(arg) > 1 && arg[0] == "domain" { if len(arg) > 1 && arg[0] == "domain" {
domain, arg = arg[1], arg[2:] origin, arg = arg[1], arg[2:]
if d := strings.TrimPrefix(domain, m.Cap("domain")); len(d) > 0 && d[0] == '.' { if d := strings.TrimPrefix(origin, m.Cap("domain")); len(d) > 0 && d[0] == '.' {
domain = d[1:] domain = d[1:]
} else if d == "" { } else if d == "" {
domain = d domain = d
} else {
domain = origin
} }
if domain == "" { if domain == "" { //本地执行
msg := m.Spawn().Cmd(arg) msg := m.Spawn().Cmd(arg)
m.Copy(msg, "result").Copy(msg, "append") m.Copy(msg, "result").Copy(msg, "append")
return return
} }
} else { } else {
if m.Has("send_code") { if m.Has("send_code") { //本地执行
msg := m.Spawn().Cmd(arg) msg := m.Spawn().Cmd(arg)
m.Copy(msg, "result").Copy(msg, "append") m.Copy(msg, "result").Copy(msg, "append")
} else { } else { //对端执行
msg := m.Spawn(ssh.Message().Source()) msg := m.Spawn(ssh.Message().Source())
msg.Cmd("send", arg) msg.Cmd("send", arg)
m.Copy(msg, "result").Copy(msg, "append") m.Copy(msg, "result").Copy(msg, "append")
@ -146,129 +148,84 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
return return
} }
miss := true match := false
host := strings.SplitN(domain, ".", 2) host := strings.SplitN(domain, ".", 2)
m.Travel(func(m *ctx.Message, i int) bool { m.Travel(func(m *ctx.Message, i int) bool {
if i == 0 { if i == 0 {
return true return true
} }
if m.Cap("hostname") == host[0] { if m.Cap("hostname") == host[0] || "*" == host[0] {
ssh, ok := m.Target().Server.(*SSH)
m.Assert(ok)
msg := m.Spawn(ssh.Message().Source())
if len(host) > 1 { if len(host) > 1 {
ssh, ok := m.Target().Server.(*SSH)
m.Assert(ok)
msg := m.Spawn(ssh.Message().Source())
msg.Cmd("send", "domain", host[1], arg) msg.Cmd("send", "domain", host[1], arg)
m.Copy(msg, "result").Copy(msg, "append")
} else { } else {
msg := m.Spawn()
msg.Cmd("send", arg) msg.Cmd("send", arg)
m.Copy(msg, "result").Copy(msg, "append")
} }
m.Copy(msg, "result").Copy(msg, "append")
miss = false if !match {
return false match = !m.Appends("domain_miss")
}
return host[0] == "*"
} }
return true return true
}, c) }, c)
if miss { if match {
msg := m.Spawn(c.Message().Source()) return
msg.Cmd("send", "domain", domain, arg)
m.Copy(msg, "result").Copy(msg, "append")
} }
if m.Target() == c && m.Has("send_code") {
return m.Appends("domain_miss", true)
if domain != "" { return
domain_miss := true }
host := strings.SplitN(domain, ".", 2) if m.Cap("domain") == m.Conf("hostname") {
m.Travel(func(m *ctx.Message, i int) bool { m.Appends("domain_miss", true)
if i > 0 {
if m.Cap("hostname") == host[0] {
ssh, ok := m.Target().Server.(*SSH)
m.Assert(ok)
msg := m.Spawn(ssh.Message().Source()).Copy(m, "option")
if len(host) > 1 {
msg.Options("downflow", true)
msg.Detail("send", "domain", host[1], arg)
} else {
msg.Detail(arg)
}
if ssh.Message().Back(msg); !msg.Appends("domain_miss") {
m.Copy(msg, "result").Copy(msg, "append")
domain_miss = false
return false
}
}
return false
}
return true
}, c)
if domain_miss && !m.Options("downflow") && m.Cap("domain") != m.Conf("domain") {
ssh, ok := c.Server.(*SSH)
m.Assert(ok)
msg := m.Spawn(ssh.Message().Source()).Copy(m, "option")
msg.Detail("send", "domain", domain, arg)
if ssh.Message().Back(msg); !msg.Appends("domain_miss") {
m.Copy(msg, "result").Copy(msg, "append")
domain_miss = false
}
}
m.Appends("domain_miss", domain_miss)
return return
} }
if m.Options("send_code") || m.Cap("status") != "start" { // 向上路由
msg := m.Spawn().Cmd(arg) msg := m.Spawn(c.Message().Source())
m.Copy(msg, "result").Copy(msg, "append") msg.Cmd("send", "domain", origin, arg)
} else { m.Copy(msg, "result").Copy(msg, "append")
msg := m.Spawn(ssh.Message().Source())
msg.Copy(m, "option").Detail(arg)
ssh.Message().Back(msg)
m.Copy(msg, "result").Copy(msg, "append")
}
return
} }
}}, }},
"pwd": &ctx.Command{Name: "pwd", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "pwd": &ctx.Command{Name: "pwd", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if len(arg) > 0 { if len(arg) == 0 {
if m.Options("send_code") { m.Echo(m.Cap("domain"))
if m.Target() == c { return
msg := m.Spawn().Cmd("send", "pwd", m.Confx("hostname", arg, 0)) }
m.Cap("hostname", msg.Result(0))
m.Cap("domain", msg.Result(1))
} else {
hostname := arg[0]
m.Travel(func(m *ctx.Message, line int) bool {
if hostname == m.Cap("hostname") {
hostname += m.Cap("nhost")
return false
}
return true
}, c)
m.Echo(m.Cap("hostname", hostname))
m.Echo("%s.%s", m.Cap("domain"), m.Cap("hostname"))
}
return
}
if m.Options("send_code") {
if m.Target() == c { if m.Target() == c {
m.Conf("hostname", arg[0]) msg := m.Spawn().Cmd("send", "pwd", m.Confx("hostname", arg, 0))
msg := m.Spawn().Cmd("send", "pwd", arg[0])
m.Cap("hostname", msg.Result(0)) m.Cap("hostname", msg.Result(0))
m.Cap("domain", msg.Result(1)) m.Cap("domain", msg.Result(1))
} else { } else {
m.Spawn().Cmd("send", "pwd", arg[0]) hostname := arg[0]
return m.Travel(func(m *ctx.Message, line int) bool {
if hostname == m.Cap("hostname") {
hostname += m.Cap("nhost")
return false
}
return true
}, c)
m.Echo(m.Cap("hostname", hostname))
m.Echo("%s.%s", m.Cap("domain"), m.Cap("hostname"))
} }
return
} }
if m.Target() == c {
m.Conf("hostname", arg[0])
msg := m.Spawn().Cmd("send", "pwd", arg[0])
m.Cap("hostname", msg.Result(0))
m.Cap("domain", msg.Result(1))
} else {
m.Spawn().Cmd("send", "pwd", arg[0])
}
m.Echo(m.Cap("domain")) m.Echo(m.Cap("domain"))
}}, }},
"hello": &ctx.Command{Name: "hello request", Help: "加密请求", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "hello": &ctx.Command{Name: "hello request", Help: "加密请求", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {