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
复制代码

 

posted @   ascertain  阅读(126)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示