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 @ 2021-01-20 15:07  ascertain  阅读(123)  评论(0编辑  收藏  举报