Commit 1d4fcc1e by 浔阳陌客

2022/04/25 开始更新

parent e449eea7
File added
#!/bin/bash #!/bin/bash
os=(windows darwin linux)
for i in "${os[@]}" ;do way=$1
test "$i" == "windows" && ext="${i}.exe" case "$way" in
test "$i" == "darwin" && ext="macos" win)
test "$i" == "linux" && ext=$i CGO_ENABLED=0 GOOS=windows go build -o caut_windows.exe *.go
CGO_ENABLED=0 GOOS="$i" GOARCH=amd64 go build -o release/caut_"$ext" -a *.go ;;
openssl sha256 release/caut_"$ext" >release/caut_"$ext"_sha256.txt *)
done os=(windows darwin linux)
\ No newline at end of file for i in "${os[@]}" ;do
test "$i" == "windows" && ext="${i}.exe"
test "$i" == "darwin" && ext="macos"
test "$i" == "linux" && ext=$i
CGO_ENABLED=0 GOOS="$i" GOARCH=amd64 go build -o release/caut_"$ext" -a *.go
openssl sha256 release/caut_"$ext" >release/caut_"$ext"_sha256.txt
done
;;
esac
package main
import "fmt"
func dInit() {
fmt.Println("欢迎使用 知微资产信息自动同步工具")
fmt.Println("该工具将为知微所有主机资产自动同步到CMDB,个人macbook仅仅记录IP和mac地址。")
fmt.Println("程序开源,项目地址: http://git.zhiweidata.top/taotengfei/caut")
fmt.Println("----------------------------------------------------------")
}
type inputBasic struct {
Region string `json:"region"`
Dept string `json:"dept"`
Name string `json:"name"`
}
// func (ib * inputBasic)
package main package main
import ( import (
"flag"
pub "git.zhiweidata.top/taotengfei/AD-Control-Golang/public" pub "git.zhiweidata.top/taotengfei/AD-Control-Golang/public"
) )
...@@ -9,3 +11,18 @@ func envCheck(ip string) { ...@@ -9,3 +11,18 @@ func envCheck(ip string) {
pub.User_computername = ip pub.User_computername = ip
} }
} }
func parseFlag() (information, bool) {
var info information
var mode bool
flag.StringVar(&info.Region, "region", "", "地区信息,有效值:宁波、郑州")
flag.StringVar(&info.Dept, "dept", "", "部门信息,有效值:人事部、培训部、产品部、技术部")
flag.StringVar(&info.Name, "name", "", "姓名")
flag.BoolVar(&mode, "server", false, "程序运行换几个")
flag.Parse()
// if !info.Check() {
// os.Exit(-1)
// }
return info, mode
}
package main
import (
"golang.org/x/text/encoding/simplifiedchinese"
)
func getWinPlatform() []byte {
psfile := []byte(`
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$page_length=270
$page_wdith=260
$button_legth=75
$button_width=25
$pltx=20 # 页面左上角的起始位置,x
$plty=30 # 页面左上角的起始位置,y
$plm=($page_length/4*2) # 页面中间分割点
$jg=3 # 页面左上角的起始位置,y
$zjl=15 # 单个汉字的像素
$line=15 # 行间距
$height=20 # 高度
$intput_legth=100
$region="宁波","郑州"
$dept="财务部","培训部","产品部","技术部"
$form = New-Object System.Windows.Forms.Form
$form.Text = "信息收集"
$form.Size = New-Object System.Drawing.Size($page_length,$page_wdith)
$form.StartPosition = 'CenterScreen'
$titleLabel = New-Object System.Windows.Forms.Label
$titleLabel.Text = '知微资产信息同步工具'
$titleLabel.Location = New-Object System.Drawing.Point($pltx,$plty)
$titleLabel.Size = New-Object System.Drawing.Size(($titleLabel.Text.Length * $zjl),$height)
$form.Controls.Add($titleLabel)
$regionLabel = New-Object System.Windows.Forms.Label
$regionLabel.Text = '主机所属地区'
$regionLabel.Location = New-Object System.Drawing.Point($pltx,($titleLabel.Location.Y+$titleLabel.Size.Height + $line))
$regionLabel.Size = New-Object System.Drawing.Size(($regionLabel.Text.Length * $zjl),$height)
$form.Controls.Add($regionLabel)
$regionComboBox = New-Object System.Windows.Forms.ComboBox
$regionComboBox.Location = New-Object System.Drawing.Point($plm,($titleLabel.Location.Y+$titleLabel.Size.Height + $line - $jg))
$regionComboBox.Size = New-Object System.Drawing.Size($intput_legth,$plty)
$regionComboBox.Height = 80
foreach ($i in $region){
[void] $regionComboBox.Items.Add($i)
}
$form.Controls.Add($regionComboBox)
$deptlabel = New-Object System.Windows.Forms.Label
$deptlabel.Text = '部门'
$deptlabel.Location = New-Object System.Drawing.Point($pltx,($regionLabel.Location.Y+$regionLabel.Size.Height + $line))
$deptlabel.Size = New-Object System.Drawing.Size(($deptlabel.Text.Length * $zjl),$height)
$form.Controls.Add($deptlabel)
$deptComboBox = New-Object System.Windows.Forms.ComboBox
$deptComboBox.Location = New-Object System.Drawing.Point($plm,($regionLabel.Location.Y+$regionLabel.Size.Height + $line - $jg))
$deptComboBox.Size = New-Object System.Drawing.Size($intput_legth,$plty)
$deptComboBox.Height = 80
foreach ($i in $dept){
[void] $deptComboBox.Items.Add($i)
}
$form.Controls.Add($deptComboBox)
$nameLabel = New-Object System.Windows.Forms.Label
$nameLabel.Text = '姓名'
$nameLabel.Location = New-Object System.Drawing.Point($pltx,($deptComboBox.Location.Y+$deptComboBox.Size.Height + $line))
$nameLabel.Size = New-Object System.Drawing.Size(($nameLabel.Text.Length * $zjl),$height)
$form.Controls.Add($nameLabel)
$nameBox = New-Object System.Windows.Forms.TextBox
$nameBox.Location = New-Object System.Drawing.Point($plm,($deptComboBox.Location.Y+$deptComboBox.Size.Height + $line - $jg))
$nameBox.Size = New-Object System.Drawing.Size($intput_legth,$height)
$form.Controls.Add($nameBox)
$pullButton = New-Object System.Windows.Forms.Button
$pullButton.Location = New-Object System.Drawing.Point((($page_length-$button_legth)/2),($nameLabel.Location.Y+$nameLabel.Size.Height + $line))
$pullButton.Size = New-Object System.Drawing.Size($button_legth,$button_width)
$pullButton.Text = '提交'
$pullButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $pullButton
$form.Controls.Add($pullButton)
$form.Topmost = $true
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$r_dept = $deptComboBox.SelectedItem
$r_region = $regionComboBox.SelectedItem
$r_name = $nameBox.Text
$result = @"
{
"region":"$r_region",
"dept":"$r_dept",
"name":"$r_name"
}
"@
Write-host $result
}
`)
r, _ := simplifiedchinese.GBK.NewEncoder().Bytes(psfile)
return r
}
...@@ -2,14 +2,20 @@ module caut ...@@ -2,14 +2,20 @@ module caut
go 1.18 go 1.18
require git.zhiweidata.top/taotengfei/AD-Control-Golang/public v0.0.0 require (
git.zhiweidata.top/taotengfei/AD-Control-Golang/public v0.0.0
github.com/ying32/govcl v2.2.0+incompatible
)
require ( require (
github.com/Azure/go-ntlmssp v0.0.0-20211209120228-48547f28849e // indirect github.com/Azure/go-ntlmssp v0.0.0-20211209120228-48547f28849e // indirect
github.com/go-asn1-ber/asn1-ber v1.5.3 // indirect github.com/go-asn1-ber/asn1-ber v1.5.3 // indirect
github.com/go-ldap/ldap/v3 v3.4.2 // indirect github.com/go-ldap/ldap/v3 v3.4.2 // indirect
github.com/gookit/color v1.5.0 // indirect github.com/gookit/color v1.5.0 // indirect
github.com/mozillazg/go-pinyin v0.19.0 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
github.com/ying32/dylib v0.0.0-20220227124818-fdf9ea9fbc96 // indirect
github.com/ying32/liblclbinres v0.0.0-20220111074620-df670ba4baf4 // indirect
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 // indirect golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 // indirect
golang.org/x/sys v0.0.0-20220403020550-483a9cbc67c0 // indirect golang.org/x/sys v0.0.0-20220403020550-483a9cbc67c0 // indirect
golang.org/x/text v0.3.7 // indirect golang.org/x/text v0.3.7 // indirect
......
...@@ -10,6 +10,8 @@ github.com/go-ldap/ldap/v3 v3.4.2 h1:zFZKcXKLqZpFMrMQGHeHWKXbDTdNCmhGY9AK41zPh+8 ...@@ -10,6 +10,8 @@ github.com/go-ldap/ldap/v3 v3.4.2 h1:zFZKcXKLqZpFMrMQGHeHWKXbDTdNCmhGY9AK41zPh+8
github.com/go-ldap/ldap/v3 v3.4.2/go.mod h1:iYS1MdmrmceOJ1QOTnRXrIs7i3kloqtmGQjRvjKpyMg= github.com/go-ldap/ldap/v3 v3.4.2/go.mod h1:iYS1MdmrmceOJ1QOTnRXrIs7i3kloqtmGQjRvjKpyMg=
github.com/gookit/color v1.5.0 h1:1Opow3+BWDwqor78DcJkJCIwnkviFi+rrOANki9BUFw= github.com/gookit/color v1.5.0 h1:1Opow3+BWDwqor78DcJkJCIwnkviFi+rrOANki9BUFw=
github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo= github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo=
github.com/mozillazg/go-pinyin v0.19.0 h1:p+J8/kjJ558KPvVGYLvqBhxf8jbZA2exSLCs2uUVN8c=
github.com/mozillazg/go-pinyin v0.19.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
...@@ -17,6 +19,12 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc ...@@ -17,6 +19,12 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/ying32/dylib v0.0.0-20220227124818-fdf9ea9fbc96 h1:+kU/yXSAQPL4G7OQOxz/j62GdM+G5yd5J1/NNm7u3QI=
github.com/ying32/dylib v0.0.0-20220227124818-fdf9ea9fbc96/go.mod h1:WPBTfbqg/aA1xqHFPB4fgBHETq/G/75yErbnexmaEOo=
github.com/ying32/govcl v2.2.0+incompatible h1:TZFn0SUuZaufZ+U7Urs9o1ZmbflsCPQcswz1bo22w9s=
github.com/ying32/govcl v2.2.0+incompatible/go.mod h1:yZVtbJ9Md1nAVxtHKIriKZn4K6TQYqI1en3sN/m9FJ8=
github.com/ying32/liblclbinres v0.0.0-20220111074620-df670ba4baf4 h1:g6QaezInPib6PtPzh4w1qEzQPD4bT3zbX8dfBCSFqYM=
github.com/ying32/liblclbinres v0.0.0-20220111074620-df670ba4baf4/go.mod h1:oEmgAOPt+dALMzap3HgEQnaN+iCLE7m3C867jERInaI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 h1:tkVvjkPTB7pnW3jnid7kNyAMPVWllTNOf/qKDze4p9o= golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 h1:tkVvjkPTB7pnW3jnid7kNyAMPVWllTNOf/qKDze4p9o=
......
...@@ -10,26 +10,27 @@ import ( ...@@ -10,26 +10,27 @@ import (
type information struct { type information struct {
ips []string ips []string
IP string IP string `json:"ip"`
macaddresses []string macaddresses []string
Macaddress string Macaddress string `json:"macaddress"`
Dept string Hostname string `json:"hostname"`
Name string Dept string `json:"dept"`
Region string Name string `json:"name"`
Region string `json:"region"`
} }
func (info *information) Check() bool { func (info *information) Check() bool {
if info.Region == "" { if info.Region == "" {
LOG(ERROR, "FLAG", fmt.Sprint("地区信息未输入\n具体参数 --regsion=宁波\n有效值:宁波、郑州")) LOG(ERROR, "FLAG", fmt.Sprint("地区信息未输入,参考命令 --regsion=宁波,有效值:宁波、郑州"))
return false return false
} }
if info.Region == "" { if info.Region == "" {
LOG(ERROR, "FLAG", fmt.Sprint("部门信息未输入\n具体参数 --dept=技术部门\n有效值:人事部门、培训部门、产品部门、技术部门")) LOG(ERROR, "FLAG", fmt.Sprint("部门信息未输入,参考命令 --dept=技术部门,有效值:人事部门、培训部门、产品部门、技术部门"))
return false return false
} }
if info.Name == "" { if info.Name == "" {
LOG(ERROR, "FLAG", fmt.Sprint("姓名未输入\n具体参数 --name=xxx")) LOG(ERROR, "FLAG", fmt.Sprint("姓名未输入,参考命令 --name=xxx"))
return false return false
} }
return true return true
...@@ -42,6 +43,7 @@ func (info *information) GetIP() { ...@@ -42,6 +43,7 @@ func (info *information) GetIP() {
default: default:
info.ips = info.getIPFromInterface() info.ips = info.getIPFromInterface()
} }
info.ipDelete()
info.ipString() info.ipString()
} }
func (info *information) getIPFromInterface() []string { func (info *information) getIPFromInterface() []string {
...@@ -67,18 +69,35 @@ func (info *information) getIPFromInterface() []string { ...@@ -67,18 +69,35 @@ func (info *information) getIPFromInterface() []string {
return ips return ips
} }
// 排除特殊提示
func (info *information) ipDelete() {
for i, j := range info.ips {
if strings.HasPrefix("127", j) || strings.HasPrefix("169", j) {
info.ips[i] = ""
}
}
}
func (info *information) ipString() string { func (info *information) ipString() string {
var r string var r string
for _, j := range info.ips { for _, ip := range info.ips {
r += fmt.Sprintf("%s,", j) if ip == "" {
continue
}
r += fmt.Sprintf("%s,", ip)
}
if len(r) != 0 {
r = r[0 : len(r)-1]
} }
r = r[0 : len(r)-1]
info.IP = r info.IP = r
return r return r
} }
func (info *information) GetMACAddress() { func (info *information) GetMACAddress() {
for _, ip := range info.ips { for _, ip := range info.ips {
fmt.Println(ip) if ip == "" {
continue
}
switch pub.OS_Type { switch pub.OS_Type {
case pub.OS_Type_Windows: case pub.OS_Type_Windows:
info.macaddresses = append(info.macaddresses, pub.GetMacaddressFromIP(ip)) info.macaddresses = append(info.macaddresses, pub.GetMacaddressFromIP(ip))
...@@ -89,5 +108,15 @@ func (info *information) GetMACAddress() { ...@@ -89,5 +108,15 @@ func (info *information) GetMACAddress() {
info.macString() info.macString()
} }
func (info *information) macString() { func (info *information) macString() {
LOG(INFO, "MAC", info.macaddresses) var r string
for _, macaddress := range info.macaddresses {
if macaddress == "" {
continue
}
r += fmt.Sprintf("%s,", macaddress)
}
if len(r) != 0 {
r = r[0 : len(r)-1]
}
info.Macaddress = r
} }
package main
import "fmt"
func linuxDesktop() {
fmt.Println("hello world")
}
func linuxServer() {
fmt.Println("hello world")
}
package main
import "fmt"
func macOS() {
fmt.Println("hello world")
}
...@@ -4,9 +4,7 @@ ...@@ -4,9 +4,7 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"os"
pub "git.zhiweidata.top/taotengfei/AD-Control-Golang/public" pub "git.zhiweidata.top/taotengfei/AD-Control-Golang/public"
) )
...@@ -14,38 +12,38 @@ import ( ...@@ -14,38 +12,38 @@ import (
const INFO = pub.INFO const INFO = pub.INFO
const ERROR = pub.ERROR const ERROR = pub.ERROR
func basicInfo() { func initialize() {
if !pub.Zhiweireach() { if !pub.Zhiweireach() {
pub.ADServerDaemon = pub.Host_adserver_ip + pub.DaemonLogListen pub.ADServerDaemon = pub.Host_adserver_ip + pub.DaemonLogListen
pub.ADServerLogDaemon = pub.Host_adserver_ip + pub.DaemonLogListen pub.ADServerLogDaemon = pub.Host_adserver_ip + pub.DaemonLogListen
} }
fmt.Println("欢迎使用 知微资产信息自动同步工具")
fmt.Println("该工具将为知微所有主机资产自动同步到CMDB,个人macbook仅仅记录IP和mac地址。")
fmt.Println("程序开源,项目地址: http://git.zhiweidata.top/taotengfei/caut")
fmt.Println("----------------------------------------------------------")
}
func parseFlag() information {
var info information
flag.StringVar(&info.Region, "region", "", "地区信息,有效值:宁波、郑州")
flag.StringVar(&info.Dept, "dept", "", "部门信息,有效值:人事部门、培训部门、产品部门、技术部门")
flag.StringVar(&info.Name, "name", "", "姓名")
flag.Parse()
if !info.Check() {
os.Exit(-1)
}
return info
} }
func main() { func main() {
basicInfo() initialize()
info := parseFlag() _, server := parseFlag()
info.GetIP()
//LOGS(INFO, "FLAG", fmt.Sprintf("版本:%s\n地区:%s\n部门:%s\n姓名:%s", pub.Version, info.Region, info.Dept, info.Name)) if server {
envCheck(info.ips[0]) switch pub.OS_Type {
info.GetMACAddress() case pub.OS_Type_Windows:
windowsServer()
case pub.OS_Type_Linux:
linuxServer()
case pub.OS_Type_MacOS:
macOS()
}
} else {
switch pub.OS_Type {
case pub.OS_Type_Windows:
windowsDesktop()
case pub.OS_Type_Linux:
linuxDesktop()
case pub.OS_Type_MacOS:
macOS()
}
}
} }
func LOG(level string, msg_type string, args ...any) { func LOG(level string, msg_type string, args ...any) {
......
SHA256(release/caut_linux)= 971badc4bab8e2dada03d2044e4ec0cf7901224edfbacde1c82c60891d535f5a
SHA256(release/caut_macos)= 10d4bd5ab3fe52679c9c5bea271da5838909cd4dbfd0a81f7283a1a0328886a8
SHA256(release/caut_windows.exe)= 19627948a5d9486bd3f6c1055dfabcfe55bcaefe0913d9de473a6874a38dd6e5
package main
import (
"encoding/json"
"fmt"
"os"
pub "git.zhiweidata.top/taotengfei/AD-Control-Golang/public"
"golang.org/x/text/encoding/simplifiedchinese"
)
const platform string = "platform.ps1"
func windowsDesktop() {
psfile := fmt.Sprintf(`%s\%s`, pub.User_tmp, platform)
l, err := os.OpenFile(psfile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0766)
if err != nil {
winErrorExit(err)
}
l.Write(getWinPlatform())
if err := l.Close(); err != nil {
winErrorExit(err)
}
out_byte, err := pub.PSFileOutputNoSplitRemoteSigend(psfile)
if err != nil {
winErrorExit(err)
}
out := parseChinese(out_byte)
var ib inputBasic
if err := json.Unmarshal(out, &ib); err != nil {
winErrorExit(err)
}
var info information
info.GetIP()
info.GetMACAddress()
info.Hostname = pub.User_computername
info.Dept = ib.Dept
info.Name = ib.Name
info.Region = ib.Region
fmt.Println(info.IP, info.Macaddress)
fmt.Println(info.Dept, info.Name, info.Region)
}
func windowsServer() {
fmt.Println("hello world")
}
func winMsg(s string) {
pub.Execcmd_nowait(fmt.Sprintf(`msg %s "%s"`, pub.User_name, s))
}
func winErrorExit(err error) {
winMsg(err.Error())
os.Exit(1)
}
func parseChinese(b []byte) []byte {
r, _ := simplifiedchinese.GBK.NewDecoder().Bytes(b)
return r
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment