赞
踩
目录
3.5.再使用 send 发送需要交互输入的字符串,结尾需要加上回车符
-
- 命令 <<开始标记
-
- ...
-
- ...
-
- 结尾标记
- [root@localhost ~]# read a <<EOF
- > zx like xiaojiejie
- > EOF
- [root@localhost ~]# echo $a
- zx like xiaojiejie
- [root@localhost ~]# wc -l <<EOF
- > abc
- > 123
- > xyz
- > 567
- > EOF
- 4
- [root@localhost ~]# passwd zhangsan <<EOF
- > abc12345
- > abc12345
- > EOF
原文件内容
- [root@localhost ~]# vim test3.txt
- [root@localhost ~]# tee test3.txt <<EOF
- > zx like xiaomeimei
- > EOF
- zx like xiaomeimei
- [root@localhost ~]# cat test3.txt
- zx like xiaomeimei
- [root@localhost day20]# vim demo1.sh
- [root@localhost day20]# bash demo1.sh
- [root@localhost day20]# cd /opt/
- [root@localhost opt]# ls
- local.repo rh
- #!/bin/bash
- file_path=/opt/local.repo
- repo_path=/mnt
-
- cat <<EOF > $file_path
- [local]
- name=local
- baseurl=file://$repo_path
- enabled=1
- gpgcheck=0
- EOF
- [root@localhost day20]# vim demo1.sh
- [root@localhost day20]# bash demo1.sh
- [root@localhost day20]# cd /opt/
- [root@localhost opt]# ls
- base.repo demo1.sh local.repo rh
- #!/bin/bash
- file_path=/opt/base.repo
- repo_path=/data
-
- cat <<EOF > $file_path
- [local]
- name=local
- baseurl=file://$repo_path
- enabled=1
- gpgcheck=0
- EOF
- [root@localhost day20]# vim demo1.sh
- [root@localhost day20]# bash demo1.sh
- [local]
- name=local
- baseurl=file:///data
- enabled=1
- gpgcheck=0
- #!/bin/bash
- file_path=/opt/base.repo
- repo_path=/data
-
- cat <<EOF
- [local]
- name=local
- baseurl=file://$repo_path
- enabled=1
- gpgcheck=0
- EOF
- [root@localhost day20]# vim demo1.sh
- [root@localhost day20]# bash demo1.sh
- [local]
- name=local
- baseurl=file://$repo_path
- enabled=1
- gpgcheck=0
- #!/bin/bash
- file_path=/opt/base.repo
- repo_path=/data
-
- cat <<'EOF'
- [local]
- name=local
- baseurl=file://$repo_path
- enabled=1
- gpgcheck=0
- EOF
加 :
注意:换#号效果会更好
开头标记前要加 -
[root@localhost day20]# yum install -y expect
#!/usr/bin/expect
- set 变量名 [lindex $argv 0]
-
- 0代表执行脚本时后面跟的第一个位置参数,第二个位置参数用 1 表示,依此类推
spawn fdisk /dev/sdb
expect "命令(输入 m 获取帮助):"
- send "n\r"
- ....
- #!/usr/bin/expect
-
- set dest_host [lindex $argv 0]
- set password [lindex $argv 1]
-
- spawn ssh $dest_host
- expect {
- "Connection refused" {send_user "目标主机访问被拒绝\n"}
- "No route to host" {send_user "目标主机没有开机\n"}
- "(yes/no)" {send "yes\r"; exp_continue;}
- "*password:" {send "$password\r"}
- }
-
- interact
虚拟机上关闭
虚拟机上开启
成功
- #!/usr/bin/expect
-
- set username [lindex $argv 0]
- set password [lindex $argv 1]
-
- spawn passwd $username
- expect "新的 密码: " {send "${password}\r"}
- expect "重新输入*" {send "${password}\r"}
-
- expect eof
- [root@localhost day20]# chmod +x demo3.sh
- [root@localhost day20]# ./demo3.sh zhangsan 123456
- #!/usr/bin/expect
-
- set timeout 1
-
- set username [ lindex $argv 0 ]
- set password [ lindex $argv 1 ]
-
- spawn su $username
- expect "密码"
- send "$password\n"
-
- expect "*]#"
- send_user "$username 切换成功!"
-
- interact
- #!/usr/bin/expect
-
- set timeout 5
- set name [lindex $argv 0]
-
- spawn fdisk $name
- expect "获取帮助"
- send "n\r"
- expect "Select"
- send "p\r"
- expect "分区号"
- send "\r"
- expect "起始"
- send "\r"
- expect "Last"
- send "+10G\r"
- expect "命令"
- send "w\r"
-
- interact
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。