Linux远程批量工具mooon_ssh和mooon_upload使用示例
Linux遠程批量工具mooon_ssh和mooon_upload使用示例.pdf
目錄
目錄 1
1.?前言 1
2.?批量執行命令工具:mooon_ssh 2
3.?批量上傳文件工具:mooon_upload 2
4.?使用示例 3
4.1.?使用示例1:上傳/etc/hosts 3
4.2.?使用示例2:檢查/etc/profile文件是否一致 3
4.3.?使用示例3:批量查看crontab 3
4.4.?使用示例4:批量清空crontab 3
4.5.?使用示例5:批量更新crontab 3
4.6.?使用示例6:取遠端機器IP 3
4.7.?使用示例7:批量查看kafka進程(環境變量方式) 4
4.8.?使用示例8:批量停止kafka進程(參數方式) 5
5.?如何編譯批量工具? 5
5.1.?GO版本 5
5.2.?C++版本 6
?
1.?前言
遠程批量工具包含:
1)?批量命令工具mooon_ssh;
2)?批量上傳文件工具mooon_upload;
3)?批量下載文件工具mooon_download。
?
可執行二進制包下載地址:
https://github.com/eyjian/libmooon/releases
?
源代碼包下載地址:
https://github.com/eyjian/libmooon/archive/master.zip
?
批量工具除由三個工具組成外,還分兩個版本:
1)?C++版本
2)?GO版本
?
當前C++版本比較成熟,GO版本相當簡略,但C++版本依賴C++運行時庫,不同環境需要特定編譯,而GO版本可不依賴C和C++運行時庫,所以不需編譯即可應用到廣泛的Linux環境。
?
使用簡單,直接執行命令,即會提示用法,如C++版本:
| $?mooon_ssh parameter[-c]'s?value?not?set ? usage: -h[]:?Connect?to?the?remote?machines?on?the?given?hosts?separated?by?comma,?can?be?replaced?by?environment?variable?'H',?example:?-h='192.168.1.10,192.168.1.11' -P[36000/10,65535]:?Specifies?the?port?to?connect?to?on?the?remote?machines,?can?be?replaced?by?environment?variable?'PORT' -u[]:?Specifies?the?user?to?log?in?as?on?the?remote?machines,?can?be?replaced?by?environment?variable?'U' -p[]:?The?password?to?use?when?connecting?to?the?remote?machines,?can?be?replaced?by?environment?variable?'P' -t[60/1,65535]:?The?number?of?seconds?before?connection?timeout -c[]:?The?command?is?executed?on?the?remote?machines,?example:?-c='grep?ERROR?/tmp/*.log' -v[1/0,2]:?Verbosity,?how?much?troubleshooting?info?to?print |
?
2.?批量執行命令工具:mooon_ssh
| 參數名 | 默認值 | 說明 |
| -u | 無 | 用戶名參數,可用環境變量U替代 |
| -p | 無 | 密碼參數,可用環境變量P替代 |
| -h | 無 | IP列表參數,可用環境變量H替代 |
| -P | 22,可修改源碼,編譯為常用端口號 | SSH端口參數,可用環境變量PORT替代 |
| -c | 無 | 在遠程機器上執行的命令,建議單引號方式指定值,除非要執行的命令本身已經包含了單引號有沖突。使用雙引號時,要注意轉義,否則會被本地shell解釋 |
| -v | 1 | 工具輸出的詳細度 |
3.?批量上傳文件工具:mooon_upload
| 參數名 | 默認值 | 說明 |
| -u | 無 | 用戶名參數,可用環境變量U替代 |
| -p | 無 | 密碼參數,可用環境變量P替代 |
| -h | 無 | IP列表參數,可用環境變量H替代 |
| -P | 22,可修改源碼,編譯為常用端口號 | SSH端口參數,可用環境變量PORT替代 |
| -s | 無 | 以逗號分隔的,需要上傳的本地文件列表,可以帶相對或絕對目錄 |
| -d | 無 | 文件上傳到遠程機器的目錄,只能為單個目錄 |
4.?使用示例
4.1.?使用示例1:上傳/etc/hosts
| mooon_upload?-s=/etc/hosts?-d=/etc |
4.2.?使用示例2:檢查/etc/profile文件是否一致
| mooon_ssh?-c='md5sum?/etc/hosts' |
4.3.?使用示例3:批量查看crontab
| mooon_ssh?-c='crontab?-l' |
4.4.?使用示例4:批量清空crontab
| mooon_ssh?-c='rm?-f?/tmp/crontab.empty;touch?/tmp/crontab.empty' mooon_ssh?-c='crontab?/tmp/crontab.emtpy' |
4.5.?使用示例5:批量更新crontab
| mooon_ssh?-c='crontab?/tmp/crontab.online' |
4.6.?使用示例6:取遠端機器IP
因為awk用單引號,所以參數“-c”的值不能使用單引號,所以內容需要轉義,相對其它來說要復雜點:
| mooon_ssh?-c="netstat?-ie?|?awk?-F[\\?:]+?'BEGIN{ok=0;}{if?(match(\$0,?\"eth1\"))?ok=1;?if?((1==ok)?&&?match(\$0,\"inet\"))?{?ok=0;?if?(7==NF)?printf(\"%s\\n\",\$3);?else?printf(\"%s\\n\",\$4);}?}'" |
?
不同的環境,IP在“netstat?-ie”輸出中的位置稍有不同,所以awk中加了“7==NF”判斷,但仍不一定適用于所有的環境。需要轉義的字符包含:雙引號、美元符和斜杠。
4.7.?使用示例7:批量查看kafka進程(環境變量方式)
| $?export?H=192.168.31.9,192.168.31.10,192.168.31.11,192.168.31.12,192.168.31.13 $?export?U=kafka $?export?P='123456' ? $?mooon_ssh?-c='/usr/local/jdk/bin/jps?-m' [192.168.31.15] 50928?Kafka?/data/kafka/config/server.properties 125735?Jps?-m [192.168.31.15]?SUCCESS ? [192.168.31.16] 147842?Jps?-m 174902?Kafka?/data/kafka/config/server.properties [192.168.31.16]?SUCCESS ? [192.168.31.17] 51409?Kafka?/data/kafka/config/server.properties 178771?Jps?-m [192.168.31.17]?SUCCESS ? [192.168.31.18] 73568?Jps?-m 62314?Kafka?/data/kafka/config/server.properties [192.168.31.18]?SUCCESS ? [192.168.31.19] 123908?Jps?-m 182845?Kafka?/data/kafka/config/server.properties [192.168.31.19]?SUCCESS ? ? ================================ [192.168.31.15?SUCCESS]?0?seconds [192.168.31.16?SUCCESS]?0?seconds [192.168.31.17?SUCCESS]?0?seconds [192.168.31.18?SUCCESS]?0?seconds [192.168.31.19?SUCCESS]?0?seconds SUCCESS:?5,?FAILURE:?0 |
?
4.8.?使用示例8:批量停止kafka進程(參數方式)
| $?mooon_ssh?-c='/data/kafka/bin/kafka-server-stop.sh'?-u=kafka?-p='123456'?-h=192.168.31.15,192.168.31.16,192.168.31.17,192.168.31.18,192.168.31.19 [192.168.31.15] No?kafka?server?to?stop command?return?1 ? [192.168.31.16] No?kafka?server?to?stop command?return?1 ? [192.168.31.17] No?kafka?server?to?stop command?return?1 ? [192.168.31.18] No?kafka?server?to?stop command?return?1 ? [192.168.31.19] No?kafka?server?to?stop command?return?1 ? ================================ [192.168.31.15?FAILURE]?0?seconds [192.168.31.16?FAILURE]?0?seconds [192.168.31.17?FAILURE]?0?seconds [192.168.31.18?FAILURE]?0?seconds [192.168.31.19?FAILURE]?0?seconds SUCCESS:?0,?FAILURE:?5 |
5.?如何編譯批量工具?
5.1.?GO版本
依賴的crypto包,從https://github.com/golang/crypto下載,放到目錄$GOPATH/src/golang.org/x或$GOROOT/src/golang.org/x下。注意需要先創建好目錄$GOROOT/src/golang.org/x,然后在此目錄下解壓crypto包。如果下載的包名為crypto-master.zip,則解壓后的目錄名為crypto-master,需要重命名為crypto。
安裝crypto包示例:
| 1)安裝go cd?/usr/local tar?xzf?go1.10.3.linux-386.tar.gz 2)mkdir?-p?go/golang.org/x 3)cd?go/golang.org/x 4)unzip?crypto-master.zip 5)mv?crypto-master?crypto |
?
命令行執行“go?help?gopath”可了解gopath,或執行“go?env”查看當前的設置。編譯方法:
| go?build?-o?mooon_ssh?mooon_ssh.go |
?
上述編譯會依賴glibc,如果不想依賴,這樣編譯:
| go?build?-o?mooon_ssh?-ldflags?'-linkmode?"external"?-extldflags?"-static"'?mooon_ssh.go |
5.2.?C++版本
C++版本為libmooon組成部分,編譯libmooon即可得到mooon_ssh、mooon_upload和mooon_download。但libmooon依賴libssh2,而libssh2又依賴openssl,所以需要先依次安裝好openssl和libssh2。
libssh2下載地址:http://www.libssh2.org。
?
1)?openssl編譯安裝方法
解壓后進入openssl源碼目錄,以版本openssl-1.0.2i為例,依次執行:
| ./config?--prefix=/usr/local/openssl-1.0.2i?shared?threads make make?install ln?-s?/usr/local/openssl-1.0.2i?/usr/local/openssl |
?
2)?libssh2編譯安裝方法
解壓后進入libssh2源碼目錄,以版本libssh2-1.6.0為例,依次執行:
| ./configure?--prefix=/usr/local/libssh2-1.6.0?--with-libssl-prefix=/usr/local/openssl make make?install |
?
注意:libssh2和比較新版本的openssl可能存在兼容問題。
?
3)?libmooon編譯方法
采用cmake編譯,所以需要先安裝好cmake,并要求cmake版本不低于2.8.11,可執行“cmake?--version”查看cmake版本,當cmake、libssh2和openssl準備好后執行下列命令編譯libmooon即可得到批量工具:
| cmake?-DCMAKE_BUILD_TYPE=Debug?-DCMAKE_INSTALL_PREFIX=/usr/local/mooon?. make make?install |
?
在make一步成功后,即可在tools子目錄中找到mooon_ssh、mooon_upload和mooon_download,實踐中mooon_download可能使用得比較少。
總結
以上是生活随笔為你收集整理的Linux远程批量工具mooon_ssh和mooon_upload使用示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何利用Python自动生成PPT
- 下一篇: Mifare Classic Tool(