Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the expect command in linux

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

本篇文章为大家展示了linux中的expect命令怎么用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

expect介绍

expect 是由Don Libes基于Tcl(Tool Command Language )语言开发的,主要应用于自动化交互式操作的场景,借助Expect处理交互的命令,可以将交互过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成。尤其适用于需要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员的工作效率

expect命令expect语法:

expect [选项][ -c cmds] [ [ -[f|b] ] cmdfile] [ args]

选项

-c:从命令行执行expect脚本,默认expect是交互地执行的

示例:

expect -c 'expect "\n" {send"pressed enter\n"}

-d:可以输出输出调试信息

示例:

expect -d ssh.exp

expect中相关命令

spawn:启动新的进程send:用于向进程发送字符串expect:从进程接收字符串interact:允许用户交互exp_continue匹配多个字符串在执行动作后加此命令except最常用的语法(tcl语言:模式–动作)单一分支模式语法:

expect "hi" {send "You said hi\n"}

匹配到hi后,会输出"you said hi",并换行

多分支模式语法:

expect"hi" { send "You said hi\n" } \

"hehe"{ send "Hehe yourself\n" } \

"bye"{ send "Good bye\n" }

匹配hi,hello,bye任意字符串时,执行相应输出。等同如下:

expect {

"hi"{ send "You said hi\n"}

"hehe"{ send "Hehe yourself\n"}

"bye"{ send "Good bye\n"}

}

示例

cat ssh2.exp

#!/usr/bin/expect

spawn ssh 192.168.8.100

expect {

"yes/no" { send"yes\n";exp_continue }

"password" { send"magedu\n" }

}

interact

#expect eof

示例:变量

cat ssh3.exp

#!/usr/bin/expect

set ip 192.168.8.100

set user root

set password magedu

set timeout 10

spawn ssh $user@$ip

expect {

"yes/no" { send"yes\n";exp_continue }

"password" { send"$password\n" }

}

interact

示例:位置参数

vim ssh4.exp

#!/usr/bin/expect

set ip [lindex $argv 0]

set user [lindex $argv 1]

set password [lindex $argv 2]

spawn ssh $user@$ip

expect {

"yes/no" { send"yes\n";exp_continue }

"password" { send"$password\n" }

}

interact

#./ssh4.exp 192.168.8.100 rootmagedu

示例:执行多个命令

cat ssh5.exp

#!/usr/bin/expect

set ip [lindex $argv 0]

set user [lindex $argv 1]

set password [lindex $argv 2]

set timeout 10

spawn ssh $user@$ip

expect {

"yes/no" { send"yes\n";exp_continue }

"password" { send"$password\n" }

}

expect "]#" { send"useradd haha\n" }

expect "]#" { send"echo magedu |passwd –stdin haha\n" }

send "exit\n"

expect eof

#./ssh5.exp 192.168.8.100 rootmagedu

示例:shell脚本调用expect

vim ssh6.sh

#!/bin/bash

ip=$1

user=$2

password=$3

expect

set timeout 10

spawn ssh$user@$ip

expect {

"yes/no" { send"yes\n";exp_continue}

"password" { send"$password\n" }

}

expect "]#" { send"useraddhehe\n" }

expect "]#" { send "echomagedu|passwd–stdinhehe\n" }

expect "]#" { send"exit\n" }

expect eof

EOF

#./ssh6.sh 192.168.8.100 rootmagedus

什么是Linux系统Linux是一种免费使用和自由传播的类UNIX操作系统,是一个基于POSIX的多用户、多任务、支持多线程和多CPU的操作系统,使用Linux能运行主要的Unix工具软件、应用程序和网络协议。

上述内容就是linux中的expect命令怎么用,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report