简单需求来讲解shell+expect
先安装下expect,yum安装
yum install expect -y
#!/bin/bash
ip=‘192.168.80.20‘ #定义远程交换服务器的IP
password=‘[email protected]‘ #定义输入的密码
expect << EOF
spawn ssh [email protected]$ip ls #在expect中执行命令
set timeout -1 #定义超时时间,-1为永不超时
expect "*:" #如果输出*:
set timeout -1
send "$password\r" #自动输入password
expect eof #结束
EOF
时间: 2024-10-21 17:00:50