前言
這是一個輪子。
大家都知道 Ansible 是功能超級強大的自動化運維工具,十分的高大上。太高大上了以至于在低端運維有點水土不服,在于三點:
- Ansible 是基于 Python 的,而 Python 下的安裝是有一堆依賴的。。。不要笑!對于很多使用 Win 的用戶而言,光是裝 Python, 裝 pip 就夠喝一壺的了。
- Ansible 的 paybook 所使用的 yaml 語法當然非常強大了。然而對于新人而言,剛入手是玩不轉的,需要學習。雖然 Ansible 相比其他的自動化運維工具,它的學習曲線已經非常平易近人了,但畢竟還是要學一下的不是么
- Ansible 自動化運維 Linux 服務器得益于 Linux 上 python 的默認支持,功能非常強大。然而如果拿來跑交換機的話,因為交換機上通常沒有 python 環境,功能就要打很多折扣了。基本上也就是執行一系列的命令組合。而我們這種有大片園區網的傳統單位,運維的大頭正式是交換機~
所以造這個輪子的出發點是基于以下考慮的:
- 要跨平臺,木有依賴,開箱即用。用 Go 來擼一個就能很好的滿足這個需求。你看 Open-Falcon 的 agent,ELK 的 beats ,都選擇用 Go 來實現,就是這個原因。
- 簡單無腦,無需學習。直接堆砌命令行就行,就像我們初始化交換機的那種命令行組合模板。只要 cli 會玩,直接照搬過來就行。
- 要支持并發。這個是 Go 的強項了,無需多言。
- 最后當然是學習 Go 啦。
一點都沒有黑 Ansible 的意思。我們也有在用 Ansible 來做自動化運維的工作,我覺得所有運維最好都學習下 Ansible,將來總是要往自動化的方向走的。這個輪子的目的在于學習 Ansible 之前,先有個夠簡單無腦的工具解決下眼前的需求~
建立 ssh 會話
Go 自身不帶 ssh 包。他的 ssh 包放在了 https://godoc.org/golang.org/x/crypto/ssh 這里。import 他就好
1
|
import "golang.org/x/crypto/ssh" |
首先我們需要建立一個 ssh 會話,比如這樣。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
func connect(user, password, host, key string, port int, cipherList []string) (*ssh.Session, error) { var ( auth []ssh.AuthMethod addr string clientConfig *ssh.ClientConfig client *ssh.Client config ssh.Config session *ssh.Session err error ) // get auth method auth = make([]ssh.AuthMethod, 0) if key == "" { auth = append(auth, ssh.Password(password)) } else { pemBytes, err := ioutil.ReadFile(key) if err != nil { return nil, err } var signer ssh.Signer if password == "" { signer, err = ssh.ParsePrivateKey(pemBytes) } else { signer, err = ssh.ParsePrivateKeyWithPassphrase(pemBytes, []byte(password)) } if err != nil { return nil, err } auth = append(auth, ssh.PublicKeys(signer)) } if len(cipherList) == 0 { config = ssh.Config{ Ciphers: []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-gcm@openssh.com", "arcfour256", "arcfour128", "aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"}, } } else { config = ssh.Config{ Ciphers: cipherList, } } clientConfig = &ssh.ClientConfig{ User: user, Auth: auth, Timeout: 30 * time.Second, Config: config, HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }, } // connet to ssh addr = fmt.Sprintf("%s:%d", host, port) if client, err = ssh.Dial("tcp", addr, clientConfig); err != nil { return nil, err } // create session if session, err = client.NewSession(); err != nil { return nil, err } modes := ssh.TerminalModes{ ssh.ECHO: 0, // disable echoing ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud } if err := session.RequestPty("xterm", 80, 40, modes); err != nil { return nil, err } return session, nil } |
ssh.AuthMethod 里存放了 ssh 的認證方式。使用密碼認證的話,就用 ssh.Password()來加載密碼。使用密鑰認證的話,就用 ssh.ParsePrivateKey() 或 ssh.ParsePrivateKeyWithPassphrase() 讀取密鑰,然后通過 ssh.PublicKeys() 加載進去。
ssh.config 這個 struct 存了 ssh 的配置參數,他有以下幾個配置選項,以下引用自GoDoc 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
type Config struct { // Rand provides the source of entropy for cryptographic // primitives. If Rand is nil, the cryptographic random reader // in package crypto/rand will be used. // 加密時用的種子。默認就好 Rand io.Reader // The maximum number of bytes sent or received after which a // new key is negotiated. It must be at least 256. If // unspecified, a size suitable for the chosen cipher is used. // 密鑰協商后的最大傳輸字節,默認就好 RekeyThreshold uint64 // The allowed key exchanges algorithms. If unspecified then a // default set of algorithms is used. // KeyExchanges []string // The allowed cipher algorithms. If unspecified then a sensible // default is used. // 連接所允許的加密算法 Ciphers []string // The allowed MAC algorithms. If unspecified then a sensible default // is used. // 連接允許的 MAC (Message Authentication Code 消息摘要)算法,默認就好 MACs []string } |
基本上默認的就好啦。但是 Ciphers 需要修改下,默認配置下 Go 的 SSH 包提供的 Ciphers 包含以下加密方式
aes128-ctr aes192-ctr aes256-ctr aes128-gcm@openssh.com arcfour256 arcfour128
連 linux 通常沒有問題,但是很多交換機其實默認只提供 aes128-cbc 3des-cbc aes192-cbc aes256-cbc 這些。因此我們還是加全一點比較好。
這里有兩個地方要提一下
1、在 clientConfig 里有這么一段
1
2
3
|
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }, |
這是因為默認密鑰不受信任時,Go 的 ssh 包會在 HostKeyCallback 里把連接干掉(1.8 之后加的應該)。但是我們使用用戶名密碼連接的時候,這個太正常了不是么,所以讓他 return nil 就好了。
2、在 NewSession() 后,我們定義了 modes 和 RequestPty。這是因為為之后使用 session.Shell() 模擬終端時,所建立的終端參數。如果不配的話,默認值可能導致在某些終端上執行失敗。例如一些 H3C 的交換機,連接建立后默認推出來的 Copyright 可能會導致 ssh 連接異常,然后超時或者直接斷掉。例如這樣:
1
2
3
4
5
|
****************************************************************************** * Copyright (c) 2004-2016 Hangzhou H3C Tech. Co., Ltd. All rights reserved. * * Without the owner's prior written consent, * * no decompiling or reverse-engineering shall be allowed. * ****************************************************************************** |
配置的參數照搬 GoDoc 上的示例就好了:
1
2
3
4
5
6
7
8
9
10
|
// Set up terminal modes modes := ssh.TerminalModes{ ssh.ECHO: 0, // disable echoing ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud } // Request pseudo terminal if err := session.RequestPty("xterm", 40, 80, modes); err != nil { log.Fatal("request for pseudo terminal failed: ", err) } |
執行命令
建立起 session 后,執行命令就很簡單了,用 session.Run() 就可以執行我們的命令,結果則返回到 session.Studout 里。我們跑個簡單的測試。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
const ( username = "admin" password = "password" ip = "192.168.15.101" port = 22 cmd = "show clock" ) func Test_SSH_run(t *testing.T) { ciphers := []string{} session, err := connect(username, password, ip, port, ciphers) if err != nil { t.Error(err) return } defer session.Close() var stdoutBuf bytes.Buffer session.Stdout = &stdoutBuf session.Run(cmd) t.Log(session.Stdout) return } |
目標是一臺交換機,測試一下
1
2
3
4
|
=== RUN Test_SSH_run --- PASS: Test_SSH_run (0.69s) ssh_test.go:30: 07:55:52.598 UTC Wed Jan 17 2018 PASS |
可以看到 show clock 的命令已經成功執行了,并返回了結果。
session.Run() 僅限定執行單條命令,要執行若干命令組合就需要用到 session.Shell() 了。意思很明確,就是模擬一個終端去一條一條執行命令,并返回結果。就像我們用 Shell 一樣,我們把整過過程打印出來輸出就好了。從 session.StdinPipe() 逐個輸入命令,從session.Stdout 和 session.Stderr 獲取 Shell 上的輸出。一樣來做個測試。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
const ( username = "admin" password = "password" ip = "192.168.15.101" port = 22 cmds = "show clock;show env power;exit" ) func Test_SSH(t *testing.T) { var cipherList []string session, err := connect(username, password, ip, key, port, cipherList) if err != nil { t.Error(err) return } defer session.Close() cmdlist := strings.Split(cmd, ";") stdinBuf, err := session.StdinPipe() if err != nil { t.Error(err) return } var outbt, errbt bytes.Buffer session.Stdout = &outbt session.Stderr = &errbt err = session.Shell() if err != nil { t.Error(err) return } for _, c := range cmdlist { c = c + "\n" stdinBuf.Write([]byte(c)) } session.Wait() t.Log((outbt.String() + errbt.String())) return } |
還是那臺交換機,測試一下
1
2
3
4
5
6
7
8
9
10
11
|
=== RUN Test_SSH --- PASS: Test_SSH (0.69s) ssh_test.go:51: sw-1#show clock 07:59:52.598 UTC Wed Jan 17 2018 sw-1#show env power SW PID Serial# Status Sys Pwr PoE Pwr Watts -- ------------------ ---------- --------------- ------- ------- ----- 1 Built-in Good sw-1#exit PASS |
可以看到,兩個命令都得到執行了,并在執行完 exit 后退出連接。
比較一下和 session.Run() 的區別,可以發現在 session.Shell() 模式下,輸出的內容包含了主機的名字,輸入的命令等等。因為這是 tty 執行的結果嘛。如果我們只需要執行命令倒也無所謂,但是如果我們還需要從執行命令的結果中讀取一些信息,這些內容就顯得有些臃腫了。比如我們在一臺 ubuntu 上跑一下看看
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
=== RUN Test_SSH --- PASS: Test_SSH (0.98s) ssh_test.go:50: Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-98-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage System information as of Thu Jan 18 16:34:56 CST 2018 System load: 0.0 Processes: 335 Usage of /: 10.0% of 90.18GB Users logged in: 0 Memory usage: 2% IP address for eth0: 192.168.80.131 Swap usage: 0% IP address for docker0: 172.17.0.1 Graph this data and manage this system at: https://landscape.canonical.com/ 16 個可升級軟件包。 16 個安全更新。 New release '17.10' available. Run 'do-release-upgrade' to upgrade to it. You have new mail. Last login: Thu Jan 18 16:31:41 2018 from 192.168.95.104 root@ubuntu-docker-node3:~# root@ubuntu-docker-node3:/opt# /opt root@ubuntu-docker-node3:/opt# 注銷 |
最起碼,上面那一堆 System information 就用不著嘛。交換機是沒有辦法,Linux 上能不能通過一條命令,也就是想辦法 session.Run() 來執行命令組合呢?
答案是可以的,把命令通過 && 連接起來就好了嘛。LInux 的 Shell 會幫我們拆開來分別運行的,比如上面的這個命令我們就可以合并成一條命令 cd /opt&&pwd&&exit
1
2
3
|
=== RUN Test_SSH_run --- PASS: Test_SSH_run (0.91s) ssh_test.go:76: /opt |
立馬就簡潔了對不對?
輪子
ssh 執行命令這樣就差不多了。要變成一個可以用 ssh 批量操作工具,我們還要給他加上并發執行,并發限制,超時控制,輸入參數解析,輸出格式等等
這里就不展開了,最終這個造出來的輪子長這樣:https://github.com/shanghai-edu/multissh
可以直接命令行來執行,通過 ; 號或者 , 號作為命令和主機的分隔符。
# ./multissh -cmds "show clock" -hosts "192.168.31.21;192.168.15.102" -u admin -p password
也可以通過文本來存放主機組和命令組,通過換行符分隔。
# ./multissh -cmdfile cmd1.txt.example -hostfile host.txt.example -u admin -p password
特別的,如果輸入的是 IP (-ips 或 -ipfile),那么允許 IP 地址段方式的輸入,例如 192.168.15.101-192.168.15.110 。(還記得 swcollector 么,類似的實現方式)
# ./multissh -cmds "show clock" -ips "192.168.15.101-192.168.15.110" -u admin -p password
支持使用 ssh 密鑰認證,此時如果輸入 password ,則為作為 key 的密碼
# ./multissh -hosts "192.168.80.131" -cmds "date;cd /opt;ls" -u root -k "server.key"
對于 linux ,支持 linuxMode 模式,也就是將命令組合通過 && 連接后,使用 session.Run() 運行。
# ./multissh -hosts "192.168.80.131" -cmds "date;cd /opt;ls" -u root -k "server.key" -l
也可以為每個主機定義不同的配置參數,以 json 格式加載配置。
1
|
# ./multissh -c ssh.json.example |
輸出可以打成 json 格式,方便程序處理。
1
|
# ./multissh -c ssh.json.example -j |
也可以把輸出結果存到以主機名命名的文本中,比如用來做配置備份
1
|
# ./multissh -c ssh.json.example -outTxt |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.jianshu.com/p/7d315f8551ad