linux shell實現(xiàn)批量主機遠程執(zhí)行命令腳本
基于expect命令實現(xiàn)
1.安裝expect
[root@logstash ~]# yum install -y expect Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Package expect-5.45-14.el7_1.x86_64 already installed and latest version Nothing to do [root@logstash ~]#
2.撰寫腳本
expect_command.sh
#!/bin/bash command=$* host_info=/root/host.info for ip in $(awk '/^[^#]/{print $1}' $host_info) do user=$(awk -v ip=$ip 'ip==$1{print $2}' $host_info) port=$(awk -v ip=$ip 'ip==$1{print $3}' $host_info) pass=$(awk -v ip=$ip 'ip==$1{print $4}' $host_info) expect -c " spawn ssh -p $port $user@$ip expect { \"(yes/no)\" {send \"yes\r\";exp_continue} \"password:\" {send \"$pass\";exp_continue} \"$user@*\" {send \"$command\r exit\r\";exp_continue} } " echo "------Execute Successful!------" done
linux exp_continue是一個在 Linux 系統(tǒng)中經(jīng)常用到的命令。在 Linux 系統(tǒng)中,exp_continue 命令用來控制 expect 腳本的循環(huán)執(zhí)行,以及在不同條件下執(zhí)行不同的操作。通過使用 exp_continue 命令,用戶可以讓 expect 腳本在滿足特定條件時繼續(xù)執(zhí)行下一個步驟,而不是中斷腳本的執(zhí)行。
exp_continue 命令的用法非常簡單,只需在 expect 腳本中使用該命令即可。例如,當用戶在 expect 腳本中需要等待用戶輸入密碼時,可以使用 exp_continue 命令讓腳本繼續(xù)執(zhí)行下一個步驟,而不是等待超時或中斷腳本。這種方式可以提高腳本的效率和可靠性。
另外,exp_continue 命令還可以用來處理不同情況下的邏輯分支。例如,當用戶在 expect 腳本中需要對不同的返回結果做出不同的處理時,可以使用 exp_continue 命令實現(xiàn)邏輯的分支跳轉(zhuǎn)。這種方式可以讓 expect 腳本更加靈活和智能。
[root@logstash ~]# chmod +x expect_command.sh [root@logstash ~]# ls -l expect_command.sh -rwxr-xr-x 1 root root 535 Jul 18 10:20 expect_command.sh [root@logstash ~]#
host.info
#格式:ip地址 用戶名 端口號 密碼 192.168.10.245 root 22 root
3.驗證
sh expect_command.sh df -h
[root@logstash ~]# sh expect_command.sh df -h spawn ssh -p 22 root@192.168.10.245 Last login: Thu Jul 18 11:34:50 2024 from 192.168.10.131 [root@kibana ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 2.0G 0 2.0G 0% /dev tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 2.0G 13M 2.0G 1% /run tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/mapper/centos-root 38G 7.7G 30G 21% / /dev/sr0 4.4G 4.4G 0 100% /mnt /dev/sda1 1014M 172M 843M 17% /boot /dev/mapper/centos-home 19G 37M 19G 1% /home tmpfs 394M 12K 394M 1% /run/user/42 tmpfs 394M 0 394M 0% /run/user/0 [root@kibana ~]# exit logout Connection to 192.168.10.245 closed. df -h exit ------Execute Successful!------ [root@logstash ~]#
sh expect_command.sh vmstat
[root@logstash ~]# sh expect_command.sh vmstat spawn ssh -p 22 root@192.168.10.245 Last login: Thu Jul 18 11:36:21 2024 from 192.168.10.131 [root@kibana ~]# vmstat procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 0 2939704 1096 471976 0 0 391 8 170 358 0 1 99 0 0 [root@kibana ~]# exit logout Connection to 192.168.10.245 closed. vmstat exit ------Execute Successful!------ [root@logstash ~]#
以上就是linux shell實現(xiàn)批量主機遠程執(zhí)行命令腳本的詳細內(nèi)容,更多關于linux shell主機遠程執(zhí)行命令的資料請關注腳本之家其它相關文章!
相關文章
利用Shell腳本循環(huán)讀取文件中每一行的方法詳解
讀取文件是我們在日常工作中經(jīng)常遇到的一個需求,下面這篇文章主要給大家介紹了關于利用Shell腳本循環(huán)讀取文件中每一行的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習下吧。2017-09-09Linux使用iptables實現(xiàn)屏蔽ip地址的示例詳解
netfilter/iptables(簡稱為iptables)組成Linux平臺下的包過濾防火墻,與大多數(shù)的Linux軟件一樣,這個包過濾防火墻是免費的。本文就來利用iptables實現(xiàn)屏蔽ip地址,需要的可以參考一下2022-10-10shell監(jiān)控linux系統(tǒng)進程創(chuàng)建腳本分享
shell監(jiān)控linux系統(tǒng)進程創(chuàng)建腳本,大家參考使用吧2013-12-12