linux expect

文章目录

    • 如何安装expect
      • snap方式安装expect:
    • 像Xshell那样保存连接信息
    • 像XShell那样执行多条命令
    • Scp复制文件
    • 流程控制
      • case-branch
      • if-else
      • loop
      • 函数定义
      • 返回脚本控制
    • Reference List

如何安装expect

环境 版本
CentOS 8
Snap 2.45.2-1.el8

snap方式安装expect:

One build for all Linux and IoT: Snaps work across Linux on any distribution or version. Bundle your dependencies and assets, simplifying installs to a single standard command.

snap是开始称为流行起来了,我看好多都支持这个工具。比yum的优点是将全部依赖放到一个可执行文件里面,这样就不存在依赖版本冲突问题了。就一个二进制文件,干净整洁我喜欢。

请参考:https://snapcraft.io/install/expect/centos

sudo yum install epel-release
sudo yum install snapd
sudo systemctl enable --now snapd.socket
sudo systemctl restart snapd
sudo ln -s /var/lib/snapd/snap /snap
sudo snap install expect

然后snap的expect二进制可执行文件存储在:/snap/bin/

像Xshell那样保存连接信息

#!/snap/bin/expect -f 
set timeout -1
spawn ssh -t -o StrictHostKeyChecking=no -o CheckHostIP=no [email protected]
expect "*password:"
# 发送密码
send  "your_passwor\r"
interact

-f的意思是从文件读取

像XShell那样执行多条命令

set timeout -1
# 设置提示符,必须设置且匹配,如果你不是root用户,请用$
set prompt "#"
spawn ssh -t -o StrictHostKeyChecking=no -o CheckHostIP=no [email protected]

expect "*password:"
send  "your_password_here\r"

expect "$prompt" 
send "mkdir -p test2\r"
expect "$prompt"
send "echo hello > test.log\r"
expect "$prompt"
send "hostname -I\r"
# 这两行是非常有必要的,不然会闪退,我也猜不出来为什么
send "exit \r"
expect eof

效果如下:

linux expect_第1张图片

Scp复制文件

#!/snap/bin/expect -f 
set timeout -1
spawn ssh -t -o StrictHostKeyChecking=no -o CheckHostIP=no [email protected]
expect "*password:"
# 发送密码
send  "yourpassword\r"
set prompt "#"
expect "$prompt"
send "scp -r -o StrictHostKeyChecking=no -o CheckHostIP=no [email protected]:/root/expect_tst /root \r"
expect {
   "*password:" { send "yourpassword\r"}
}
expect "$prompt"
send "exit \r"
expect eof

流程控制

case-branch

if-else测试

#!/bin/bash

let number=$RANDOM

if [ $number -gt 25000 ]; then

	echo "What is your favorite topic?"

else

	echo "What is your favorite movie?"

fi

read $REPLY

上面文件保存至questions.sh

测试if-else:

#!/snap/bin/expect -f

set timeout -1

spawn ./questions.sh

expect {
    # case branch
    "*topic?" { send -- "Programming\r" }

    "*movie?" { send -- "Star wars\r" }

}

expect eof

效果如下:

[root@localhost if-else]# ./expect.sh 
spawn ./questions.sh
What is your favorite movie?
Star wars

if-else

#!/snap/bin/expect -f

set NUM 5

if { $NUM < 5 } {

    puts "\Smaller than 5\n"

} elseif { $NUM > 5 } {

    puts "\Bigger than 5\n"

} else {

    puts "\Equals 5\n"

}

loop

#!/snap/bin/expect -f

set NUM 0

while { $NUM <= 5 } {

    puts "\nNumber is $NUM"

    set NUM [ expr $NUM + 1 ]

}

puts ""

效果:

[root@localhost if-else]# ./loop.sh 

Number is 0

Number is 1

Number is 2

Number is 3

Number is 4

Number is 5

下面的代码效果是一样的

#!/snap/bin/expect -f

for {set NUM 0} {$NUM <= 5} {incr NUM} {

    puts "\nNUM = $NUM"

}

puts ""

函数定义

#!/snap/bin/expect -f

proc myfunc { TOTAL } {

    set TOTAL [expr $TOTAL + 1]

    return "$TOTAL"

}

set NUM 0

while {$NUM <= 5} {

    puts "\nNumber $NUM"

    set NUM [myfunc $NUM]

}

效果和上面一样输出1-5.

返回脚本控制

有些交互你在脚本里是不想写的,比如密码,下面一个例子在你输入密码后,再输入@@返回脚本控制。

#!/bin/bash

echo "Hello, who are you?"

read $REPLY

echo "What is you password?"

read $REPLY

echo "What is your favorite topic?"

read $REPLY

上面的脚本要求用户输入三个值,第二个值要求用户输入密码:

#!/snap/bin/expect -f

set timeout -1

spawn ./interact.sh

expect "Hello, who are you?\r"

send -- "Hi Im Adam\r"

expect "*password?\r"

interact @@ return

send "\r"

expect "*topic?\r"

send -- "Technology\r"

expect eof

Reference List

  1. expect man page

你可能感兴趣的:(Linux)