1. 程式人生 > >linux 腳本之 expect命令使用

linux 腳本之 expect命令使用

expect 靜默操作

1 概述

expect 是由Don Libes基於Tcl(Tool Command Language )語言開發的,主要應用於自動化交互式操作的場景,借助Expect處理交互的命令,可以將交互過程如:ssh登錄,ftp登錄等寫在一個腳本上,使之自動化完成。尤其適用於需要對多臺服務器執行相同操作的環境中,可以大大提高系統管理人員的工作效率

2 語法

expect命令

expect 語法:

expect [選項] [ -c cmds] [ [ -[f|b] ] cmdfile] [ args]

選項

-c:從命令行執行expect腳本,默認expect是交互地執行的

示例:expect -c ‘expect "\n" {send "pressed enter\n"}

-d:可以輸出輸出調試信息

示例:expect -d ssh.exp

expect中相關命令

spawn:啟動新的進程

send:用於向進程發送字符串

expect:從進程接收字符串

interact:允許用戶交互

exp_continue匹配多個字符串在執行動作後加此命令

expect最常用的語法(tcl語言:模式-動作)

單一分支模式語法:

expect “hi” {send “You said hi\n"}

匹配到hi後,會輸出“you said hi”,並換行

多分支模式語法:

expect "hi" { send "You said hi\n" } \

"hehe" { send “Hehe yourself\n" } \

"bye" { send “Good bye\n" }

匹配hi,hello,bye任意字符串時,執行相應輸出。等同如下:

expect {

"hi" { send "You said hi\n"}

"hehe" { send "Hehe yourself\n"}

"bye" { send “Good bye\n"}

}

3 腳本

以下是在expect在shell腳本中調用常用的幾個用法,自動拷貝腳本,下發腳本,ssh 和telnet 連接遠程主機.

#!/bin/bash
#
#******************************************************************************
#Author:               Sunny
#Date:                 2017-09-03
#FileName:             scp.sh
#version:              1.0
#Your change info:     
#Description:          For
#Copyright(C):         2017  All rihts reserved
#*****************************************************************************

echo "1 copy wang home dir,usage:$0"
echo "2 copy file under wang home,usage: $0 file_to_copy"
echo "3 send file to wang home,usage: $0 file_to_send"
echo "4 login other host by ssh,usage: $0 login_ip"
echo "5 login other host by telnet,usage: $0 login_ip login_user"
read -p "input the remote ip: " ip
read -p "input full path file_name or dir: " file
read -p "input host password: " passwd
read -p "Please input your choice: " choice
case $choice in
1)
    expect -c "
    spawn scp -r [email protected]$ip:$file "$file\_$(date +%F-%H-%M)"
    expect {
          # \"*assword\" {set timeout 300; send \"$passwd\r\"; }
           \"*assword\" {set timeout 300; send \"$passwd\r\"; }
           \"yes/no\" { send \"yes\r\"; exp_continue; }
    }
    expect eof"
    ;;  
2)
    expect -c "
   spawn scp  [email protected]$ip:$file  /root/
    expect {
          # \"*assword\" {set timeout 500; send \"$passwd\r\"; }
           \"*assword\" {set timeout 500; send \"$passwd\r\"; }
           \"yes/no\" { send \"yes\r\"; exp_continue; }
    }
    expect eof"
    ;;  
3)
    expect -c "
   spawn  scp  $file  [email protected]$ip:/root
   expect {
          # \"*assword\" {set timeout 500; send \"$passwd\r\"; }
           \"*assword\" {set timeout 500; send \"$passwd\r\"; }
           \"yes/no\" { send \"yes\r\"; exp_continue; }
    }
        expect eof"
    ;;
4)
   expect -c "
    spawn  ssh $ip
    expect {
           \"yes/no\" { send \"yes\r\"; exp_continue; }
    \"*assword\" {set timeout 500; send \"$passwd\r\"; }
    }   
    interact
     expect eof"
     ;;
5)

      expect -c "
    spawn  telnet $ip
    expect {
    \"*assword\" {set timeout 500; send \"$passwd\r\"; }
    \"login\" { send \"sunny\r\";exp_continue; }
    }  
    interact
     expect eof"
     ;;
*)
    echo "Your input is wrong,please check"
    exit
    ;;
esac

4 總結

expect可以實際靜默的操作,這個在腳本中經常用到,讀者根據對應的功能,拷貝相關代碼,可以直接把變量換成固定的值,不需要每次運行腳本都需要人工輸入變量值,實現靜默操作。

本文出自 “自學linux” 博客,請務必保留此出處http://ghbsunny.blog.51cto.com/7759574/1963007

linux 腳本之 expect命令使用