欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

linux shell實(shí)現(xiàn)批量主機(jī)遠(yuǎn)程執(zhí)行命令腳本

 更新時(shí)間:2024年09月13日 09:22:55   作者:小黑要上天  
這篇文章主要介紹了linux shell實(shí)現(xiàn)批量主機(jī)遠(yuǎn)程執(zhí)行命令腳本,文章通過代碼示例講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下

基于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)文章

  • 每天一個(gè)linux命令 whereis命令

    每天一個(gè)linux命令 whereis命令

    這篇文章主要為大家詳細(xì)介紹了一個(gè)linux命令:whereis命令,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • shell實(shí)現(xiàn)貪吃蛇的示例代碼

    shell實(shí)現(xiàn)貪吃蛇的示例代碼

    本文主要介紹了shell實(shí)現(xiàn)貪吃蛇的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • 利用Shell腳本循環(huá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
  • shell腳本退出的正確方式與最佳實(shí)踐

    shell腳本退出的正確方式與最佳實(shí)踐

    這篇文章主要給大家介紹了關(guān)于shell腳本退出的正確方式與最佳實(shí)踐,shell是linux系統(tǒng)必備工具,在linux系統(tǒng)里打開終端或者使用ssh連接時(shí)都是使用命令語言作為交互支撐,需要的朋友可以參考下
    2023-07-07
  • 檢查Linux系統(tǒng)中文件大小的方法總結(jié)

    檢查Linux系統(tǒng)中文件大小的方法總結(jié)

    在Linux操作系統(tǒng)中,掌握如何高效檢查文件大小是每位開發(fā)者和系統(tǒng)管理員的必備技能,本文詳細(xì)介紹了四種檢查Linux文件大小的方法,感興趣的朋友可以參考下
    2024-03-03
  • Linux中的grep?-v、-e、-E用法小結(jié)

    Linux中的grep?-v、-e、-E用法小結(jié)

    grep是一種強(qiáng)大的文本搜索工具,它能使用正則表達(dá)式搜索文本,并把匹配的行打印出來,這篇文章主要介紹了Linux之grep?-v、-e、-E用法總結(jié),需要的朋友可以參考下
    2022-11-11
  • Linux bash Shell中的變量類型詳解

    Linux bash Shell中的變量類型詳解

    這篇文章主要介紹了Linux bash Shell中的變量類型詳解,變量類型共分為本地變量、局部變量、環(huán)境變量、位置變量和特殊變量等,需要的朋友可以參考下
    2015-06-06
  • Linux使用iptables實(shí)現(xiàn)屏蔽ip地址的示例詳解

    Linux使用iptables實(shí)現(xiàn)屏蔽ip地址的示例詳解

    netfilter/iptables(簡稱為iptables)組成Linux平臺下的包過濾防火墻,與大多數(shù)的Linux軟件一樣,這個(gè)包過濾防火墻是免費(fèi)的。本文就來利用iptables實(shí)現(xiàn)屏蔽ip地址,需要的可以參考一下
    2022-10-10
  • sed模式空間和暫存空間的區(qū)別

    sed模式空間和暫存空間的區(qū)別

    很多朋友對sed模式空間和暫存空間的區(qū)別不是很清楚,這里簡單的介紹下,方便需要的朋友
    2013-01-01
  • shell監(jiān)控linux系統(tǒng)進(jìn)程創(chuàng)建腳本分享

    shell監(jiān)控linux系統(tǒng)進(jìn)程創(chuàng)建腳本分享

    shell監(jiān)控linux系統(tǒng)進(jìn)程創(chuàng)建腳本,大家參考使用吧
    2013-12-12

最新評論