expect Linux
1.交互式命令
不会执行 ip a命令,直接退出
#!/bin/env expect
set timeout 3
spawn ssh -l root 172.16.30.102
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "edification0!\n" }
}
expect "#"
send "ip a\n"
超时3S后退出
#!/bin/env expect
set timeout 3
spawn ssh -l root 172.16.30.102
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "edification0!\n" }
}
expect eof
执行ip a命令后立即退出
#!/bin/env expect
set timeout 3
spawn ssh -l root 172.16.30.102
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "edification0!\n" }
}
expect "#"
send "ip a\n"
expect "#"
send "exit\n"
执行ip a命令后,超时退出
#!/bin/env expect
set timeout 3
spawn ssh -l root 172.16.30.102
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "edification0!\n" }
}
expect "#"
send "ip a\n"
expect eof
2.非交互式命令
#!/bin/env expect
set timeout 5
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub -f sack1
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "edification0!\n" }
}
expect "#"
send "ip a\n"
expect eof
spawn没有打开,ssh-copy-id执行完,进程退出,spawn随之退出
spawn bash 打开新spawn进程,用来执行 ip a命令,成功执行
#!/bin/env expect
set timeout 5
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub -f sack1
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "edification0!\n" }
}
expect eof
随报错,但是公钥推送成功
ssh-copy-id 执行接受后,spawn进程结束,使用expect eof报错,交互式登录的时候使用expect eof用来退出最后的交互
嵌入bash脚本执行
#!/bin/bash
password=edification0!
/bin/env expect <<-delimiter
set timeout 5
spawn ssh -l root sack1 df -hT
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
delimiter
缺少expect eof,不能正确执行
#!/bin/bash
password=edification0!
/bin/env expect <<-delimiter
set timeout 5
spawn ssh -l root sack1 df -hT
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
expect eof
delimiter
上述才能正确执行
#!/bin/bash
password=edification0!
/bin/env expect <<-delimiter
set timeout 5
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub -f root@sack1
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
expect eof
delimiter
缺少expect eof同样不能正确执行
#!/bin/bash
password=edification0!
/bin/env expect <<-delimiter
set timeout 5
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub -f root@sack1
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
delimiter
echo $?
测试发现当后面有命令是需把expect eof去掉
expect 直接结果恒定为0
#!/bin/bash
/bin/env expect <<-delimiter
set timeout 3
spawn su - root
expect {
"password" { send "sack\n";exp_continue }
"*" { send "ip a\n" }
}
send "exit\n"
expect eof
delimiter
echo $?
是否使用 send "exit\n" & expect eof 只能多调试了
expect 不能单使用 "*" ,会造成循环
Example
#!/bin/bash
echo -e "\e[7m$BASHPID\e[0m"
/bin/env expect <<-delimiter
set timeout 3
spawn su - root
expect {
"password" { send "sack\n";exp_continue }
"#" { send "echo $BASHPID\n" }
}
send "exit\n"
expect eof
delimiter
#!/bin/bash
/bin/env expect <<-delimiter
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub -f sack1
set timeout 3
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "edification0!\n" }
}
#expect eof 加入此行报错
delimiter
echo $?
#!/bin/bash
/bin/env expect <<-end
set timeout 5
spawn ssh root@sack1
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "edification0!\n" }
}
expect "#"
send "ip a\n"
send "exit\n"
expect eof
end
ip a
#!/bin/env bash
source /etc/rc.d/init.d/functions
password=edification0!
>ip.txt
rpm -q expect >& /dev/null
if [ $? -ne 0 ];then
yum install expect -y
if [ $? -ne 0];then
echo -e '\e[7mInstall expect Failed\e[0m'
exit 2
else
echo -e '\e[7mInstall expect Success\e[0m'
fi
fi
if [ ! -f ~/.ssh/id_rsa ];then
ssh-keygen -P '' -f ~/.ssh/id_rsa -t rsa -C 'expect automatic'
if [ $? -eq 0 ];then
echo -e '\e[5mssh-keygen Success\e[0m'
else
echo -e '\e[5mssh-keygen Failed\e[0m'
exit 5
fi
fi
function coercion(){
for i in {0..255};do
for j in {0..255};do
{
ip=172.16.$i.$j
ping -c2 -w3 $ip &> /dev/null
code=$?
if [ $code -eq 0 ];then
action "$ip" /bin/true
echo $ip >> ip.txt
else
action "$ip" /bin/false
fi
{
if [ $code -eq 0 ];then
/bin/env expect <<-delimiter
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub -f $ip
set timeout 3
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
expect eof
delimiter
fi
}
} &
done
done
}
coercion &> /dev/null
wait
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律