linux 自動互動工具expect

2022-06-10 21:27:11 字數 1871 閱讀 1251

**:

簡介:

expect是乙個自動化互動套件,主要應用於執行命令和程式時,系統以互動形式要求輸入指定字串,實現互動通訊。expect自動互動流程:spawn啟動指定程序---expect獲取指定關鍵字---send向指定程式傳送指定字元---執行完成退出.注意該指令碼能夠執行的前提是安裝了expect

yum -y install expect
spawn               互動程式開始後面跟命令或者指定程式

expect 獲取匹配資訊匹配成功則執行expect後面的程式動作

send exp_send 用於傳送指定的字串資訊

exp_continue 在expect中多次匹配就需要用到

send_user 用來列印輸出 相當於shell中的echo

exit 退出expect指令碼

eof expect執行結束 退出

set 定義變數

puts 輸出變數

set timeout 設定超時時間

使用expect登入到遠端主機上檢視ip位址

#!/bin/bash

user="root"

ip="192.168.137.121"

passwd="ilovewy"

/usr/bin/expect <<-eof

spawn ssh $user@$ip ip addr

set time 30

expect

"*password:"

}expect eof

eof

#!/bin/bash

#ssh 免密分發指令碼

read -p "input user " user

read -p "input host_ip " host

read -p "input host_user_passwd " passwd

if [ -e "/root/.ssh/id_rsa" ] ;then

echo "file "

else

echo "no file"

/usr/bin/expect <<-eof

spawn ssh-keygen -t rsa

set time 30

expect

"*(empty for no passphrase):"

"*same passphrase again:"

}expect eof

eofficp_rsa()

"*password:"

}expect eof

eof }

mian ()

main

密碼過期需要批量修改密碼

#!/bin/bash

for i in `cat /root/soft/ip.txt`

do /usr/bin/expect << eof

spawn /usr/bin/ssh root@$i

expect

}expect

}expect

}expect "*]#"

send "echo huawei@123|passwd --stdin root\r"

expect "*]#"

send "exit\r"

expect eof

eofdone

linux自動化互動工具expect簡單配置使用

1 掛載 mount t iso9660 o loop dev cdrom mnt cd 2 查詢expect安裝包 yum list grep expect 3 安裝 yum install expect.i686 y 4 新建並編輯expect配置檔案vi expectdemo.sh 配置內容如...

Linux互動工具 here document

here document 乙個有特殊目的的 段,它使用i o重定向將一段 能傳遞引數和命令替換 傳遞到別的互動程式或命令中,通常用於互動式。usage command endofmessage 可以抑制文件前tab的輸出,endofmessage 單引號可以阻止文件內變數的擴充套件 conmman...

自動化互動工具 expect

lenovo v110 15ikb notebook scp 192.168.70.187 123 192.168.70.187 s password sudo apt install expect 安裝會在 usr bin 下生成expect工具。spawn 啟動新的程序 send 用於向程序傳送...