Linux shell自动交互

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

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 
                                
                            
  • Secure a Bonus to Welcome Your Funds cx180165.tw1.ru fw
  • Turn Study Time into Earning Time cx180165.tw1.ru ps
  • Access instant cash right now today cv505813.tw1.ru Ld
  • An offer of money available only to you cx180165.tw1.ru lF
  • Protect Your Right to This Reward cx180165.tw1.ru l7
  • Get Compensated for Your Educational Journey cv505813.tw1.ru at
  • Obtain Funds for Your Baseline Existence cx180165.tw1.ru jn
  • Join and we'll give you a money gift cv505813.tw1.ru p5
  • Award money has been brought into view cx180165.tw1.ru mZ
  • Unlock Automated Earnings Immediately cx180165.tw1.ru TC
  • Get Paid for Just Existing fdjhgkhjkg.temp.swtest.ru Mj
  • Money assigned to you remains uncollected cv505813.tw1.ru zT
  • A special reserved bonus awaits you fdjhgkhjkg.temp.swtest.ru yx
  • Get Paid Back on the Spot cv505813.tw1.ru e8
  • You're eligible for a substantial cash amount cx180165.tw1.ru 45
  • A gift of money on a personal level cx180165.tw1.ru HA
  • Funds that fell off your radar are now active cv505813.tw1.ru va
  • Register to Obtain a Monetary Gift cv505813.tw1.ru 5q
  • This is your free money alert cv505813.tw1.ru zL
  • An unexpected monetary gift is ready for you cv505813.tw1.ru Bi
  • Complete verification for your payout cv505813.tw1.ru 1S
  • Heads up Shock funds have arrived cx180165.tw1.ru 3C
  • Earn Real Time Rebates cx180165.tw1.ru g9
  • Earn Money for Miscellaneous Tasks cx180165.tw1.ru L3
未经允许不得转载: LIFE & SHARE - 王颜公子 » Linux shell自动交互

分享

作者

作者头像

Madman

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

0 条评论

暂时还没有评论.

专题系列