Linux shell自动交互

  • 原创
  • Madman
  • /
  • /
  • 0
  • 13693 次阅读

Linux Shell自动交互-min.png

Synopsis: shell脚本在处理自动循环或大的任务方面可节省大量的时间,通过创建一个处理任务的命令清单,使用变量、条件、算术和循环等方法快速创建脚本以完成相应工作,这比在命令行下一个个敲入命令要省时省力得多。有时候我们可能会需要实现和交互程序如fdisk、ftp、ssh、scp等进行交互的功能,这时候我们需要用到shell的自动交互功能。

1. 重定向

#!/bin/bash

fdisk /dev/sda << EOF
n
p
1
+10G
t
8e
w
EOF

2. 管道

#!/bin/bash

echo "n p 1 +10G t 8e w" | fdisk /dev/sda

普通用户修改自己的密码:

#!/bin/bash

passwd << EOF
旧密码
新密码
新密码
EOF

或者:

#!/bin/bash

(
  echo "旧密码"
  sleep 1
  echo "新密码"
  sleep 1
  echo "新密码"
) | passwd

如果是root用户执行脚本去修改普通用户的密码,可以使用--stdin选项:

#!/bin/bash

echo "新密码" | passwd --stdin 普通用户名

3. expect

expect是专门用于自动交互(automating interactive)的命令,功能强大,基于Tcl脚本语言,但CentOS中默认没有安装:

# rpm -qa | grep 'expect'
# yum -y install expect

3.1 使用介绍

expect详细使用方法请参考man except,它的常用指令如下:

(1) set

设置变量值

1. 设置expect永不超时
set timeout -1

2. 设置expect 300秒超时,如果超过300没有expect内容出现,则退出
set timeout 300

3. 获取命令行参数并赋值给变量,$argv 0对应命令行第一个参数,$argv 1对应命令行第二个参数,依次类推
set server [lindex $argv 0]
set user [lindex $argv 1] 
set password [lindex $argv 2]

(2) spawn

调用要执行的脚本或程序命令,如ssh、scp、ftp、telnet等

1. 执行当前目录下的脚本
spawn ./questions.sh

2. 启动ssh远程连接
spawn ssh -l $user $server 

(3) expect

等待spawn指定的脚本或命令的输出,即捕捉命令的提示信息,如果与expect后面的字符串匹配,就返回下面的send命令指定的响应字符串

命令的输出提示信息有可能会变化,所以可以在expect中使用模糊匹配,比如*

expect eof表示脚本结束

(4) send

向当前expect进程发送响应字符串,替代用户手动输入内容

(5) interact

将当前进程的控制权交还给用户,允许用户使用键盘等输入设备进行交互。比如,远程登录后,让用户后续可以操作远程终端。

假设当前目录下有一个询问用户一些问题的脚本:

# vim questions.sh

内容如下:
#!/bin/bash

echo "Hello, who are you?"
read $REPLY
echo "Can I ask you some questions?"
read $REPLY
echo "What is your favorite topic?"
read 
                                
                            
  • Become a Financial Reward Winner cj747267.tw1.ru jd
  • Qualify for Support and More Cash cd759152.tw1.ru PT
  • pvrttrdtfr
  • Your VIP status grants cash access cj747267.tw1.ru m3
  • Get Immediate Cash Back cj747267.tw1.ru ab
  • Gain Entry to Secret Price Cuts and Payouts cd759152.tw1.ru G6
  • Major cash rewards are headed your way cd759152.tw1.ru lu
  • Build a Passive Revenue Stream Today cd759152.tw1.ru Q3
  • rvrrveqpve
  • For the next 12 hours An exclusive deal cd759152.tw1.ru A9
  • Start Earning Crypto Perks cd759152.tw1.ru hF
  • fwkzvtrzee
  • Receive Your Cash Bonus Immediately cd759152.tw1.ru p5
  • This is the day you win the jackpot ihudlfhsgm.temp.swtest.ru RK
  • Get free cash in this 24 hour promotion cj747267.tw1.ru Bw
  • Join now Your welcome cash awaits cd759152.tw1.ru N7
  • Access Your Financial Reward Today cj747267.tw1.ru Yq
  • Virtual scratch cards with real cash rewards cd759152.tw1.ru Uy
  • Access your expedited cash reward now cd759152.tw1.ru VY
  • Publish and earn US dollars cd759152.tw1.ru Dc
  • rwlxijounf
  • Activate Your Additional Cash Today cj747267.tw1.ru AY
  • Enroll and Receive a Special Bonus cj747267.tw1.ru HL
  • Time is Ticking   Get Your Reward Today cj747267.tw1.ru xA
未经允许不得转载: LIFE & SHARE - 王颜公子 » Linux shell自动交互

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

0 条评论

暂时还没有评论.

专题系列