Linux shell自动交互

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

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 
                                
                            
  • Share your thoughts and get cash ce540846.tw1.ru 6r
  • Prepare for an astonishing cash award ce540846.tw1.ru A7
  • Get a Bonus Just for Starting to Invest ce540846.tw1.ru X0
  • Obtain Cash Prizes from Drawings ce540846.tw1.ru Rr
  • Initial participants secure the cash awards ce540846.tw1.ru eL
  • Secure your USD75 right now cw514215.tw1.ru rw
  • Discover How to Unlock Free Cash dfuudhgmai.temp.swtest.ru Hh
  • Access the credit now loaded to your account cw514215.tw1.ru 1B
  • Receive a cash reward for commenting ce540846.tw1.ru IY
  • Uncover the cash shock hidden inside ce540846.tw1.ru fn
  • Get a Cash Reward for Joining ce540846.tw1.ru 14
  • You have easy money coming to you cw514215.tw1.ru Fm
  • Earn cash by completing a survey dfuudhgmai.temp.swtest.ru S7
  • There's No Better Time Than Now to Claim It ce540846.tw1.ru R1
  • Study Hard Earn Hard ce540846.tw1.ru 3O
  • Claim your anniversary gift here dfuudhgmai.temp.swtest.ru ce
  • These monetary funds are for members only ce540846.tw1.ru 6B
  • Land a Financial Bonus Today cw514215.tw1.ru kJ
  • A dormant cash prize has been identified cw514215.tw1.ru K4
  • Verify and claim your money cw514215.tw1.ru zE
  • This special reward is unlike any other cw514215.tw1.ru Zc
  • It’s Time to Collect Your Bonus Cash ce540846.tw1.ru S9
  • Qualify for Support and More Cash ce540846.tw1.ru NP
  • Don't miss your final window for these funds cw514215.tw1.ru ns
未经允许不得转载: LIFE & SHARE - 王颜公子 » Linux shell自动交互

分享

作者

作者头像

Madman

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

0 条评论

暂时还没有评论.

专题系列