shell中expect介绍

shell中expect介绍

expect介绍

      借助Expect处理交互的命令,可以将交互 过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成.尤其适用于需 要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员的工作效率 

expect安装 

   [root@ansible ssh]# rpm -qa | grep expect
       expect-5.45-14.el7_1.x86_64
   [root@ansible ssh]# yum install 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"} } 

 

 自动拷贝文件到远程主机

    执行expect 不能以bash file 的方式来执行        (开启一个子shell进程)

    必须通过  chmod +x file     ./file  这样的方式    (不会开启子shell进程,只在当前shell环境中执行)

    expect 如果只交互一次如拷贝文件   结尾就使用                                            expect eof 

              如果需要连续交互如登录远程主机执行各种命令结尾就需使用           interact

 

 

复制代码

1.安装expect  系统默认没有此命令
   yum install expect

2.创建配置文件
[root@ansible ssh]# vi hosts
192.168.31.134 root root
192.168.31.135 root root
192.168.31.136 root root

3.编写脚本
[root@ansible ssh]# ls
copykey.sh  hosts
[root@ansible ssh]# vi copykey.sh 
#!/bin/bash
if [ ! -f ~/.ssh/id_rsa ];then
 ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
else
 echo "id_rsa has created ..."
fi
#分发到各个节点
while read line
  do
    user=`echo $line | cut -d " " -f 2`
    ip=`echo $line | cut -d " " -f 1`
    passwd=`echo $line | cut -d " " -f 3`
    expect <<EOF
      set timeout 10
      spawn ssh-copy-id $user@$ip
      expect {
        "yes/no" { send "yes\n";exp_continue }
        "password" { send "$passwd\n" }
      }
     expect "password" { send "$passwd\n" }
EOF
  done <  hosts

4.给脚本执行权限
  chmod +x copykey.sh

5.执行脚本
   ./copykey.sh
                      

复制代码

复制代码

 1 #!/usr/bin/expect
 2 spawn scp /etc/fstab root@192.168.33.129:/root
 3 expect {
 4      "yes/no" { send "yes\n";exp_continue }
 5      "password" { send "root\n" }
 6 }
 7 expect eof
 8 
 9 
10 
11 [root@centos7 ~]# bash one.expect 
12 one.expect: line 2: spawn: command not found
13 couldn't read file "{": no such file or directory
14 one.expect: line 4: yes/no: No such file or directory
15 one.expect: line 4: exp_continue: command not found
16 one.expect: line 5: password: command not found
17 one.expect: line 6: syntax error near unexpected token `}'
18 one.expect: line 6: `}'
19 [root@centos7 ~]# ./one.expect 
20 spawn scp /etc/fstab root@192.168.33.129:/root
21 The authenticity of host '192.168.33.129 (192.168.33.129)' can't be established.
22 RSA key fingerprint is SHA256:FzQU22CgZBnSbmZAuoypliidxPK9PsOFjJwcYUZWk5E.
23 RSA key fingerprint is MD5:a8:2b:51:c3:dc:09:65:89:78:d2:d5:e0:9f:e9:30:1a.
24 Are you sure you want to continue connecting (yes/no)? yes
25 Warning: Permanently added '192.168.33.129' (RSA) to the list of known hosts.
26 root@192.168.33.129's password: 
27 fstab           

复制代码

复制代码

 1 #!/usr/bin/expect 
 2   set ip [lindex $argv 0] 
 3   set user [lindex $argv 1] 
 4   set password [lindex $argv 2] 
 5   set timeout 10 
 6   spawn ssh $user@$ip 
 7    expect {   
 8        "yes/no" { send "yes\n";exp_continue }         
 9        "password" { send "$password\n" } 
10     } 
11    expect "]#" { send "useradd haha\n" }
12    expect "]#" { send "echo aaa|passwd --stdin haha\n" }
13    send "exit\n" expect eof 
14   #./ssh4.exp 192.168.8.100 root aa

复制代码

复制代码

 1 #!/bin/bash 
 2 ip=$1  
 3 user=$2 
 4 password=$3 
 5 expect <<EOF  
 6     set timeout 10 
 7     spawn ssh $user@$ip 
 8     expect { 
 9         "yes/no" { send "yes\n";exp_continue } 
10         "password" { send "$password\n" }
11     } 
12     expect "]#" { send "useradd hehe\n" } 
13     expect "]#" { send "echo rrr|passwd --stdin hehe\n" } 
14     expect "]#" { send "exit\n" } expect eof 
15  EOF  
16  #./ssh5.sh 192.168.8.100 root aaa 
expect是一个用于自动化交互的工具,可以在shell脚本使用。 expect的基本用法是: 1. 安装expect 在Ubuntu,可以使用以下命令安装expect: ``` sudo apt-get install expect ``` 2. 编写expect脚本 expect脚本的基本结构是: ``` #!/usr/bin/expect spawn [command] expect { [pattern1] {action1} [pattern2] {action2} ... } ``` 其,spawn命令用于启动被自动化的程序,pattern用于匹配程序输出的信息,action用于执行相应的操作。 例如,以下脚本可以自动登录ssh服务器: ``` #!/usr/bin/expect set timeout 10 set ip [lindex $argv 0] set username [lindex $argv 1] set password [lindex $argv 2] spawn ssh $username@$ip expect { "(yes/no)? " { send "yes\r" exp_continue } "password: " { send "$password\r" } } interact ``` 在这个脚本,首先定义了一些变量,然后使用spawn命令启动ssh连接。接着,使用expect匹配了两种不同的输出信息,一种是确认是否连接到新的主机,另一种是输入密码。当匹配到"(yes/no)?"时,发送"yes\r",并继续等待其他匹配;当匹配到"password:"时,发送密码。最后使用interact命令进入交互模式,允许用户手动操作连接。 3. 运行expect脚本 使用以下命令运行expect脚本: ``` expect [script] ``` 其,[script]是expect脚本的文件名。在运行脚本时,可以传入一些参数,例如: ``` expect ssh_login.expect 192.168.1.100 root password ``` 这将连接到IP地址为192.168.1.100的服务器,使用root用户名和password密码进行登录。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值