centos 7.6——shell脚本——免交互

shell脚本——免交互

一、 Here DOcument

1. here Document 概述

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

语法格式

命令 <<标记

标记

2. Here Document 使用注意事项

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

3. Here Doucument免交互

(1)通过read命令接收输入并打印


[root@promote data]# vim here_no_interactive_read.sh

#!/bin/bash
read i <<EOF
Hi
EOF
echo $i

******验证**************
[root@promote data]# chmod +x here_no_interactive_read.sh 
[root@promote data]# ./here_no_interactive_read.sh 
Hi




(2)通过passwd给用户设置密码

[root@promote data]# vim here_non_interactive_passwd.sh

#!/bin/bash
passwd jerry <<EOF
123
123
EOF


******验证**************

[root@promote data]# chmod +x here_no_interactive_passwd.sh
[root@promote data]# useradd jerry
[root@promote data]# ./here_non_interactive_passwd.sh 
更改用户 jerry 的密码 。
新的 密码:无效的密码: 密码少于 8 个字符
重新输入新的 密码:passwd:所有的身份验证令牌已经成功更新。


4. Here Document 变量替代

变量的替代

[root@promote data]# vim here_var_replace.sh

#!/bin/bash
doc_file="2019.txt"
i="company"
cat>$doc_file <<HERE
Take him from home to $i
HERE

******验证**************
[root@promote data]# chmod +x here_var_replace.sh 
[root@promote data]# ./here_var_replace.sh 
[root@promote data]# cat 2019.txt
Take him from home to company


5. Here Document 变量设定

[root@promote data]# vim here_var_set.sh

#!/bin/bash
ivar="Great Beautyful"
myvar=$(cat <<EOF
this is Line 1
that are sun,moon and stars.
$ivar
EOF
)
echo $myvar

******验证**************
[root@promote data]# sh here_var_set.sh 
this is Line 1 that are sun,moon and stars. Great Beautyful


6. 关闭变量替换功能

(1)去除每行之前的TAB作用


[root@promote data]# vim here_format_shut.sh

#!/bin/bash
cat <<-'EOF'    //'-'表示抑制行首的TAB作用
this is line 1
$kgc
EOF

******验证**************
[root@promote data]# sh here_format_shut.sh 
this is line 1
$kgc

7.Here Document 多行注释

通过Here Document 方式使Bash 支持多行注释
语法格式

: << DO-NOTHING

第一行注释
第二行注释

DO-NOTHING

二、Expect 概论

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

基本命令

expect

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

send

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

spawn

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

结束符

expect eof

等待执行结束

interact

执行完成后保持交互状态,把控制权交给控制台

set

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

exp_continue

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

send_user

  • 回显命令,相当于echo

8. 接收参数

  • expect 脚本可以接受从bash 传递的参数
  • 可以使用 set 变量名 [lindex $argv n] 获得
  • n 从0开始,分别表示第一个,第二个,第三个…参数

expect语法

(1)单一分支语法

expect “passwd:” {send “mypassword\r”;}

(2)多分支模式语法

expect “aaa” {send “AAA\r”}

expect “bbb” {send “BBB\r”}

expect “ccc” {send “CCC\r”}

expect {
“aaa”;{send “AAA\r”}
“bbb” ;{send “BBB\r”}
“ccc” ;{send “CCC\r”}
}

expect {
“aaa” {send “AAA”;exp_continue} //exp_continue继续执行下面一条指令
“bbb” {send “BBB”;exp_continue}
“ccc” {send “CCC”}
}

直接执行

案例一:自动新建用户

首先安装expect

yum -y install expect

[root@promote data]# vim passwd.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

验证

chmod +x passwd.sh   
./passwd.sh 192.168.75.100 ty2964  //因为不是bash环境下所以,用./来执行
spawn ssh root@192.168.75.100
root@192.168.75.100's password: 
Last login: Mon Jul 27 15:43:14 2020 from 192.168.75.134

exit   //退出
 


嵌入执行

案例二:无交互创建新用户



[root@promote data]# vim jack.sh

#!/bin/bash
username=$1
set timeout=20
useradd ${username}
/usr/bin/expect <<-EOF 
spawn passwd ${username}
expect { 
        "新的"
        {send "123\r";exp_continue}
        "重新"
        {send "123\r";}
}


EOF


*****验证********

[root@promote data]# ./jack.sh jack1
spawn passwd jack1
更改用户 jack1 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。

案例三:实现ssh自动登录


[root@promote ~]# vim expect_auto_ssh.sh

#!/usr/bin/expect
set timeout 5
set hostname [lindex $argv 0]
set password [lindex $argv 1]
spawn ssh@root$hostname
expect {
        "Connection refused" exit
        "Name or service not known" exit
        "to continue"; {send "yes\r";exp_continue}
        "password:";{send "$password\r"}
}
interact   ##携带interact参数表示登录成功后将控制台交给用户,否则登录完成后将退出
exit

*****验证********
[root@promote data]# ./expect_auto_ssh.sh 192.168.75.100 ty2964
spawn ssh 192.168.75.100
root@192.168.75.100's password: 
Last login: Tue Jul 28 11:57:45 2020 from 192.168.75.134
[root@localhost ~]# exit



案例四:利用expect ftp登录



[root@promote data]# vim expect_ftp.sh

#!/usr/bin/expect -f
set timeout 10
spawn ftp 192.168.75.100
expect "Name*"
send "ftp\r"
expect "Password:*"
send "\r"
expect "ftp>*"
interact
expect eof

*****验证*******

        

猜你喜欢

转载自blog.csdn.net/weixin_42099301/article/details/107613339
今日推荐