Linxu下 expect的安装与使用

expect学习

    1.什么是except
        Expect是基于Tcl的一个相对简单的免费脚本文件语言工具,用于实现自动和交互式程序进行通信
            is a software suite for automating interactive tools
    2.Expect程序工作流程
        spawn启动进程 -> expect 期待关键字 ->send 向进程发送密码字符 ->退出结束
    3.Expect语法
        spawn:是Expect的初始命令,之后所有的Expect操作都在这个进程
            spawn  命令
            spawn  ssh-copy-id -i $file oldgirl@$host
        expect:匹配内容的输出,一旦匹配上就执行expect后面的命令,依附spawn执行
            特有参数: -re ,表示使用正则表达式的方式匹配
            expect 表达式 动作 表达式 动作 .....   
            expect "*password:"  {send "123456 "}   -->发那个在第二行不需要大括号了
                --> expect "*password:"
                    send "123456 "
        send:配合使用
        continue:多用于连续匹配多个命令的场景
            它完成的工作是从头开始遍历
                也就是说,
                    没有这个命令,匹配完成第一个关键字后就会继续匹配第二个关键字(或的关系)
                    有这个命令,匹配完成第一个关键字后第二次匹配任然从第一个关键字开始(与的关系)
        send_user:把后面的参数输出到标准输出(屏幕)中去,send/exp_send命令默认是输出到程序中
            send_user "please input the passwd: "
        exit:直接退出脚本,但是也可以做一些扫尾工作
            exit -onexit {
                exec rm $tmpfile
                send_user "good bye "
            }
        expect变量:
            set  变量名 变量值     -->设置
            puts $变量名           -->读取
                set host [lindex $argv 1]    -->接受命令行的参数,取第一个参数
        expect关键字:
            用于匹配过程,代表某个状态,一般用于命令族,而不能单独使用
            eof:用于文件的结束符,ftp的终止等,一般后面跟上动作做进一步控制
                eof {ftp connect close}
            timeout:全局性的时间控制开关
                set timeout 60  ==> expect {-timeout 60}
                    0:立即超时 
                   -1:永不超时
                expect timeout {puts "Expect was timeout"; return}

expect安装      

源码安装:
1.下载expect和tcl 下载地址: https://files.cnblogs.com/files/ftl1012/expect-5.43.0.tar%E5%92%8Ctcl8.4.11-src.tar.zip 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

  yum安装:

yum install expect

编写expect&&Shell脚本用于批量管理用户免密登录 

    1.exp脚本,用于ssh公钥传输 

    #! /usr/local/bin/expect
            if { $argc !=2 } {
               send_user "usage :expect dispatch_sshkey.expect file host 
"
                   exit
               }
        #define var
        set file [lindex $argv 0]
        set host [lindex $argv 1]
        set password "hhh"
        #spawn scp /home/omd/2017-08-23 root@192.168.25.137:/home/omd
        #spawn scp -P11544 $file root@$host:$dir
        #spawn ssh-copy-id -i $file "-p 11544 root@$host:$dir"
        #spawn ssh-copy-id -i  .ssh/id_dsa.pub omd@192.168.25.137
        spawn ssh-copy-id -i $file oldgirl@$host
        expect { 
           -timeout 30    
           "yes/no"    {send "yes
";exp_continue}
           "*password" {send "$password
"}
           timeout {puts "expect was timeout by omd"; return}
        }
        expect eof

    2.sh脚本, 用于批量管理

 #! /bin/sh
        . /etc/init.d/functions
        for ip in `cat /etc/hosts|grep "192.168.25.13[5 6 7]" |awk '{print $1}' `
        do  
           /usr/local/bin/expect /data/scripts/dispatch_sshkey.exp  ~/.ssh/id_dsa.pub $ip
           if [[ $? -eq 0 ]]; then
              action "$ip" /bin/true
           else  
              action "$ip" /bin/false 
           fi     
        done 

  其他参考

Linxu下 expect的实用实例_1

Linxu下 expect的实用实例(jump)_2

原文地址:https://www.cnblogs.com/ftl1012/p/expect.html