Expect

Expect is atool for automating interactive applications such as telnet, ftp, passwd, fsck,rlogin, tip, etc. Expect really makes this stuff trivial. Expect is also usefulfor testing these same applications. And by adding Tk, you can wrap interactiveapplications in X11 GUIs.

Expect canmake easy all sorts of tasks that are prohibitively difficult with anythingelse. You will find that Expect is an absolutely invaluable tool - using it,you will be able to automate tasks that you've never even thought of before -and you'll be able to do this automation quickly and easily.


[root@SRV1~]#yum install tcl -y

[root@SRV1~]# yum install expect -y

[root@SRV1 ~]# cat expect.exp

#Author:RELEARN

#Date:2013-08-02

#!/usr/bin/expect-f

setipaddr [lindex $argv 0]

setpasswd [lindex $argv 1]

settimeout 30

spawnssh root@$ipaddr

expect{

       "yes/no" {

        send "yes\r";exp_continue

       } "password:" {

        send "$passwd\r"

       } "Permission denied, please tryagain." {

        exit

       } "Connection refused" {

        exit

       } timeout {

        exit

       }

}


expect"]#"

send"/sbin/service crond stop\r"

expect"]#"

send"echo 'server' |passwd --stdin root\r"

expect"]#"

send"/usr/sbin/useradd RELEARN\r"

expect"]#"

send"/usr/bin/passwd RELEARN\r"

expect{

       "New password:" {

        send "srv6@1\r";exp_continue

       } "New UNIX password:" {

        send "srv6@1\r";exp_continue

       } "Retype new password:" {

        send "srv6@1\r"

       } "Retype new UNIX password:"{

         send "srv6@1\r"

       }

}

send"exit\r"

expecteof

exit


编写循环SHELL脚本

[root@SRV1 ~]# cat set.sh

passwd=server

fori in `cat iplist.txt`

do

       expect expect.exp $i $passwd

done


--------------------------------------------------------------------------------------------------------

eg:

#!/usr/bin/expect-f

 

#setipaddr 192.168.0.148

#setpasswd server

#setname root

#settimeout 10

 

setipaddr [lindex $argv 0]

setpasswd [lindex $argv 1]

setname root

settimeout 10

 

spawnssh $name@$ipaddr

expect{

        "*yes/no" { send"yes\r"; exp_continue }

        "*assword:" { send"$passwd\r" }

}

expect"#*"

send"ifconfig\r"

expect"#*"

send"exit\r"

expecteof

#interact

_________________________________________

注: interact (执行完后保持交互状态,把控制权交给控制台,此时可以手动操作了)

     exp_continue (继续执行下面的匹配) 


Shell与Expect程序结合:

#!/bin/bash

foripaddr in 192.168.0.148 192.168.0.129

do

        /usr/bin/expect << YONG

        set timeout 10

        spawn ssh root@$ipaddr

        expect {

                "*yes/no" { send"yes\r"; exp_continue }

                "*assword:" { send"server\r" }

        }

        expect "*#"

        send "ifconfig\r"

        expect "*#"

        send "exit\r"

        expect eof

YONG

done

#!/bin/bash

#批量修改主机密码

fori in {1..254}

do

        ip=192.168.0.$i

        /usr/bin/expect << YONG

        spawn ssh root@$ip

        expect {

                "*yes/no" { send"yes\r"; exp_continue }

                "*assword" {send"server\r"}

        }

        expect "#"

        send "passwd\r"

       

        expect "New password:"

        send "srvww123689@#\r"

       

        expect "Retype new password:"

        send "srvww123689@#\r"

        send "exit\r"

        expect eof     

 

YONG

Done


本文出自 “态度决定一切” 博客,转载请与作者联系!

你可能感兴趣的:(expect)