批量管理的第一步,都須要進行免密碼登錄。python
[blackfox@localhost ~]$ mkdir ~/.ssh [blackfox@localhost ~]$ chmod 700 ~/.ssh [blackfox@localhost ~]$ cd .ssh [blackfox@localhost .ssh]$ ssh-keygen -t rsa -P # -P 密碼,生成id_rsa和id_rsa.pub [blackfox@localhost .ssh]$ cat id_rsa.pub > authorized_keys [blackfox@localhost .ssh]$ chmod 600 authorized_keys [blackfox@localhost .ssh]$ scp -P 52113 authorized_keys blackfox@192.168.1.4:/home/blackfox/.ssh # -P 端口 [blackfox@localhost .ssh]$ ssh -p 52113 192.168.1.4 date # 測試
核心功能:上傳、下載、同步、跑ssh命令shell
批量在遠程主機上上傳、下載文件時,推薦使用服務器
[blackfox@localhost ~]$ python setup.py install # 安裝python # 下載pssh:https://pypi.python.org/pypi/pssh/2.3.1,上傳服務器 [blackfox@localhost batch-tools]$ tar zxvf pssh-2.3.1.tar.gz [root@localhost pssh-2.3.1]$ python setup.py install [blackfox@localhost pssh-2.3.1]$ pssh -H blackfox@192.168.1.4:52113 -P date # 查看遠程主機時間
[blackfox@localhost pssh-2.3.1]$ pssh -i -h /home/blackfox/batch-tools/pssh-2.3.1/hosts "uptime" # 查看遠程主機負載 # 調用sudo命令時,須要設置遠程終端sudo免密碼 [root@localhost blackfox]# visudo # 修改 blackfox ALL=(ALL) NOPASSWD: ALL # 註釋 Defaults requiretty # sudo默認tty終端運行,註釋後能夠在後臺運行 [blackfox@localhost ~]$ pssh -i -x "-l blackfox" "-p 52113" -h /home/blackfox/batch-tools/pssh-2.3.1/hosts -o info "uptime;uname -a" # -x傳多個參數,用空格分開;-l指定用戶;-p指定最大線程數;-o重定向內容輸出到info文件夾,多個ssh命令用';'隔開。
[blackfox@localhost ~]$ pnuke -h ~/batch-tools/pssh-2.3.1/hosts httpd # 相似killall,關閉全部遠程主機上的httpd服務
[blackfox@localhost ~]$ pscp -h ~/batch-tools/pssh-2.3.1/hosts -r ~/test ~/test # 本機test目錄,複製到遠程的test目錄
[blackfox@localhost ~]$ pslurp -h ~/batch-tools/pssh-2.3.1/hosts -r -L ~/test ~/test new # 遠程test目錄,複製到本機test目錄下192.168.1.4目錄下,更名new。 -L指定本機目錄
[blackfox@localhost ~]$ prsync -h ~/batch-tools/pssh-2.3.1/hosts -az -r ~/test ~/test # 經過rsync協議同步,-az保持屬性,壓縮傳輸
批量執行ssh命令,腳本ssh
執行腳本時,推薦使用分佈式
# 下載地址:https://sourceforge.net/projects/mussh/files/latest/download # 直接解壓 [root@localhost mussh]# ln mussh /usr/local/bin/mussh # 建立硬連接 [root@localhost bin]# mussh -V
[blackfox@localhost bin]$ mussh -o "port=52113" -h blackfox@192.168.1.4 -c uptime # -c執行ssh命令,多個命令用';'隔開批 [blackfox@localhost mussh]$ mussh -o "port=52113" -H hosts -l blackfox -c date # -H讀取文件的主機列表 -l用戶名,也能夠在文件中填寫 [blackfox@localhost ~]$ mussh -o "port=52113" -H ~/batch-tools/mussh/hosts -s /usr/bin/sh -C "/home/blackfox/bin/learn_shell/1.sh" # -C執行腳本,不能夠用相對路徑,如~ -s執行腳本的shell路徑(默認:sh)
批量配置、部署、文件複製等功能測試
支持交互模式,執行不肯定命令時,推薦使用ui
# 下載地址:https://sourceforge.net/projects/pdsh/files/latest/download [root@localhost pdsh-2.26]# ./configure --with-ssh --with-rsh --with-mrsh --with-mqshell --with-dshgroups --with-machines=/etc/pdsh/machines # --with-[module]啓用模塊;--with-dshgroups將一組主機列表寫入~/.dsh/group或/etc/dsh/group目錄下;--with-machines是--with-dshgroups的擴展,將要管理的主機寫到/etc/pdsh/machines文件中。 [root@localhost pdsh-2.26]# make [root@localhost pdsh-2.26]# make install [root@localhost pdsh-2.26]# pdsh -V