通过expect脚本在H3C设备批量执行命令 By HKL,

时间:2022-07-22
本文章向大家介绍通过expect脚本在H3C设备批量执行命令 By HKL, ,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.本文主要记录了在Linux系统中使用自动化测试工具expect通过ssh登陆H3C设置并批量执行相同命令

2.安装expect

以ubuntu为例

sudo apt install expect

3.编辑expect脚本

以ssh登陆H3C设置并配置AAA的服务为例

expect.sh

#!/usr/bin/expect
set timeout 5
set f [open ip.txt]   #ip.txt为同目录下配置交换机ip地址的文件
while {1} {
	set ip [gets $f]
	if {[eof $f]} {
		close $f
		break
	}

	spawn ssh -c aes128-cbc -oStrictHostKeyChecking=no YourUsername@$ip
	#expect "Please type 'yes' or 'no'"
	#send "yesr"
	expect "*password:"
	send "YourPasswordr" #password123为ssh的登陆密码
	expect "*>"
	send "sysr"
	expect "*]"
	send "radius scheme Schema_aaar"
	expect "*a]"
	send "primary authentication YourRadiusPrimaryIPr"
	expect "*a]"
	send "primary accounting YourRadiusPrimaryIPr"
	expect "*a]"
	send "secondary authentication YourRadiusSecondaryIPr"
	expect "*a]"
	send "secondary accounting YourRadiusSecondaryIPr"
	expect "*a]"
	send "quitr"
	expect "*]"
	send "quitr"
	expect "*>"
	send "saver"
	expect "*Y/N]"
	send "Yr"
	expect "*):"
	send "r"
	expect "*Y/N]"
	send "Yr"
	expect "*>"
	send "quitr"
}
interact

其中ip.txt以如下为例

10.1.1.1
10.1.1.2
10.1.1.3
10.1.1.4