# 下载 yum 源并安装sshpass
yum install -y wget ; wget http://mirrors.aliyun.com/repo/epel-7.repo ; mv epel-7.repo /etc/yum.repos.d/ ; yum list ; yum install sshpass
yum install -y expect
# interact [ˌɪn(t)ərˈækt] 互动
#---------- 使用expect交互工具登录ssh ---------#
#!/bin/bash
yum install -y expect
# 安装交互工具包
set timeout 30
# 设置匹配字符的等待时间
/usr/bin/expect << EOF
# 执行交互程序,通过EOF 打包给程序
spawn ssh root@hc2.ssh.gs
# 进行远程连接服务器
expect "password:"
# 匹配密码提示
send "aA20200930\r"
# 输入密码并换行
expect "#"
# 登录成功后匹配符号 #
send "echo '登录成功' \r"
# 执行打印命令并换行
expect eof
# 匹配结束
exit
# 退出
EOF
# 总打包结束
#!/usr/bin/expect -f
set port 22
set user root
set host eisc.cn
set password xxxxxx
set timeout -1
spawn ssh $user@$host
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*assword:" { send "$password\r" }
}
expect "*#*"
send "ip a > /root/ip.txt \r"
send "exit\r"
interact
spawn scp $host:/root/ip.txt /root
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*assword:" { send "$password\r" }
}
expect eof
获取登陆成功的主句IP地址
cat /root/ip.txt | grep inet | grep broadcast | awk -F" " '{print $2}' >> ip.txt ; cat /dev/null > ip.txt
Powered by ddoss.cn 12.0
©2015 - 2025 ddoss
渝公网安备50011302222260号
渝ICP备2024035333号
【实验平台安全承诺书】
小绿叶技术社区,优化网络中,点击查看配置信息
主机监控系统: 安全防火墙已开启检查cc攻击-下载文件完成后等待10s 恢复访问,检查连接数低于峰值恢复访问
您的IP:10.8.227.66,2025-12-16 07:19:07,Processed in 0.01812 second(s).