美文网首页shell
shell编程之Expect

shell编程之Expect

作者: 肉包君 | 来源:发表于2021-06-25 11:15 被阅读0次

    2021-01-13
    expect的安装

    [root@host ~]# yum -y install expect
    

    expect应答函数的语法
    是一个免费的编程工具, 用来实现自动的交互式任务, 而无需人为干预. 说白了expect就是一套用来实现自动交互功能的软件

    在实际工作中我们运行命令、脚本或程序时, 这些命令、脚本或程序都需要从终端输入某些继续运行的指令,而这些输入都需要人为的手工进行. 而利用expect则可以根据程序的提示, 模拟标准输入提供给程序, 从而实现自动化交互执行. 这就是expect

    能够在工作中熟练的使用Shell脚本就可以很大程度的提高工作效率, 如果再搭配上expect,那么很多工作都可以自动化进行,对工作的展开如虎添翼

    用法:
    1)定义脚本执行的shell
        #!/usr/bin/expect 类似于#!/bin/bash
    
    2)set timeout 30
        设置超时时间30s
    
    3)spawn
        spawn是进入expect环境后才能执行的内部命令,不能直接在默认的shell环境中执行
        功能:传递交互命令
    
    4)expect
        这里的expect同样是expect命令
        功能:判断输出结果是否包含某项字符串,没有则立即返回,否则等待一段时间后返回,等待通过timeout设置
    
    5)send
        执行交互动作,将交互要执行的动作进行输入给交互指令
        命令字符串结尾要加上“r”,如果出现异常等待状态可以进行核查
    
    6)interract
        执行完后保持交互状态,把控制权交给控制台
        如果不加这一项,交互完成会自动退出
    
    7)exp_continue
        继续执行接下来的操作
    
    expect环境中设置变量用set,识别不了bash方式定义的变量
    错误方式:
    [root@host expect]# cat expect02.sh 
    #!/usr/bin/expect
    user=22
    spawn echo $user
    [root@host expect]# ./expect02.sh 
    invalid command name "user=22"
        while executing
    "user=22"
        (file "./expect02.sh" line 3)
        
    正确方式:    
    [root@host expect]# cat ./expect02.sh
    #!/usr/bin/expect
    set user 22
    spawn echo $user
    [root@host expect]# ./expect02.sh 
    spawn echo 22
    
    ============================================================================
    
    
    [root@host expect]# cat expect01.sh 
    #!/usr/bin/expect
    spawn ssh root@192.168.62.146
    expect {
            "yes/no" { send "yes\r";exp_continue }
            "password:" { send "123\r" }
    }
    interact
    
    interact的作用测试
    执行测试是否免交互:
    要是用/usr/bin/expect的shell执行
    [root@host expect]#/usr/bin/expect expect01.sh
    成功
    
    擦除最后一行interact进行测试
    [root@host expect]#/usr/bin/expect expect01.sh
    进入之后立即退出
    ============================================================================
    
    \r的作用测试
    [root@host expect]# cat expect01.sh 
    #!/usr/bin/expect
    set user root
    set pass 123
    set ip 192.168.62.146
    spawn ssh $user@$ip
    expect {
            "yes/no" { send "ye";exp_continue }
            "password:" { send "pas" }
    }
    interact
    
    [root@host expect]# ./expect01.sh 
    spawn ssh root@192.168.62.146
    root@192.168.62.146's password: 
    
    加上\r之后
    [root@host expect]# ./expect01.sh 
    spawn ssh root@192.168.62.146
    root@192.168.62.146's password: 
    Permission denied, please try again.
    root@192.168.62.146's password: 
    ============================================================================
    
    设置变量的方式
    [root@host expect]# cat expect01.sh 
    #!/usr/bin/expect
    set user root
    set pass 123
    set ip 192.168.62.146
    spawn ssh $user@$ip
    expect {
            "yes/no" { send "yes\r";exp_continue }
            "password:" { send "$pass\r" }
    }
    interact
    

    实现ssh自动登录----expect的常用实例

    #!/usr/bin/env expect
    # 
    # Author: 
    
    set password "123"
    set username "root"
    
    spawn ssh $username@192.168.161.130
    expect "*yes/no*" {send "yes\r"}
    expect "*password*" {send "$password"}
    

    实现自动生成密钥

    [root@host ~]# ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa):
    Created directory '/root/.ssh'.
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:C+buJw3dkFuyWCTXWGr9oSHjoaLeZzMfonA0Vj/BbW0 root@nginxserver02
    The key's randomart image is:
    +---[RSA 2048]----+
    |         +.      |
    |      ..++..     |
    |      .+O.= E    |
    |     . ==*.= .   |
    |    = ++SB. .    |
    |   + =o.+o.      |
    |  o . ooo        |
    | . + o*.o.       |
    |  . o+o*.        |
    +----[SHA256]-----+
    
    [root@host ~]# vim no_enter_sshkey
    #!/usr/bin/env expect
    # 
    # Author: 
    
    spawn ssh-keygen
    expect "*):*" {send "\r"}
    expect "*passphrase*" {send "\r"}
    expect "*again*" {send "\r"}
    expect eof
    

    将expect函数嵌入shell脚本

    /usr/bin/expect <<-EOF
    set password "123"
    set username "root"
    spawn ssh $username@192.168.161.130
    expect "*yes/no*" {send "yes\r"}
    expect "*password*" {send "$password"}
    EOF
    
    设置位置参数的方式(拓展)
    [root@host expect]# cat expect01.sh 
    #!/usr/bin/expect
    set timeout 30
    set user [ lindex $argv 0 ]
    set pass [ lindex $argv 1 ]
    set ip [ lindex $argv 2 ]
    spawn ssh $user@$ip
    expect {
            "yes/no" { send "yes";exp_continue }
            "password:" { send "$pass\r" }
    }
    interact
    运行结果为:
    [root@host expect]# ./expect01.sh root 123 192.168.62.146
    spawn ssh root@192.168.62.146
    root@192.168.62.146's password: 
    Last login: Thu Aug 15 23:26:59 2019 from 192.168.62.136
    

    相关文章

      网友评论

        本文标题:shell编程之Expect

        本文链接:https://www.haomeiwen.com/subject/amtpaktx.html