语法格式
命令<<标记
...
...
标记
[root@localhost ~]# vim hello.sh
#!/bin/bash
read i <<EOF
hello world
EOF
echo $i
[root@localhost ~]# chmod +x hello.sh
[root@localhost ~]# ./hello.sh
hello world
[root@localhost ~]# vim user.sh
#!/bin/bash
passwd lisi <<EOF
123abc
123abc
EOF
[root@localhost ~]# ./passwd.sh
更改用户 lisi 的密码 。
新的 密码:无效的密码: 密码少于 7 个字符
重新输入新的 密码:passwd:所有的身份验证令牌已经成功更新。
[root@localhost ~]# vim user.sh
#!/bin/bash
name="cllt"
filename="test.txt"
cat > $filename <<EOF
this is $name.com
EOF
[root@localhost ~]# cat test.txt
this is cllt.com
[root@localhost ~]# vim user.sh
#!/bin/bash
first="hello"
filename=$(cat <<EOF
this is cllt.com
my name is cllt
$first
EOF
)
echo $filename
[root@localhost ~]# ./user.sh
this is cllt.com my name is cllt hello
[root@localhost ~]# vim user.sh
#!/bin/bash
first="hello"
cat <<'EOF' '单引号关闭变量替换'
this is cllt.com
$first
EOF
[root@localhost ~]# ./user.sh
this is cllt.com
$first
[root@localhost ~]# vim user.sh
#!/bin/bash
first="hello"
cat <<-EOF '-表示抑制行首的TAB作用'
this is cllt.com
$first
EOF
[root@localhost ~]# ./user.sh
this is cllt.com
hello
通过Here Document方式使Bash支持多行注释
[root@localhost ~]# vim user.sh
#!/bin/bash
first="hello"
: <<EOF '标记之间为注释内容'
this is cllt.com
$first
EOF
echo "lemon"
[root@localhost ~]# ./user.sh
lemon
在Linux系统中expect一般不自带,需要自己安装
yum install expect -y
expect
判断上次输出结果中是否包含指定的字符串,如果有则立即返回,否则就等待超时时间后返回
只能捕捉由spawn启动的进程的输出
用于接收命令执行后的输出,然后和期望的字符串匹配
send
向进程发送字符串,用于模拟用户的输入
该命令不能自动回车换行,-般要加\r (回车)
spawn
启动进程,并跟踪后续交互信息
expect eof(结束符)
等待执行结束(执行自动化任务通常使用expect eof)
interact(结束符)
执行完成后保持交互状态,把控制权交给控制台
set
设置超时时间,过期则继续执行后续指令
单位是秒
timeout -1表示永不超时
默认情况下,timeout是10秒
exp_contiune
允许expec继续向下执行指令
send_user
回显命令,相当于echo
接收参数
Expect脚本可以接受从bash传递的参数
可以使用[lindex $argv n]获得
n从0开始,分别表示第一 个,第二个,第三…参数
expect "password: " {send "mypassword\r“;}
expect "aaa" {send“AAA\r"} 'send命令不具备回车换行功能,一般要加\r或\n'
expect "bbb" {send“BBB\r"}
expect "ccc" {send“CCC\r"}
expect { '只要匹配了aaa或bbb或ccc中的任何一个,执行相应的send语句后退出该expect语句'
"aaa"{send“AAA\r"}
"bbb" {send“BBB\r"}
"CCC" {send“CCC\r"}
}
expect { 'exp_ continue表示继续后面的匹配,如果匹配了aaa, 执行完send语句后还要继续向下匹配bbb'
"aaa" {send“AAA"; exp_ continue}
"bbb" {send“BBB"; exp_ continue}
"ccc" {send“CCC"}
}
[root@localhost ~]# vim demo01.sh
#!/usr/bin/expect
#超时时间
set timeout 20
#开启日志
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
[root@localhost ~]# ./demo01.sh 192.168.179.144 123456
spawn ssh root@192.168.179.144
root@192.168.179.144's password:
Last login: Tue Jul 28 09:34:58 2020 from 192.168.179.1
[root@server ~]#
[root@localhost ~]# vim demo02.sh
#!/bin/bash
hostname=$1
password=$2
#expect嵌入
/usr/bin/expect <<-EOF
spawn ssh root@${hostname}
#捕捉提示信息
expect {
"connecting (yes/no)"
{send "yes\r";exp_continue}
"*password:"
{send "${password}\r";}
}
expect "*]#"
send "exit\r"
expect eof
EOF
[root@localhost ~]# ./demo02.sh 192.168.179.144 123456
spawn ssh root@192.168.179.144
root@192.168.179.144's password:
Last login: Tue Jul 28 09:35:17 2020 from 192.168.179.100
[root@server ~]# exit
登出
Connection to 192.168.179.144 closed.
[root@localhost ~]#
[root@localhost ~]# vim demo03.sh
#!/bin/bash
users=$1
passwd=$2
useradd ${users}
#expect嵌入
/usr/bin/expect <<-EOF
spawn passwd ${users}
#捕捉提示信息
expect {
"密码:"
{send "$2\r";exp_continue}
"密码:"
{send "$2\r";}
}
#expect eof
EOF
[root@localhost ~]# ./demo03.sh wangwu 123456
spawn passwd wangwu
更改用户 wangwu 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@localhost ~]# vim demo04.sh
#!/usr/bin/expect
set timeout 5
set hostname [lindex $argv 0]
set password [lindex $argv 1]
spawn ssh ${hostname}
expect {
"Connection refused" exit
"Name or service not known" exit
"connecting (yes/no)"{send "yes\r";exp_continue}
"*password:"{send "${password}\r"}
}
interact
exit
[root@localhost ~]# ./demo04.sh 192.168.179.144 123456
spawn ssh 192.168.179.144
root@192.168.179.144's password:
Last login: Tue Jul 28 09:50:59 2020 from 192.168.179.100
[root@server ~]# exit
[root@localhost ~]# ./demo04.sh
spawn ssh root@
ssh: Could not resolve hostname : Name or service not known
[root@localhost ~]#