linux shell實(shí)現(xiàn)批量主機(jī)遠(yuǎn)程執(zhí)行命令腳本
基于expect命令實(shí)現(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是一個(gè)在 Linux 系統(tǒng)中經(jīng)常用到的命令。在 Linux 系統(tǒng)中,exp_continue 命令用來控制 expect 腳本的循環(huán)執(zhí)行,以及在不同條件下執(zhí)行不同的操作。通過使用 exp_continue 命令,用戶可以讓 expect 腳本在滿足特定條件時(shí)繼續(xù)執(zhí)行下一個(gè)步驟,而不是中斷腳本的執(zhí)行。
exp_continue 命令的用法非常簡單,只需在 expect 腳本中使用該命令即可。例如,當(dāng)用戶在 expect 腳本中需要等待用戶輸入密碼時(shí),可以使用 exp_continue 命令讓腳本繼續(xù)執(zhí)行下一個(gè)步驟,而不是等待超時(shí)或中斷腳本。這種方式可以提高腳本的效率和可靠性。
另外,exp_continue 命令還可以用來處理不同情況下的邏輯分支。例如,當(dāng)用戶在 expect 腳本中需要對不同的返回結(jié)果做出不同的處理時(shí),可以使用 exp_continue 命令實(shí)現(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.驗(yàn)證
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實(shí)現(xiàn)批量主機(jī)遠(yuǎn)程執(zhí)行命令腳本的詳細(xì)內(nèi)容,更多關(guān)于linux shell主機(jī)遠(yuǎn)程執(zhí)行命令的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
利用Shell腳本循環(huán)讀取文件中每一行的方法詳解
讀取文件是我們在日常工作中經(jīng)常遇到的一個(gè)需求,下面這篇文章主要給大家介紹了關(guān)于利用Shell腳本循環(huán)讀取文件中每一行的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)下吧。2017-09-09檢查Linux系統(tǒng)中文件大小的方法總結(jié)
在Linux操作系統(tǒng)中,掌握如何高效檢查文件大小是每位開發(fā)者和系統(tǒng)管理員的必備技能,本文詳細(xì)介紹了四種檢查Linux文件大小的方法,感興趣的朋友可以參考下2024-03-03Linux使用iptables實(shí)現(xiàn)屏蔽ip地址的示例詳解
netfilter/iptables(簡稱為iptables)組成Linux平臺下的包過濾防火墻,與大多數(shù)的Linux軟件一樣,這個(gè)包過濾防火墻是免費(fèi)的。本文就來利用iptables實(shí)現(xiàn)屏蔽ip地址,需要的可以參考一下2022-10-10shell監(jiān)控linux系統(tǒng)進(jìn)程創(chuàng)建腳本分享
shell監(jiān)控linux系統(tǒng)進(jìn)程創(chuàng)建腳本,大家參考使用吧2013-12-12