Fork me on GitHub

Linux下expect脚本语言的简单使用

背景

最近在做直播服务器性能测试,因单台推流服务器性能限制,需多台机器同时推流,而且要监控直播平台各个底层服务器的性能,手动推流并开启监控繁琐而且易出错,故有如下内容。

什么是Expect

介绍

Expect是一个用来实现自动交互功能的软件套件。使用它,系统管理员可以创建脚本来对命令或程序进行输入,而这些命令和程序是期望从终端(terminal)得到输入,一般来说这些输入都需要手工输入进行的。Expect则可以根据程序的提示模拟标准输入提供给程序需要的输入来实现交互程序执行。甚至可以实现简单的BBS聊天机器人。

Expect是不断发展的,随着时间的流逝,其功能越来越强大,已经成为系统管理员的的一个强大助手。Expect需要Tcl编程语言的支持,要在系统上运行Expect必须首先安装Tcl。

Expect的四个关键命令:

spawn:启动新的进程
expect:从进程接收字符串
send:用于向进程发送字符串
interact:允许用户交互

安装

直接使用yum安装相关依赖库以及expect

yum install tcl
yum install expect

使用

pushtestall.sh

远程登录启动nmon监控和ffmpeg推流脚本

#!/usr/bin/expect -f  

set password CDVcloud123 
set listnmon /home/huangyl/nmon
set listffmpeg /home/huangyl
set script loop.sh
set timeout -1

#外网登录推流1
spawn ssh root@101.201.237.216
expect {
 "*yes/no" { send "yes\r"; exp_continue }  
 "*password:" { send "$password\r" }  
}

#登录入口1,并开启nmon监控
spawn ssh root@172.18.1.14
expect {
 "*yes/no" { send "yes\r"; exp_continue }  
 "*password:" { send "$password\r" }  
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "cd $listnmon\r"
expect "*#"
send "./nmon -f -s 5 -c 800\r"

#登录入口2,并开启nmon监控
spawn ssh root@172.18.29.128
expect {
 "*yes/no" { send "yes\r"; exp_continue }
 "*password:" { send "$password\r" }
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "cd $listnmon\r"
expect "*#"
send "./nmon -f -s 5 -c 800\r"


#登录出口1,并开启nmon监控
spawn ssh root@172.18.29.124
expect {
 "*yes/no" { send "yes\r"; exp_continue }
 "*password:" { send "$password\r" }
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "cd $listnmon\r"
expect "*#"
send "./nmon -f -s 5 -c 800\r"


#登录出口2,并开启nmon监控
spawn ssh root@172.18.1.15
expect {
 "*yes/no" { send "yes\r"; exp_continue }
 "*password:" { send "$password\r" }
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "cd $listnmon\r"
expect "*#"
send "./nmon -f -s 5 -c 800\r"



#登录接口1,并开启nmon监控
spawn ssh root@172.18.1.17
expect {
 "*yes/no" { send "yes\r"; exp_continue }
 "*password:" { send "$password\r" }
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "cd $listnmon\r"
expect "*#"
send "./nmon -f -s 5 -c 800\r"


#登录接口2,并开启nmon监控 
spawn ssh root@172.18.29.125
expect {
 "*yes/no" { send "yes\r"; exp_continue }
 "*password:" { send "$password\r" }
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "cd $listnmon\r"
expect "*#"
send "./nmon -f -s 5 -c 800\r"


#登录直播1,并开启nmon监控
spawn ssh root@172.18.1.16
expect {
 "*yes/no" { send "yes\r"; exp_continue }
 "*password:" { send "$password\r" }
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "cd $listnmon\r"
expect "*#"
send "./nmon -f -s 5 -c 800\r"


#登录直播2,并开启nmon监控
spawn ssh root@172.18.29.127
expect {
 "*yes/no" { send "yes\r"; exp_continue }
 "*password:" { send "$password\r" }
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "cd $listnmon\r"
expect "*#"
send "./nmon -f -s 5 -c 800\r"


#登录推流1,执行推流命令
spawn ssh root@172.18.1.6
expect {
 "*yes/no" { send "yes\r"; exp_continue }
 "*password:" { send "$password\r" }
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "cd $listffmpeg\r"
expect "*#"
send "./$script\r"

#登录推流2,执行推流命令
spawn ssh root@172.18.1.9
expect {
 "*yes/no" { send "yes\r"; exp_continue }
 "*password:" { send "$password\r" }
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "cd $listffmpeg\r"
expect "*#"
send "./$script\r"

#登录推流2,执行推流命令
spawn ssh root@172.18.1.18
expect {
 "*yes/no" { send "yes\r"; exp_continue }
 "*password:" { send "$password\r" }
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "cd $listffmpeg\r"
expect "*#"
send "./$script\r"

#控制权移交
interact

killffmpeg.sh

#!/usr/bin/expect -f  

set password CDVcloud123 
#set listnmon /home/huangyl/nmon
set listffmpeg /home/huangyl
set timeout -1

#外网登录推流1,并杀死ffmpeg
spawn ssh root@101.201.237.216
expect {
 "*yes/no" { send "yes\r"; exp_continue }  
 "*password:" { send "$password\r" }  
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "ps -ef|grep ffmpeg\r"
expect "*#"
send "killall ffmpeg\r"
expect "*#"
send "killall ffmpeg\r"

#内网登录推流2,并杀死ffmpeg
spawn ssh root@172.18.1.9
expect {
 "*yes/no" { send "yes\r"; exp_continue }  
 "*password:" { send "$password\r" }  
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "ps -ef|grep ffmpeg\r"
expect "*#"
send "killall ffmpeg\r"
expect "*#"
send "killall ffmpeg\r"

#内网登录推流3,并杀死ffmpeg
spawn ssh root@172.18.1.18
expect {
 "*yes/no" { send "yes\r"; exp_continue }  
 "*password:" { send "$password\r" }  
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "ps -ef|grep ffmpeg\r"
expect "*#"
send "killall ffmpeg\r"
expect "*#"
send "killall ffmpeg\r"


#控制权移交
interact

playtest.sh

srs_bench压测

#!/usr/bin/expect -f  

set password CDVcloud123 
set listnmon /home/huangyl/nmon
set listbench /home/huangyl/srs-bench-master/objs
set usernum 1
set roadname https://zbfee.cdvcloud.com/551016/551016/7t3dmx/g96gwq/06ijj2.m3u8
#set script loop.sh
set timeout 30


#登录出口1,打开nmon监控
spawn ssh root@172.18.29.124
expect {
 "*yes/no" { send "yes\r"; exp_continue }
 "*password:" { send "$password\r" }
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "cd $listnmon\r"
expect "*#"
send "./nmon -f -s 5 -c 800\r"


##登录出口2,打开nmon监控
spawn ssh root@172.18.1.15
expect {
 "*yes/no" { send "yes\r"; exp_continue }
 "*password:" { send "$password\r" }
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "cd $listnmon\r"
expect "*#"
send "./nmon -f -s 5 -c 800\r"


#登录接口1,打开nmon监控
spawn ssh root@172.18.1.17
expect {
 "*yes/no" { send "yes\r"; exp_continue }
 "*password:" { send "$password\r" }
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "cd $listnmon\r"
expect "*#"
send "./nmon -f -s 5 -c 800\r"


##登录接口2,打开nmon监控
spawn ssh root@172.18.29.125
expect {
 "*yes/no" { send "yes\r"; exp_continue }
 "*password:" { send "$password\r" }
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "cd $listnmon\r"
expect "*#"
send "./nmon -f -s 5 -c 800\r"

##登录srs_bench,开始模拟直播观看
spawn ssh root@172.18.1.6
expect {
 "*yes/no" { send "yes\r"; exp_continue }
 "*password:" { send "$password\r" }
}
expect "*#"
send "ifconfig\r"
expect "eth0*"
send "cd $listbench\r"
expect "*#"
send "./sb_hls_load -c $usernum -r $roadname\r"


#控制权移交
interact

以上,深深的感受到自动化脚本语言的强大~

-------------本文结束感谢您的阅读-------------
坚持原创技术分享,您的支持将鼓励我继续创作!
0%