b.执行./configure --prefix=/expect 
        c.执行make 
        d.执行make install 
        e.执行mkdir -p /tools/lib 
        f.执行cp tclConfig.sh /tools/lib/ 
        g. 将/tools/bin目录export到环境变量 
           tclpath=/tools/bin 
           export tclpath 

第5步:安装Expect 
        进入/soft/expect-5.43目录 
        执行./configure --prefix=/tools --with-tcl=/tools/lib --with-x=no 
        如果最后一行提示: 
        configure: error: Can't find Tcl private headers 
        需要添加一个头文件目录参数 
        --with-tclinclude=../tcl8.4.11/generic,即 
        ./configure --prefix=/tools --with-tcl=/tools/lib --with-x=no --with-tclinclude=../tcl8.4.11/generic 
        ../tcl8.4.11/generic 就是tcl解压安装后的路径,一定确保该路径存在 
        执行make 
        执行make install 
        编译完成后会生在/tools/bin内生成expect命令 
        执行/tools/bin/expect出现expect1.1>提示符说明expect安装成功. 

第6步:创建一个符号链接 
        ln -s /tools/bin/expect /usr/bin/expect 
        查看符号连接 
        ls -l /usr/bin/expect 
        lrwxrwxrwx 1 root root 17 06-09 11:38 /usr/bin/expect -> /tools/bin/expect 

        这个符号链接将在编写expect脚本文件时用到,例如在expect文件头部会指定用于执行该脚本的shell 
        #!/usr/bin/expect 
        ... 
        ... 

----------------------

1.下载expect和tcl

下载地址:http://download.csdn.net/download/tobyaries/5754943

2.安装expect

tar -zxvf tcl8.4.11-src.tar.gz 
tar -zxvf expect-5.43.0.tar.gz 
cd tcl8.4.11/unix
 ./configure
make && make install

cd expect-5.43

./configure --with-tcl=/usr/local/lib/ --with-tclinclude=/data/software/tcl8.4.11

make && make install

3.脚本测试

 

[plain] view plain copy
 
 print?Expect安装方法Expect安装方法
  1. #!/usr/bin/expect -f  
  2.   
  3.   
  4. set timeout 30  
  5. set name [lindex $argv 0]  
  6. set pw [lindex $argv 1]  
  7. log_user 0  
  8. spawn passwd $name  
  9. for {set i 1} {$i < 3} {incr i} {  
  10.     expect "*password:" {send "$pw\r"}  
  11. }  
  12. expect "*successfully"  
  13.     send_user "Password updated successfully\n"  
  14. expect eof  



 

 

[解析]

  很简单的程序,首先把第一个参数赋值给变量name作为用户名,把第二个参数赋值给pw作为密码。然后关闭标准输出,就是类似shell的 "> /dev/null",然后spawn开启子进程运行passwd程序,因为会重复输入2次密码,所以这里我们用到了TCL语法的for循环,执行2次密码输入。最后匹配到successfully字样的就输出修改成功,然后退出脚本。

 

相关文章: