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!------"
donelinux 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 命令的用法非常簡(jiǎn)單,只需在 expect 腳本中使用該命令即可。例如,當(dāng)用戶在 expect 腳本中需要等待用戶輸入密碼時(shí),可以使用 exp_continue 命令讓腳本繼續(xù)執(zhí)行下一個(gè)步驟,而不是等待超時(shí)或中斷腳本。這種方式可以提高腳本的效率和可靠性。
另外,exp_continue 命令還可以用來處理不同情況下的邏輯分支。例如,當(dāng)用戶在 expect 腳本中需要對(duì)不同的返回結(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地址 用戶名 端口號(hào) 密碼 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í)行命令的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Shell $0, $#, $*, $@, $?, $$和命令行參數(shù)的使用
這篇文章主要介紹了Shell $0, $#, $*, $@, $?, $$和命令行參數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
在 Bash 中循環(huán)遍歷文件內(nèi)容的不同方法
本文介紹了在Bash腳本中遍歷文件內(nèi)容的幾種方法,包括使用while循環(huán)和read命令逐行讀取文件,使用for循環(huán)和cat命令讀取整個(gè)文件內(nèi)容,使用IFS變量進(jìn)行單詞循環(huán),以及使用awk命令進(jìn)行更高級(jí)的文本處理,每種方法都有其適用的場(chǎng)景和優(yōu)缺點(diǎn),感興趣的朋友跟隨小編一起看看吧2025-12-12
開發(fā)者常用及實(shí)用Linux Shell命令備忘錄(小結(jié))
這篇文章主要介紹了開發(fā)者常用及實(shí)用Linux Shell命令備忘錄(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
程序中獲取linux系統(tǒng)啟動(dòng)時(shí)間方法
需要在應(yīng)用程序獲取系統(tǒng)的啟動(dòng)時(shí)間,通過sysinfo中的uptime可以計(jì)算出系統(tǒng)的啟動(dòng)時(shí)間,下面介紹一下獲取方法,大家參考使用吧2014-01-01

