shell脚本----免交互

目录

  • 一、Here Document免交互
    • 1、Here Document概述
    • 2、Here Document使用注意事项
    • 3、Here Document变量设定
    • 4、Here Document常规用法
  • 二、expect基本命令
    • 1、expect概述
    • 2、expect基本命令
      • expect:
      • send:
      • spawn:
      • 结束符:
      • set命令:
      • exp_continue:
      • send_user:
      • 接收参数:
    • 3、expect语法
    • 4、expect嵌套执行
      • 1、直接方式
      • 2、嵌套执行
    • 示例:expect完成ftp登录

一、Here Document免交互

1、Here Document概述

  • 使用 I/O 重定向的方式将命令列表提供给交互式程序
  • 标准输入的一种替代品
  • 语法格式:
命令 <<标记
...
...
标记

2、Here Document使用注意事项

  • 标记可以使用任意合法字符
  • 结尾的标记一定要顶格写,前面不能有任何字符
  • 结尾的标记后面也不能有任何字符(包括空格)
  • 开头标记前后的空格会被省略

3、Here Document变量设定

  • 变量设定和替换:
vim test3.sh
#!/bin/bash
filename="test.txt"
i="company"
cat > $filename <<EOF
take him from home to $i
EOF
cat test.txt
take him from home to company
  • 关闭变量替换
vim test3.sh
#!/bin/bash
filename="test.txt"
i="company"
cat > $filename <<'EOF'
take him from home to $i
EOF
cat test.txt
take him from home to $i
  • 去除每行前面的TAB字符
vim test3.sh
#!/bin/bash
filename="test.txt"
i="company"
cat > $filename <<-'EOF'
       take him from home to $i
EOF
cat test.txt
take him from home to $i
  • 多行注释
vim test3.sh
#!/bin/bash
filename="test.txt"
i="company"
 <<-'EOF'
take him from home to $i   ##注释内容
EOF

4、Here Document常规用法

vim test1.sh
#!/bin/bash
read i <<EOF   
hello world
EOF
echo $i
./test1.txt
hello world
给用户设置密码的免交互
vim test2.sh
#!/bin/bash
passwd zhangsan <<EOF
123456
123456     ##每一条交互提示会执行一条交互内容
EOF
sh test2.sh

二、expect基本命令

1、expect概述

  • 建立在tcl之上的一个工具
  • 用于进行自动化控制和测试
  • 解决shell脚本中交互相关的问题
  • 需要用yum安装expect

2、expect基本命令

expect:

  • 判断上次输出结果中是否包含指定的字符串,如果有则立即返回,否则等待超时时间后返回
  • 只能捕捉由spawn启动的进程的输出
  • 用于接收命令执行后的输出,然后和期望的字符串匹配

send:

  • 向进程发送字符串,用于模拟用户的输入
  • 该命令不能自动回车,一般要加\r执行回车

spawn:

  • 启动进程,并跟踪后续交互信息

结束符:

  • expect :等待执行结束
  • interact :执行完成后保持交互状态,把控制权交给控制台

set命令:

  • 设置超时时间,过期则继续执行后续命令
  • 单位为秒
  • timeout -1 表示永不超时
  • 默认情况下,timeout 是10秒

exp_continue:

  • 允许expect 继续向下执行指令

send_user:

  • 回显命令,相当于echo

接收参数:

  • expect 脚本可以接收bash传递的参数
  • 可以使用[lindex $argv n]获得
  • n从0开始,分别是第一个,第二个参数

3、expect语法

  • 单一分支语法:
expect “passwd:”  {send  “mypasswd\r”;}
  • 多分支模式语法:
expect “aaa”  {send “AAA\r”}
expect “bbb”  {send “BBB\r”}
expect “ccc”  {send “CCC\r”}
  • 此模式中:只要执行了aaa,bbb或ccc中的一个,执行send后就退出语句
expect  {
  “aaa”  {send “AAA\r”}
  “bbb”  {send “BBB\r”}
   “ccc”  {send “CCC\r”}
}
  • continue表示继续执行后面的匹配
expect  {
  “aaa”  {send “AAA\r”;exp_continue}
  “bbb”  {send “BBB\r”;exp_continue}
  “ccc”  {send “CCC\r”}
}

4、expect嵌套执行

1、直接方式

#!/usr/bin/expect
  set timeout 60   ##超时
  log_file test.log   ##开启日志
  log_user 1    ##显示信息
  
  ##定义变量
  set hostname [lindex $argv 0]
  set password [lindex $argv 1]
  spawn ssh root@${hostname}   ##追踪指令
  expect {    ##捕捉提示信息
    "connecting (yes/no)"
    {send "yes\r";exp_continue}
    "*password"
    {send "${password}\r"}
}
interact   ##转交控制权

2、嵌套执行

#!/bin/bash
hostname=$1
password=$2
/usr/bin/expect <<-EOF
spawn ssh root@$hostname
expect {
  "(yes/no)"
  {send "yes\r";exp_continue}
  "*password"
  {send "$password\r"}
}
expect "*】#"
send "exit\r"
expect eof
EOF

示例:expect完成ftp登录

#!/usr/bin/expect 
set timeout 10
set ip [lindex $argv 0 ]
spawn ftp $ip
expect "Name*"
send "ftp\r"
expect "Password:*"
send "\r"
expect "ftp>*"
interact
expect eof

你可能感兴趣的:(shell脚本----免交互)