Shell 中的 expect 命令使用

这篇博客介绍了如何利用 Expect 工具编写脚本,实现SSH登录远程主机和SCP文件传输的自动化。通过示例详细解释了expect命令的语法,包括spawn、send、expect等,并展示了如何处理交互式认证和避免子shell问题。此外,还给出了处理多分支模式的expect脚本示例,以及在shell中调用expect进行文件拷贝和执行多条命令的操作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 "Goodbye\n" } 

    匹配 hi, hehe, bye 中的任意字符串时, 发送相应字符串。等同于:

          expect { "hi" { send "You said hi\n" } "hehe" { send "Hehe yourself\n" } "bye" { send "Goodbye\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

读取配置文件自动执行ssh

示例二

#!/usr/bin/expect
spawn scp /etc/fstab root@192.168.33.129:/root
expect {
     "yes/no" { send "yes\n";exp_continue }
     "password" { send "root\n" }
}
expect eof



[root@centos7 ~]# bash one.expect 
one.expect: line 2: spawn: command not found
couldn't read file "{": no such file or directory
one.expect: line 4: yes/no: No such file or directory
one.expect: line 4: exp_continue: command not found
one.expect: line 5: password: command not found
one.expect: line 6: syntax error near unexpected token `}'
one.expect: line 6: `}'
[root@centos7 ~]# ./one.expect 
spawn scp /etc/fstab root@192.168.33.129:/root
The authenticity of host '192.168.33.129 (192.168.33.129)' can't be established.
RSA key fingerprint is SHA256:FzQU22CgZBnSbmZAuoypliidxPK9PsOFjJwcYUZWk5E.
RSA key fingerprint is MD5:a8:2b:51:c3:dc:09:65:89:78:d2:d5:e0:9f:e9:30:1a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.33.129' (RSA) to the list of known hosts.
root@192.168.33.129's password: 
fstab

示例三

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

执行多条命令

示例四

#!/bin/bash 
ip=$1  
user=$2 
password=$3 
expect <<EOF  
    set timeout 10 
    spawn ssh $user@$ip 
    expect { 
        "yes/no" { send "yes\n";exp_continue } 
        "password" { send "$password\n" }
    } 
    expect "]#" { send "useradd hehe\n" } 
    expect "]#" { send "echo rrr|passwd --stdin hehe\n" } 
    expect "]#" { send "exit\n" } expect eof 
 EOF  
 #./ssh5.sh 192.168.8.100 root aaa 

shell调用expect

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值