shell中使用expect進行互動

2021-10-23 09:28:52 字數 958 閱讀 2703

expect常用語shell指令碼中的互動指令

#!

/usr/bin/expect

# 設定變數

set ip 192.168

.1.100

set user ljw

set password 123456

set timeout 5

#開啟乙個會話

spawn ssh $user@$ip

#通過expect對出現的情況進行輸入值

expect

"password:";}

# interact進入互動模式 不加的話會直接退出

#執行一些命令,$是我終端顯示$,根據情況修改

expect "$"

send "ls\r"

send "exit\r"

# 結束expect

expect eof

...

set ip [lindex $ar** 0

] #第乙個引數

set user [lindex $ar** 1

] #第二個引數..

.

#!

/usr/bin/bash

>ip.txt

password=

123456

user=ljw

#判斷公鑰是否存在,不存在建立if[

!-f ~

/.ssh/id_rsa ]

;then

ssh-keygen -p ""

-f ~

/.ssh/id_rsa

fifor i in

do"password:"

} expect eof

eoffi }

& #後台執行

done

wait

shell與expect結合使用

摘自 在linux作業系統下,使用指令碼自動化,一般由兩種方案,方案一 telnet ftp,方案二 ssh scp expect。以下主要使用ssh scp expect為例進行說明使用方式。第一步 安裝expect yum y install expect 第二步 驗證,執行expect是否正確...

shell與expect結合使用

在linux作業系統下,使用指令碼自動化,一般由兩種方案,方案一 telnet ftp,方案二 ssh scp expect。以下主要使用ssh scp expect為例進行說明使用方式。第一步 安裝expect yum y install expect 第二步 驗證,執行expect是否正確 第三...

shell使用expect實現sftp的自動互動

檢查是否安裝expect 直接在linux上執行 expect 如提示無此命令則未安裝 安裝expect 切換到root使用者下,執行 yum install expect expect簡單使用 1 在需要執行的指令碼中調取expect指令碼 vim exec expect sftp.sh usr ...