亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

shell中expect的實現(xiàn)示例

 更新時間:2025年09月09日 09:29:23   作者:一周困?天.  
Expect是一個實現(xiàn)自動化交互任務(wù)的工具,通過模擬用戶輸入完成交互程序執(zhí)行,本文就來介紹一下shell中expect的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下

一、概述

Expect是一個免費的編程工具語言,用來實現(xiàn)自動和交互式任務(wù)進行通信,而無需人的干預(yù)。Expect的作者DonLibes在1990年開始編寫Expect時對Expect做有如下定義:Expect是一個用來實現(xiàn)自動交互功能的軟件套件。通過expect系統(tǒng)管理員可以創(chuàng)建腳本用來實現(xiàn)對命令或程序提供輸入,而這些命令和程序是期望從終端(terminal)得到輸入,一般來說這些輸入都需要手工輸入進行的。Expect則可以根據(jù)程序的提示模擬標準輸入提供給程序需要的輸入來實現(xiàn)交互程序執(zhí)行。甚至可以實現(xiàn)實現(xiàn)簡單的BBS聊天機器人。

我們通過Shell可以實現(xiàn)簡單的控制流功能,如:循環(huán)、判斷等。但是對于需要交互的場合則必須通過人工來干預(yù),有時候我們可能會需要實現(xiàn)與SSH、FTP服務(wù)器等進行免交互的自動連接功能,而Expect正是用來實現(xiàn)這種功能的工具。

Expect是不斷發(fā)展的,隨著時間的流逝,其功能越來越強大,已經(jīng)成為系統(tǒng)管理員的的一個強大助手。Expect需要Tcl編程語言的支持,所以要在系統(tǒng)上運行Expect必須首先安裝Tcl。

通過ssh遠程操控主機時交互解決方式:

  • 通過ssh的密鑰對
  • 通過sshpass工具提交密碼
  • 通過expect工具提交密碼

二、安裝expect

[root@localhost ~]# rpm -q expect
?
[root@localhost ~]# yum -y install expect

三、如何使用expect

在使用expect程序前,我們首先簡單說下expect在常規(guī)使用中的工作流程:

首先expect的內(nèi)部命令spawn啟動指定進程-->expect獲取期待的關(guān)鍵字-->內(nèi)部命令send向指定進程發(fā)送響應(yīng)內(nèi)容-->進程執(zhí)行完成后,退出expect程序。

通過上面expect的工作流程,我們可以認識到,expect的內(nèi)置命令:spawn、expect、send等非常重要,所以下面我們先來了解下它們的使用。

3-1、spawn命令

spawn作用:啟動新的產(chǎn)生交互的進程

spawn命令的語法:

spawn [選項] [需要執(zhí)行的shell命令或程序等]

下面我們以修改一個已存在賬戶的密碼為例,舉例如下:

[root@localhost ~]# useradd tom
?
[root@localhost ~]# passwd tom
?
更改用戶 tom 的密碼 。
?
新的 密碼:
?
無效的密碼: 密碼未通過字典檢查 - 過于簡單化/系統(tǒng)化
?
重新輸入新的 密碼:
?
passwd:所有的身份驗證令牌已經(jīng)成功更新。

基于expect交互界面做法

[root@localhost ~]# expect
?
expect1.1> spawn passwd tom
?
spawn passwd tom
?
1388
?
expect1.2> expect "密碼:"
?
更改用戶 tom 的密碼 。
?
新的 密碼:expect1.3> send "123456\r"
?
expect1.4> expect "密碼:"
?
無效的密碼: 密碼少于 8 個字符
?
重新輸入新的 密碼:expect1.5> send "123456\r"
?
expect1.6> expect eof
?
passwd:所有的身份驗證令牌已經(jīng)成功更新。
?
expect1.7> exit

3-2、expect命令

expect作用:獲取從spawn命令執(zhí)行的命令和程序后產(chǎn)生的交互信息。看看是否匹配,如果匹配,就開始執(zhí)行expect進程接收字符串。

expect命令的語法:

  expect [選項] 表達式 [動作]

  選項:比如"-re"表示使用正則表達式來進行匹配。

案例:

[root@localhost ~]# expect
?
expect1.1> spawn passwd tom
?
spawn passwd tom
?
1437
?
expect1.2> expect "新的 密碼:"
?
更改用戶 tom 的密碼 。
?
新的 密碼:expect1.3> send "123456\r"
?
expect1.4> expect "新的 密碼:"
?
無效的密碼: 密碼少于 8 個字符
?
重新輸入新的 密碼:expect1.5> send "123456\r"
?
expect1.6> expect eof
?
passwd:所有的身份驗證令牌已經(jīng)成功更新。
?
expect1.7> exit

3-3、send命令

send命令的主要作用是,在expect命令匹配完指定的字符后,發(fā)送指定的字符串給系統(tǒng)程序,在字符中可以支持部分特殊轉(zhuǎn)義符,比如:n(回車)r(換行)t(制表符)等。

案例:

[root@localhost ~]# expect
?
expect1.1> spawn passwd tom
?
spawn passwd tom
?
1437
?
expect1.2> expect "新的 密碼:"
?
更改用戶 tom 的密碼 。
?
新的 密碼:expect1.3> send "123456\r"
?
expect1.4> expect "新的 密碼:"
?
無效的密碼: 密碼少于 8 個字符
?
重新輸入新的 密碼:expect1.5> send "123456\r"
?
expect1.6> expect eof
?
passwd:所有的身份驗證令牌已經(jīng)成功更新。
?
expect1.7> exit

下面是上述實驗的expect腳本案例:

修改密碼案例1:

[root@localhost ~]# vim change_pass1.exp
?
#!/usr/bin/expect
?
# Filename : change_pass1.exp
?
spawn passwd tom
?
expect "密碼:"
?
send "123456\r"
?
expect "新的 密碼:"
?
send "123456\r"
?
expect eof
?
[root@localhost ~]# chmod +x change_pass1.exp
?
[root@localhost ~]# ./change_pass1.exp
?
spawn passwd tom
?
更改用戶 tom 的密碼 。
?
新的 密碼:
?
無效的密碼: 密碼少于 8 個字符
?
重新輸入新的 密碼:
?
passwd:所有的身份驗證令牌已經(jīng)成功更新。

修改密碼案例2:

[root@localhost ~]# vim chang_pass2.exp
?
#!/usr/bin/expect
?
# Filename : change_pass2.exp
?
spawn passwd tom
?
expect {
?
"新的 密碼:" { send "123456\r"; exp_continue }
?
"新的 密碼:" { send "123456\r" }
?
eof
?
}
?
[root@localhost ~]# chmod +x chang_pass2.exp
?
[root@localhost ~]# ./chang_pass2.exp
?
spawn passwd tom
?
更改用戶 tom 的密碼 。
?
新的 密碼:
?
無效的密碼: 密碼少于 8 個字符
?
重新輸入新的 密碼:
?
passwd:所有的身份驗證令牌已經(jīng)成功更新。

3-4、exp_continue命令

exp_continue命令的主要作用是,如果需要一次匹配多個字符串,那么多次匹配字符串并執(zhí)行不同的動作中,可以讓expect程序?qū)崿F(xiàn)繼續(xù)匹配的效果。

案例:

[root@localhost ~]# vim chang_pass2.exp
?
#!/usr/bin/expect
?
# Filename : change_pass2.exp
?
spawn passwd tom
?
expect {
?
"新的 密碼:" { send "123456\r"; exp_continue }
?
"新的 密碼:" { send "123456\r" }
?
eof
?
}
?
[root@localhost ~]# chmod +x chang_pass2.exp
?
[root@localhost ~]# ./chang_pass2.exp
?
spawn passwd tom
?
更改用戶 tom 的密碼 。
?
新的 密碼:
?
無效的密碼: 密碼少于 8 個字符
?
重新輸入新的 密碼:
?
passwd:所有的身份驗證令牌已經(jīng)成功更新。

3-5、send_user命令

send_user命令的作用是:用來打印expect腳本信息,類似shell里的echo命令。

[root@localhost ~]# vim send_user.exp
?
#!/usr/bin/expect
?
#Filename: send_user.exp
?
send_user "beijingn"
?
send_user "shanghait"
?
send_user "guangzhoun"
?
[root@localhost ~]# chmod +x send_user.exp
?
[root@localhost ~]# ./send_user.exp
?
beijing
?
shanghai guangzhou

3-6、expect變量

3-6-1、普通變量

expect中定義普通變量的語法如下:

set 變量名 變量值

例如:

set chengshi "beijing"

調(diào)取變量的方法是:

puts $變量名
?
#或者
?
send_user "$變量名"

案例:

[root@localhost ~]# vim expect_var.exp
?
#!/usr/bin/expect
?
# Filename: expect_var.exp
?
set name "crushlinux"
?
set address "beijing"
?
puts $name
?
send_user "$name t $addressn"
?
[root@localhost ~]# chmod +x expect_var.exp
?
[root@localhost ~]# ./expect_var.exp
?
crushlinux
?
crushlinux beijing

3-6-2、expect中位置參數(shù)變量

如何向expect腳本中像shell一樣傳遞類似于$0、$1等位置參數(shù),用于接收及控制expect腳本傳遞位置參數(shù)變量呢?

expect是通過如下語法來進行的:

set <變量名稱> [lindex $argv <param index> ]

當然,除了基本的位置參數(shù)外,expect還支持其它的特殊參數(shù),比如:

$argc 表示傳入?yún)?shù)的個數(shù)

$argv0 表示當前執(zhí)行腳本的名稱。

案例:

[root@localhost ~]# vim expect_var2.exp
?
#!/usr/bin/expect
?
# Filename: expect_var2.exp
?
set file1 [lindex $argv 0]
?
set file2 [lindex $argv 1]
?
set file3 [lindex $argv 2]
?
puts "The files are : $file1 $file2 $file3"
?
puts "The number of files are: $argc"
?
puts "The expect script name is: $argv0"
?
[root@localhost ~]# chmod +x expect_var2.exp
?
[root@localhost ~]# ./expect_var2.exp 1.txt 2.txt 3.txt
?
The files are : 1.txt 2.txt 3.txt
?
The number of files are: 3
?
The expect script name is: ./expect_var2.exp

3-7、expect中if條件語句

expect中if 條件語句的語法結(jié)構(gòu)如下:

if {條件表達式} {
?
commands;
?
}
?
if {條件表達式} {
?
commands;
?
} else {
?
commands;
?
}

注意事項:上面條件語句中每個"{"前要至少保證有一個空格。

案例:

[root@localhost ~]# vim expect-if.exp
?
#!/usr/bin/expect
?
# Filename: expect-if.exp
?
if {$argc <= 3} {
?
puts "The IP numbers <= 3"
?
} else {
?
puts "The IP numbers > 3"
?
}
?
[root@localhost ~]# chmod +x expect-if.sh
?
[root@localhost ~]# ./expect-if.sh 192.168.200.{1..3}
?
The IP numbers <= 3
?
[root@localhost ~]# ./expect-if.sh 192.168.200.{1..5}
?
The IP numbers > 3

3-8、expect中常用關(guān)鍵字

3-8-1 eof關(guān)鍵字

eof是和spawn對應(yīng)的,當spawn發(fā)送指令到終端執(zhí)行起始會有一個eof,等指令在終端完畢后,在返回時eof被expect捕捉,就好比在shell中cat >>file <<OEFrr content rrEOF一樣,在結(jié)束時也要有EOF,這樣是對應(yīng)的。因前面案例中已有舉例,這里就不再舉例說明。

Interact允許用戶交互,由管理員結(jié)束進程。

3-8-2 timeout關(guān)鍵字

expect腳本我們都知道,首先spawn我們要執(zhí)行的命令,然后就給出一堆expect的屏幕輸出,如果輸出匹配了我們的expect的正則匹配內(nèi)容,我們就會send一個命令上去,模擬用戶輸入。

但是expect中等待命令的輸出信息是有一個timeout的設(shè)定的,默認是10秒。這個特性是防止那些執(zhí)行死機的命令的。一旦到了這個timeout,還是沒有屏幕輸出的話,expect腳本中下面的代碼就會執(zhí)行?;蛘呶覀冊趀xpect腳本中如果定義了timeout的響應(yīng)代碼的話,這些代碼就會被執(zhí)行。

解決這樣的問題非常簡單,最簡單的辦法就是在expect腳本的開頭定義:

set timeout -1 -- 永久不超時
?
set timeout 0 -- 立即執(zhí)行
?
set timeout XX -- 設(shè)定具體的timeout時間(秒),默認是10秒。

案例1:

[root@localhost ~]# vim expect-timeout.exp
?
#!/usr/bin/expect
?
# Filename: expect-timeout.exp
?
spawn ssh root@192.168.200.112 ifconfig ens32
?
set timeout 15
?
expect {
?
"yes/no" { send "yesn"; exp_continue }
?
timeout { puts "request timeout 15s r" }
?
}
?
[root@localhost ~]# chmod +x expect-timeout.exp
?
[root@localhost ~]# ./expect-timeout.exp
?
spawn ssh root@192.168.200.112 ifconfig ens32
?
root@192.168.200.112's password: request timeout 15s
<--此處等待15秒后顯示。

案例2:

[root@localhost ~]# vim expect-timeout2.exp
?
#!/usr/bin/expect
?
# Filename: expect-timeout2.exp
?
spawn ssh root@192.168.200.112 ifconfig ens32
?
set timeout 15
?
expect {
?
"yes/no" { send "yesn"; exp_continue }
?
"*password*" { send "123456\r" }
?
}
?
expect eof
?
#interact
?
[root@localhost ~]# chmod +x expect-timeout.exp
?
[root@localhost ~]# ./expect-timeout.exp
?
spawn ssh root@192.168.200.112 ifconfig ens32
?
root@192.168.200.112's password:
?
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
?
inet 192.168.200.112 netmask 255.255.255.0 broadcast 192.168.200.255
?
inet6 fe80::20c:29ff:fe8c:f2d9 prefixlen 64 scopeid 0x20<link>
?
ether 00:0c:29:8c:f2:d9 txqueuelen 1000 (Ethernet)
?
RX packets 2472 bytes 1511193 (1.4 MiB)
?
RX errors 0 dropped 0 overruns 0 frame 0
?
TX packets 1714 bytes 211891 (206.9 KiB)
?
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

在上面的這個案例中沒有等待15秒才輸出結(jié)果,是因為中間沒有屏幕輸出需要交互等待15秒鐘的動作。

3-9、expect中for循環(huán)語句

{ set i 1 } 定義i的值為1
?
{ $i <= 10 }循環(huán)的條件
?
{ incr i 1} 制定$i的增量值,必須寫在這行的末尾處,默認增量值為1
[root@localhost ~]# cat expect-for.exp
?
#!/usr/bin/expect
?
for { set i 1 } { $i <= 10 } { incr i 1 } {
?
puts "$i"
?
}
?
[root@localhost ~]# expect expect-for.exp
?
1
?
2
?
3
?
4
?
5
?
6
?
7
?
8
?
9
?
10

3-10、expect中while循環(huán)語句

[root@localhost ~]# vim expect-while.exp
?
#!/usr/bin/expect
?
set i 1
?
while {$i <= 10} {
?
puts "$i"
?
sleep 1
?
incr i 1
?
}


[root@localhost ~]# expect expect-while.exp
?
1
?
2
?
3
?
4
?
5
?
6
?
7
?
8
?
9
?
10

四、Shell腳本調(diào)用expect 的方法

在shell腳本中使用expect --c "..."可以在shell中調(diào)用expect編程語言;

[root@localhost ~]# cat shell-expect1.sh
?
#!/bin/bash
?
for i in 192.168.200.{112..113}
?
do
?
expect -c "
?
spawn ssh root@$i ifconfig ens32
?
expect {
?
"yes/no" { send "yes\r";exp_continue }
?
"password" { send "123456\n" }
?
}
?
expect eof
?
"
?
done
?
[root@localhost ~]# bash shell-expect1.sh
?
spawn ssh root@192.168.200.112 ifconfig ens32
?
root@192.168.200.112's password:
?
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
?
inet 192.168.200.112 netmask 255.255.255.0 broadcast 192.168.200.255
?
inet6 fe80::20c:29ff:fe8c:f2d9 prefixlen 64 scopeid 0x20<link>
?
ether 00:0c:29:8c:f2:d9 txqueuelen 1000 (Ethernet)
?
RX packets 3512 bytes 1656192 (1.5 MiB)
?
RX errors 0 dropped 0 overruns 0 frame 0
?
TX packets 2808 bytes 365154 (356.5 KiB)
?
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
?
spawn ssh root@192.168.200.113 ifconfig ens32
?
root@192.168.200.113's password:
?
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
?
inet 192.168.200.113 netmask 255.255.255.0 broadcast 192.168.200.255
?
inet6 fe80::20c:29ff:fe0b:7eab prefixlen 64 scopeid 0x20<link>
?
ether 00:0c:29:0b:7e:ab txqueuelen 1000 (Ethernet)
?
RX packets 2722 bytes 1511052 (1.4 MiB)
?
RX errors 0 dropped 0 overruns 0 frame 0
?
TX packets 2196 bytes 258197 (252.1 KiB)
?
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

2.在shell腳本中使用/usr/bin/expect <<-EOF ...EOF的方式可以調(diào)用絕大多數(shù)的其它腳本語言,這種方式執(zhí)行命令建議使用絕對路徑,而且要嚴格遵守expect的腳本格式;

[root@localhost ~]# cat shell-expect2.sh
?
#!/bin/bash
?
for i in 192.168.200.{112..113}
?
do
?
/usr/bin/expect << EOF
?
spawn ssh root@$i ifconfig ens32
?
expect {
?
"yes/no" { send "yes\r";exp_continue }
?
"password" { send "123456\n" }
?
}
?
expect eof
?
EOF
?
done
?
[root@localhost ~]# bash shell-expect2.sh
?
spawn ssh root@192.168.200.112 ifconfig ens32
?
root@192.168.200.112's password:
?
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
?
inet 192.168.200.112 netmask 255.255.255.0 broadcast 192.168.200.255
?
inet6 fe80::20c:29ff:fe8c:f2d9 prefixlen 64 scopeid 0x20<link>
?
ether 00:0c:29:8c:f2:d9 txqueuelen 1000 (Ethernet)
?
RX packets 3329 bytes 1632688 (1.5 MiB)
?
RX errors 0 dropped 0 overruns 0 frame 0
?
TX packets 2609 bytes 340022 (332.0 KiB)
?
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
?
spawn ssh root@192.168.200.113 ifconfig ens32
?
root@192.168.200.113's password:
?
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
?
inet 192.168.200.113 netmask 255.255.255.0 broadcast 192.168.200.255
?
inet6 fe80::20c:29ff:fe0b:7eab prefixlen 64 scopeid 0x20<link>
?
ether 00:0c:29:0b:7e:ab txqueuelen 1000 (Ethernet)
?
RX packets 2561 bytes 1488208 (1.4 MiB)
?
RX errors 0 dropped 0 overruns 0 frame 0
?
TX packets 1997 bytes 233005 (227.5 KiB)
?
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
?
[root@localhost ~]# cat zhixing.sh
?
#!/bin/bash
?
for i in 192.168.200.{112..113}
?
do
?
/usr/bin/expect << EOF
?
spawn ssh root@$i wget http://192.168.200.111/user20.sh -O
/tmp/user20.sh && bash /tmp/user20.sh
?
expect {
?
"yes/no" { send "yesr";exp_continue }
?
"password" { send "123456n" }
?
}
?
expect eof
?
EOF
?
done

五、expect在生產(chǎn)環(huán)境中的案例

案例要求:

手邊現(xiàn)在有一個統(tǒng)計CPU信息的腳本(cpuinfo.sh),該腳本在單獨的物理服務(wù)器上或虛擬主機上執(zhí)行后,能統(tǒng)計該機器CPU的五個相關(guān)信息,比如:

[root@localhost ~]# sh cpuinfo.sh
?
logical CPU number in total: 32 //當前機器中邏輯CPU個數(shù)為:32
?
phycical CPU number in total: 2 //當前機器中物理CPU顆數(shù)為: 2
?
core number in a physical CPU: 8 //機器中每顆物理CPU有8核心
?
logical CPU number in a phycical CPU:16 //機器中每顆物理CPU 有16
個邏輯CPU
?
Hyper threading is enabled. //每顆CPU的超線程已經(jīng)打開

具體代碼如下:

[root@localhost ~]# cat cpuinfo.sh
?
#!/bin/bash
?
# filename: cpu-info.sh
?
#this script only works in a Linux system which has one or more
indentical physical CPU(S)
?
echo -n "logical CPU number in total: "
?
#邏輯CPU個數(shù)
?
cat /proc/cpuinfo |grep "^processor" |wc -l
?
#有些系統(tǒng)沒有多核也沒有打開超線程,就直接退出腳本
?
cat /proc/cpuinfo |grep -qi "core id"
?
if [ $? -ne 0 ]; then
?
echo "Warning. No multi-core or hyper-threading is enabled."
?
exit 0;
?
fi
?
echo -n "phycical CPU number in total: "
?
#物理CPU個數(shù)
?
cat /proc/cpuinfo |grep "physical id" |sort |uniq |wc -l
?
echo -n "core number in a physical CPU: "
?
#每個物理CPU上的core的個數(shù)(未計入超線程)
?
core_per_phy_cpu=$(cat /proc/cpuinfo |grep "core id"|sort |uniq
|wc -l)
?
echo $core_per_phy_cpu
?
echo -n "logical CPU number in a phycical CPU:"
?
#每個物理CPU中邏輯CPU(可能是core,threads或both)的個數(shù)
?
logical_cpu_per_phy_cpu=$(cat /proc/cpuinfo |grep "siblings"|sort
|uniq |awk -F: '{print $2}')
?
echo $logical_cpu_per_phy_cpu
?
#是否打開有超線程
?
#如果在同一個物理CPU上有兩個邏輯CPU具有相同的"core
id",那么超線程是打開的
?
#此處根據(jù)前面計算的 core_per_phy_cpu和
logical_cpu_per_phy_cpu的比較來查看超線程
?
if [ $logical_cpu_per_phy_cpu -gt $core_per_phy_cpu ]; then
?
echo "Hyper threading is enabled."
?
elif [ $logical_cpu_per_phy_cpu -eq $core_per_phy_cpu ]; then
?
echo "Hyper threading is NOT enabled."
?
else
?
echo "Error. There's something wrong."
?
fi

下面我們要做的是將這個腳本通過expect批量分發(fā)到指定范圍的機器上進行自動化跑批量,以便能通過shell腳本自動統(tǒng)計批量采集所有被指定機器上的CPU相關(guān)信息。具體腳本實現(xiàn)思路如下:

1)通過expect實現(xiàn)將單一腳本自動免交互上傳至指定客戶機。

2)通過shell腳本中的循環(huán)語句,調(diào)取expect單機自動免交互上傳文件腳本。從而實現(xiàn)將cpuinfo.sh腳本批量上傳到客戶機指定目錄下。

3)通過expect實現(xiàn)自動免交互方式在指定單臺客戶機上執(zhí)行指定命令或腳本程序。

4)通過shell腳本中的循環(huán)語句,調(diào)取單機免交互自動執(zhí)行腳本程序的expect腳本。從而實現(xiàn)在多臺機器上免交互自動執(zhí)行shell腳本。

腳本案例如下:

1)通過expect實現(xiàn)將單一腳本自動免交互上傳至指定客戶機。

[root@localhost ~]# vim auto_upload_file.exp
?
#!/usr/bin/expect
?
# Filename: auto_upload_file.exp
?
if { $argc != 3 } {
?
puts "usage: expect $argv0 file host dir"
?
exit
?
}
?
set file [lindex $argv 0]
?
set host [lindex $argv 1]
?
set dir [lindex $argv 2]
?
set password "123456"
?
spawn scp -P22 -rp $file root@$host:$dir
?
expect {
?
"yes/no" {send "yes\r"; exp_continue}
?
"*password*" {send "$password\r"}
?
}
?
expect eof
?
[root@localhost ~]# chmod +x auto_upload_file.exp
?
[root@localhost ~]# ./auto_upload_file.exp
?
usage: expect ./auto_upload_file.exp file host dir
?
[root@localhost ~]# ./auto_upload_file.exp cpuinfo.sh
192.168.200.112 /opt
?
spawn scp -P22 -rp cpuinfo.sh root@192.168.200.112:/opt
?
root@192.168.200.112's password:
?
cpuinfo.sh 100% 1470 393.6KB/s 00:00

2)通過shell腳本中的循環(huán)語句,調(diào)取expect單機自動免交互上傳文件腳本。從而實現(xiàn)將cpuinfo.sh腳本批量上傳到客戶機指定目錄下。

[root@localhost ~]# vim auto_upload_files.sh
?
#!/bin/bash
?
# Filename : auto_upload_files.sh
?
if [ $# -ne 2 ]; then
?
echo "useage : sh $0 file dir"
?
exit 1
?
fi
?
file=$1
?
dir=$2
?
for IP in 192.168.200.{112..113} ; do
?
expect auto_upload_file.exp $file $IP $dir
?
done
?
[root@localhost ~]# chmod +x auto_upload_files.sh
?
[root@localhost ~]# ./auto_upload_files.sh
?
useage : sh ./auto_upload_files.sh file dir
?
[root@localhost ~]# ./auto_upload_files.sh cpuinfo.sh /opt/
?
spawn scp -P22 -rp cpuinfo.sh root@192.168.200.112:/opt/
?
root@192.168.200.112's password:
?
cpuinfo.sh 100% 1470 783.5KB/s 00:00
?
spawn scp -P22 -rp cpuinfo.sh root@192.168.200.113:/opt/
?
root@192.168.200.113's password:
?
cpuinfo.sh 100% 1470 662.7KB/s 00:00

3)通過expect實現(xiàn)自動免交互方式在指定單臺客戶機上執(zhí)行指定命令或腳本程序。

[root@localhost ~]# vim auto_run_host.exp
?
#!/usr/bin/expect
?
# Filename: auto_run_1host.sh
?
if { $argc !=2 } {
?
puts "usage: expect $argv0 ip command"
?
exit
?
}
?
set ip [lindex $argv 0]
?
set cmd [lindex $argv 1]
?
set password "123456"
?
spawn ssh root@$ip $cmd
?
expect {
?
"yes/no" {send "yes\r"; exp_continue}
?
"*password*" {send "$password\r"}
?
}
?
expect eof
?
[root@localhost ~]# chmod +x auto_run_host.exp
?
[root@localhost ~]# ./auto_run_host.exp 192.168.200.112 "ifconfig
ens32"
?
spawn ssh root@192.168.200.112 ifconfig ens32
?
root@192.168.200.112's password:
?
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
?
inet 192.168.200.112 netmask 255.255.255.0 broadcast 192.168.200.255
?
inet6 fe80::20c:29ff:fe8c:f2d9 prefixlen 64 scopeid 0x20<link>
?
ether 00:0c:29:8c:f2:d9 txqueuelen 1000 (Ethernet)
?
RX packets 2650 bytes 1541675 (1.4 MiB)
?
RX errors 0 dropped 0 overruns 0 frame 0
?
TX packets 1874 bytes 236034 (230.5 KiB)
?
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
?
[root@localhost ~]# ./auto_run_host.exp 192.168.200.112 "source
/opt/cpuinfo.sh"
?
spawn ssh root@192.168.200.112 source /opt/cpuinfo.sh
?
root@192.168.200.112's password:
?
logical CPU number in total: 1
?
phycical CPU number in total: 1
?
core number in a physical CPU: 1
?
logical CPU number in a phycical CPU:1
?
Hyper threading is NOT enabled.

4)通過shell腳本中的循環(huán)語句,調(diào)取單機免交互自動執(zhí)行腳本程序的expect腳本。從而實現(xiàn)在多臺機器上免交互自動執(zhí)行shell腳本。

[root@localhost ~]# vim auto_run_hosts.sh
?
#!/bin/bash
?
# Filename: auto_run_hosts.sh
?
if [ $# -ne 1 ]; then
?
echo "useage: $0 cmd"
?
exit 3
?
fi
?
cmd=$1
?
for IP in 192.168.200.{112..113}; do
?
expect auto_run_host.exp $IP "$cmd"
?
done
?
[root@localhost ~]# chmod +x auto_run_hosts.sh
?
[root@localhost ~]# ./auto_run_hosts.sh
?
useage: ./auto_run_hosts.sh cmd
?
[root@localhost ~]# ./auto_run_hosts.sh "source
/opt/cpuinfo.sh"
?
spawn ssh root@192.168.200.112 source /opt/cpuinfo.sh
?
root@192.168.200.112's password:
?
logical CPU number in total: 1
?
phycical CPU number in total: 1
?
core number in a physical CPU: 1
?
logical CPU number in a phycical CPU:1
?
Hyper threading is NOT enabled.
?
spawn ssh root@192.168.200.113 source /opt/cpuinfo.sh
?
root@192.168.200.113's password:
?
logical CPU number in total: 1
?
phycical CPU number in total: 1
?
core number in a physical CPU: 1
?
logical CPU number in a phycical CPU:1
?
Hyper threading is NOT enabled.

到此這篇關(guān)于shell中expect的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)shell expect內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評論