golang實現跨平台scp批量群發檔案

2021-07-12 05:35:32 字數 1953 閱讀 6606

passwd = flag.string("p", "", "-p passwd 指定密碼.")

user = flag.string("u", "root", "-u root 指定登入使用者.")

cfg = flag.string("c", "serverlist", "-c serverlist 指定serverlist")

ip_port = flag.string("i", "", "-i ip:port 指定目標機器的ip埠,必須和-p結合使用否則不生效.")

dpath = flag.string("d", "", "-d /tmp/20160531.zip 指定傳送到的路徑,不能為空.")

spath = flag.string("s", "", "-s 20160531.zip 指定要傳送檔案的路徑,不能為空.")

)func main()

file, err := os.open(*spath)

if err != nil

info, _ := file.stat()

defer file.close()

if *ip_port != "" && *passwd != ""

scp(client, file, info.size(), *dpath)

return

} var list string

ok := (*passwd != "" && *ip_port == "")

list = config(*cfg, ok)

if len(list) <= 0

for _, v := range list else

client, err := dail(*user, *passwd, *ip_port)

if err != nil

scp(client, file, info.size(), *dpath) }}

func dail(user, password, ip_port string) (*ssh.client, error)

conf := ssh.clientconfig

return ssh.dial("tcp", ip_port, &conf)

}func scp(client *ssh.client, file io.reader, size int64, path string)

go func() ()

if err := session.run(fmt.sprintf("/usr/bin/scp -qrt %s", dirname)); err != nil else

if session, err = client.newsession(); err == nil

fmt.printf("%s 的md5:\n%s\n", client.remoteaddr(), string(buf)) }}

func config(path string, ok bool) (list string)

defer file.close()

buf := bufio.newreader(file)

for

str := strings.trimspace(string(line))

strs := split.split(str)

if ok

} else

} }return

}

golang跨平台編譯

golang中跨平台交叉編譯涉及兩個重要的環境變數 goos和goarch,分別代表target host os和target host arch,如果沒有顯式設定這些環境變數,我們通過go env可以看到go編譯器眼中這兩個環境變數的當前值 go env goarch amd64 gobin ho...

Golang 跨平台編譯

1 首先進入go src 原始碼所在目錄,執行如下命令建立目標平台所需的包和工具檔案。cd usr loc al g o sr c cgo enabled 0 goos linux goarch amd64 make.bash 如果是 windows 則修改 goos 即可。cgo enabled ...

golang 跨平台編譯

本文交叉編譯需要 1.5 以上 cgo enabled 0 goos windows goarch amd64 go build o test win x64.exe test.go cgo enabled 0 goos linux goarch amd64 go build o test linu...