expect - 自动交互脚本

<!DOCTYPE html>
<html>
 <head> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  <link rel="Stylesheet" type="text/css" href="../styles/style.css" /> 
  <title>expect - 自动交互脚本</title> 
 </head> 
 <body> 
  <div id="header"> 
   <ul id="top-nav"> 
    <li><a href="../index.html">首页</a></li> 
    <li><a href="index.html">分类首页</a></li> 
   </ul> 
  </div> 
  <div id="cse"></div> 
  <div id="main"> 
   <h1>expect - 自动交互脚本</h1> 
   <div class="toc"> 
    <ul> 
     <li><a href="#toc_0.1">expect参数</a> 
      <ul> 
       <li><a href="#toc_0.1.1">启用选项</a> </li>
       <li><a href="#toc_0.1.2">常用命令</a> </li>
       <li><a href="#toc_0.1.3">命令介绍</a> </li>
      </ul> </li>
     <li><a href="#toc_0.2">expect范例</a> </li>
    </ul>  
   </div> 
   <h2 id="toc_0.1">expect参数</h2> 
   <ul> 
    <li> <a href="expect_handbook.html">expect教程中文版</a> </li>
    <li> <a href="http://blog.csdn.net/classwang/article/details/3928467">expect中文手册</a> </li>
    <li> <a href="expect_description.html">expect说明</a> </li>
   </ul> 
   <h3 id="toc_0.1.1">启用选项</h3> 
   <ul> 
    <li> <code>-c</code>:执行脚本前先执行的命令,可多次使用。 </li>
    <li> <code>-d</code>:debug模式,可以在运行时输出一些诊断信息,与在脚本开始处使用<code>exp_internal 1</code>相似。 </li>
    <li> <code>-D</code>:启用交换调式器,可设一整数参数。 </li>
    <li> <code>-f</code>:从文件读取命令,仅用于使用#!时。如果文件名为&quot;-&quot;,则从stdin读取(使用&quot;./-&quot;从文件名为-的文件读取)。 </li>
    <li> <code>-i</code>:交互式输入命令,使用&quot;exit&quot;或&quot;EOF&quot;退出输入状态。 </li>
    <li> <code>--</code>:标示选项结束(如果你需要传递与expect选项相似的参数给脚本时),可放到<code>#!</code>行:<code>#!/usr/bin/expect --</code>。 </li>
    <li> <code>-v</code>:显示expect版本信息。 </li>
   </ul> 
   <h3 id="toc_0.1.2">常用命令</h3> 
   <pre class="brush:bash">
# 命令行参数 
# $argv,参数数组,使用[lindex $argv n]获取,$argv 0为脚本名字
# $argc,参数个数
set username [lindex $argv 1]  # 获取第1个参数
set passwd [lindex $argv 2]    # 获取第2个参数

set timeout 30 # 设置超时

# spawn是expect内部命令,开启ssh连接
spawn ssh -l username 192.168.1.1

# 判断上次输出结果里是否包含“password:”的字符串,如果有则立即返回,否则就等待一段时间(timeout)后返回
expect &quot;password:&quot;

# 发送内容ispass(密码、命令等)
send &quot;ispass\r&quot;

# 发送内容给用户
send_user &quot;$argv0 [lrange $argv 0 2]\n&quot;
send_user &quot;It's OK\r&quot;
# 执行完成后保持交互状态,控制权交给控制台(手工操作)。否则会完成后会退出。
interact
</pre> 
   <h3 id="toc_0.1.3">命令介绍</h3> 
   <ul> 
    <li> close:关闭当前进程的连接。 </li>
    <li> debug:控制调试器。 </li>
    <li> disconnect:断开进程连接(进程仍在后台运行)。 
     <ul> 
      <li> 定时读取密码、执行priv_prog <pre class="brush:bash">
send_user &quot;password?\ &quot;
expect_user -re &quot;(.*)\n&quot;
for {} 1 {} {
    if {[fork]!=0} {sleep 3600;continue}
    disconnect
    spawn priv_prog
    expect Password:
    send &quot;$expect_out(1,string)\r&quot;
    . . .
    exit
}
</pre> </li>
     </ul> </li>
    <li> exit:退出expect。 </li>
    <li> exp_continue [-continue_timer]:继续执行下面的匹配。 </li>
    <li> exp_internal [-f file] value: </li>
   </ul> 
   <h2 id="toc_0.2">expect范例</h2> 
   <ul> 
    <li> 自动telnet会话 <pre class="brush:bash">
#!/usr/bin/expect -f
set ip [lindex $argv 0 ]         # 接收第1个参数,作为IP
set userid [lindex $argv 1 ]     # 接收第2个参数,作为userid
set mypassword [lindex $argv 2 ] # 接收第3个参数,作为密码
set mycommand [lindex $argv 3 ]  # 接收第4个参数,作为命令
set timeout 10                   # 设置超时时间

# 向远程服务器请求打开一个telnet会话,并等待服务器询问用户名
spawn telnet $ip
    expect &quot;username:&quot;
    # 输入用户名,并等待服务器询问密码
    send &quot;$userid\r&quot;
    expect &quot;password:&quot;
    # 输入密码,并等待键入需要运行的命令
    send &quot;$mypassword\r&quot;
    expect &quot;%&quot;
    # 输入预先定好的密码,等待运行结果
    send &quot;$mycommand\r&quot;
    expect &quot;%&quot;
    # 将运行结果存入到变量中,显示出来或者写到磁盘中
    set results $expect_out(buffer)
    # 退出telnet会话,等待服务器的退出提示EOF
    send &quot;exit\r&quot;
    expect eof
</pre> </li>
    <li> 自动建立FTP会话 <pre class="brush:bash">
#!/usr/bin/expect -f
set ip [lindex $argv 0 ]         # 接收第1个参数,作为IP
set userid [lindex $argv 1 ]     # 接收第2个参数,作为Userid
set mypassword [lindex $argv 2 ] # 接收第3个参数,作为密码
set timeout 10                   # 设置超时时间

# 向远程服务器请求打开一个FTP会话,并等待服务器询问用户名
spawn ftp $ip
    expect &quot;username:&quot;
    # 输入用户名,并等待服务器询问密码
    send &quot;$userid\r&quot;
    expect &quot;password:&quot;
    # 输入密码,并等待FTP提示符的出现
    send &quot;$mypassword\r&quot;
    expect &quot;ftp&gt;&quot;
    # 切换到二进制模式,并等待FTP提示符的出现
    send &quot;bin\r&quot;
    expect &quot;ftp&gt;&quot;
    # 关闭ftp的提示符
    send &quot;prompt\r&quot;
    expect &quot;ftp&gt;&quot;
    # 下载所有文件
    send &quot;mget *\r&quot;
    expect &quot;ftp&gt;&quot;
    # 退出此次ftp会话,并等待服务器的退出提示EOF
    send &quot;bye\r&quot;
    expect eof
</pre> </li>
   </ul> 
   <ul> 
    <li> 自动登录ssh执行命令 <pre class="brush:bash">
#!/usr/bin/expect
set IP     [lindex $argv 0]
set USER   [lindex $argv 1]
set PASSWD [lindex $argv 2]
set CMD    [lindex $argv 3]

spawn ssh $USER@$IP $CMD
expect {
    &quot;(yes/no)?&quot; {
        send &quot;yes\r&quot;
        expect &quot;password:&quot;
        send &quot;$PASSWD\r&quot;
        }
    &quot;password:&quot; {send &quot;$PASSWD\r&quot;}
    &quot;* to host&quot; {exit 1}
    }
expect eof
</pre> </li>
    <li> 自动登录ssh <pre class="brush:bash">
#!/usr/bin/expect -f  
set ip [lindex $argv 0 ]         # 接收第1个参数,作为IP
set username [lindex $argv 1 ]   # 接收第2个参数,作为username
set mypassword [lindex $argv 2 ] # 接收第3个参数,作为密码
set timeout 10                   # 设置超时时间 

spawn ssh $username@$ip       # 发送ssh请求
expect {                      # 返回信息匹配 
&quot;*yes/no&quot; { send &quot;yes\r&quot;; exp_continue}  # 第一次ssh连接会提示yes/no,继续  
&quot;*password:&quot; { send &quot;$mypassword\r&quot; }    # 出现密码提示,发送密码  
} 
interact        # 交互模式,用户会停留在远程服务器上面
</pre> </li>
   </ul> 
   <ul> 
    <li> 批量登录ssh服务器执行操作范例,设定增量的for循环 <pre class="brush:bash">
#!/usr/bin/expect
for {set i 10} {$i &lt;= 12} {incr i} {
    set timeout 30
    set ssh_user [lindex $argv 0]
    spawn ssh -i .ssh/$ssh_user abc$i.com

    expect_before &quot;no)?&quot; {
    send &quot;yes\r&quot; }
    sleep 1
    expect &quot;password*&quot;
    send &quot;hello\r&quot;
    expect &quot;*#&quot;
    send &quot;echo hello expect! &gt; /tmp/expect.txt\r&quot;
    expect &quot;*#&quot;
    send &quot;echo\r&quot;
}
exit
</pre> </li>
   </ul> 
   <ul> 
    <li> 批量登录ssh并执行命令,foreach语法 <pre class="brush:bash">
#!/usr/bin/expect
if {$argc!=2} {
    send_user &quot;usage: ./expect ssh_user password\n&quot;
    exit
}
foreach i {11 12} {
    set timeout 30
    set ssh_user [lindex $argv 0]
    set password [lindex $argv 1]
    spawn ssh -i .ssh/$ssh_user root@xxx.yy.com
    expect_before &quot;no)?&quot; {
    send &quot;yes\r&quot; }
    sleep 1

    expect &quot;Enter passphrase for key*&quot;
    send &quot;password\r&quot;
    expect &quot;*#&quot;
    send &quot;echo hello expect! &gt; /tmp/expect.txt\r&quot;
    expect &quot;*#&quot;
    send &quot;echo\r&quot;
}
exit
</pre> </li>
   </ul> 
   <ul> 
    <li> 另一自动ssh范例,从命令行获取服务器IP,foreach语法,expect嵌套 <pre class="brush:bash">
#!/usr/bin/expect
# 使用方法: script_name ip1 ip2 ip3 ...

set timeout 20
if {$argc &lt; 1} {
  puts &quot;Usage: script IPs&quot;
  exit 1
}
# 替换你自己的用户名
set user &quot;username&quot;
#替换你自己的登录密码
set password &quot;yourpassword&quot;

foreach IP $argv {
spawn ssh $user@$IP

expect \
  &quot;(yes/no)?&quot; {
    send &quot;yes\r&quot;
    expect &quot;password:?&quot; {
      send &quot;$password\r&quot;
    }
  } &quot;password:?&quot; {
    send &quot;$password\r&quot;
}

expect &quot;\$?&quot;
# 替换你要执行的命令
send &quot;last\r&quot;
expect &quot;\$?&quot;
sleep 10
send &quot;exit\r&quot;
expect eof
}
</pre> </li>
   </ul> 
   <ul> 
    <li> 批量ssh执行命令,用shell调用tclsh方式、多进程同时执行 
     <ul> 
      <li> tclsh - Simple shell containing Tcl interpreter <pre class="brush:bash">
#!/bin/sh
# -*- tcl -*- \
exec tclsh $0 &quot;$@&quot;
package require Expect
set username [lindex $argv 0]
set password [lindex $argv 1]
set argv [lrange $argv 2 end]
set prompt &quot;(%|#|\\$) $&quot;
foreach ip $argv {
    spawn ssh -t $username@$ip sh
    lappend ids $spawn_id
}
expect_before -i ids eof {
    set index [lsearch $ids $expect_out(spawn_id)]
    set ids [lreplace $ids $index $index]
    if [llength $ids] exp_continue
}
expect -i ids &quot;(yes/no)\\?&quot; {
    send -i $expect_out(spawn_id) yes\r
    exp_continue
} -i ids &quot;Enter passphrase for key&quot; {
    send -i $expect_out(spawn_id) \r
    exp_continue
} -i ids &quot;assword:&quot; {
    send -i $expect_out(spawn_id) $password\r
    exp_continue
} -i ids -re $prompt {
    set spawn_id $expect_out(spawn_id)
    send &quot;echo hello; exit\r&quot;
    exp_continue
} timeout {
    exit 1
}
</pre> </li>
     </ul> </li>
   </ul> 
   <ul> 
    <li> ssh登录过程常规提示文字 <pre>
The authenticity of host '192.168.17.35 (192.168.17.35)' can't be established.
RSA key fingerprint is 25:e8:4c:89:a3:b2:06:ee:de:66:c7:7e:1b:fa:1c:c5.
Are you sure you want to continue connecting (yes/no)?


Warning: Permanently added '192.168.17.35' (RSA) to the list of known hosts.
Enter passphrase for key '/data/key/my_dsa':


Last login: Sun Jan 26 13:39:37 2014 from 192.168.11.143
[root@master003 ~]#


root@192.168.16.90's password:


Last login: Thu Jan 23 17:50:43 2014 from 192.168.11.102
[root@lvsmaster ~]#
</pre> </li>
   </ul> 
   <ul> 
    <li> ssh自动登录expect脚本:ssh.expect <pre class="brush:bash">
#!/usr/bin/expect -f
# Auther:YuanXing
# Update:2014-02-08
if {$argc &lt; 4} {
    send_user &quot;Usage:\n  $argv0 IPaddr User Passwd Port Passphrase\n&quot;
    puts stderr &quot;argv error!\n&quot;
    sleep 1
    exit 1
}

set ip         [lindex $argv 0 ]
set user       [lindex $argv 1 ]
set passwd     [lindex $argv 2 ]
set port       [lindex $argv 3 ]
set passphrase [lindex $argv 4 ]
set timeout 6
if {$port == &quot;&quot;} {
    set port 22
}
#send_user &quot;IP:$ip,User:$user,Passwd:$passwd,Port:$port,Passphrase:$passphrase&quot;
spawn ssh -p $port $user@$ip

expect_before &quot;(yes/no)\\?&quot; {
    send &quot;yes\r&quot;}

expect \
&quot;Enter passphrase for key*&quot; {
    send &quot;$passphrase\r&quot;
    exp_continue
} &quot; password:?&quot; {
    send &quot;$passwd\r&quot;
    exp_continue
} &quot;*\[#\\\$]&quot; {
    interact
} &quot;* to host&quot; {
    send_user &quot;Connect faild!&quot;
    exit 2
} timeout {
    send_user &quot;Connect timeout!&quot;
    exit 2
} eof {
    send_user &quot;Lost connect!&quot;
    exit
}
</pre> </li>
    <li> Mikrotik backup script using ssh and expect 
     <ul> 
      <li> <a href="http://www.pmoghadam.com/homepage/HTML/mikrotik-backup-script-ssh-expect.html">http://www.pmoghadam.com/homepage/HTML/mikrotik-backup-script-ssh-expect.html</a> <pre class="brush:bash">
#!/bin/bash
# BY: Pejman Moghadam
# TAG: mikrotik, ssh, expect, lftp
# DATE: 2012-05-27 14:42:14 

BACKUP_DIR=&quot;/var/backups&quot;
HOSTNAME=&quot;192.168.88.1&quot;
PORT=&quot;22&quot;
USER=&quot;admin&quot;
PASS=&quot;123456&quot;
TMP=$(mktemp)
TODAY=$(date +%F)
FILENAME=&quot;$HOSTNAME-$TODAY&quot;
PATH=&quot;/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin&quot;

# create expect script
cat &gt; $TMP &lt;&lt; EOF 
#exp_internal 1 # Uncomment for debug
set timeout -1
spawn ssh -p$PORT $USER@$HOSTNAME
match_max 100000
expect -exact &quot;password:&quot;
send -- &quot;$PASS\r&quot;
sleep 1
expect &quot; &gt; &quot;
send -- &quot;/export file=$FILENAME\r&quot;
expect &quot; &gt; &quot;
send -- &quot;/system backup save name=$FILENAME\r&quot;
expect &quot; &gt; &quot;
send -- &quot;quit\r&quot;
expect eof
EOF

# run expect script
#cat $TMP # Uncomment for debug
expect -f $TMP

# remove expect script
rm $TMP

# download and remove backup files
# &quot;xfer:clobber on&quot; means overwrite existing files
cd ${BACKUP_DIR}
echo &quot;
  set xfer:clobber on
  get ${FILENAME}.rsc
  rm ${FILENAME}.rsc 
  get ${FILENAME}.backup
  rm ${FILENAME}.backup&quot; | 
lftp -u $USER,$PASS $HOSTNAME
</pre> </li>
     </ul> </li>
   </ul> 
  </div> 
  <div id="footer"> 
   <p> &copy; 2012 - 2015 XStar &nbsp;|&nbsp;<a href="http://code.google.com/p/vimwiki/" title="vimwiki">Powerby:Vimwiki</a> &nbsp;|&nbsp;<a href="http://kwiki.github.io" title="丘迟">Style:丘迟</a> &nbsp;|&nbsp;<a href="../index.html">首页</a> &nbsp;|&nbsp;<a href="index.html">分类首页</a> &nbsp;|&nbsp;<a href="../SiteMap.html">站点地图</a> </p> 
  </div> 
  <script type="text/javascript">var vimwiki_rootpath="../";</script> 
  <script type="text/javascript" src="http://cdn.staticfile.org/jquery/2.0.0/jquery.min.js"></script> 
  <script type="text/javascript" src="../scripts/vimwiki.js"></script>   
 </body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值