Home | Mirror | Search

6. expect

$ sudo apt-get install expect
	

命令含義

#!/usr/bin/expect
set timeout 30
spawn ssh root@192.168.1.1
expect "password:"
send "mypassword\r"
interact
	

set 設置變數

spawn 執行命令

expect 檢測點

send 發送指令

6.1. 模擬登錄 telnet 獲取Cisco配置

例 31.1. example for expect

cat tech-support.exp
#!/usr/bin/expect
set timeout 30
spawn telnet 172.16.1.24
expect "Password: "
send "chen\r"
expect "*>"
send "enable\r"
expect "Password: "
send "chen\r"
expect "*#"
send "sh tech-support\r"
send "!\r"
expect "*-Switch#"
send "exit\r"
expect eof
exit
			

3層設備

cisco.exp
#!/usr/bin/expect
set ip [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
log_file $ip.log
spawn telnet $ip
expect "Username:"
send "$username\r"
expect "Password:"
send "$password\r"
expect "#"
send "show running-config\r"
send "exit\r"
expect eof
			

2層設備

			
$ cat config.exp 
#!/usr/bin/expect
set timeout 30
set host [lindex $argv 0]
set password [lindex $argv 1]
set done 0

log_file $host.log
spawn telnet $host
expect "Password:"
send "$password\r"
expect "*>"
send "enable\r"
expect "Password: "
send "$password\r"
expect "*#"
send "show running-config\r"

while {$done == 0} {
expect {
" --More--" { send -- " " }
"*#" { set done 1 }
eof { set done 1 }
}
}

send "\r"
expect "*#"
send "exit\r"
expect eof
exit


$ cat loop.sh 
#! /bin/sh
while read sw
do
	./config.exp $sw
done <<EOF
172.16.0.240 chen
172.16.0.241 chen
172.16.0.242 chen
172.16.0.243 chen
172.16.0.245 chen
172.16.0.246 chen
EOF
	
$ chmod +x config.exp loop.sh
$ ./loop.sh
			
			

6.2. 模擬登錄 ssh

例 31.2. example for expect

#!/usr/bin/expect
set timeout 30
spawn ssh root@192.168.1.1
expect "password:"
send "mypassword\r"
interact
			

例 31.3. example 1

#!/usr/bin/expect
  set password 1234  #密碼
  #download
  spawn scp /www/* root@172.16.1.2:/www/
  set timeout 300
  expect "172.16.1.2's password:"
  set timeout 3000
  #exec sleep 1
  send "$password\r"
  set timeout 300
  send "exit\r"
  #expect eof
  interact
  spawn scp /www/* root@172.16.1.3:/www/
  set timeout 300
  expect "root@172.16.1.3's password:"
  set timeout 3000
  #exec sleep 1
  send "$password\r"
  set timeout 300
  send "exit\r"
  interact
			

例 31.4. *.exp

$ expect autossh.exp neo@192.168.3.10 chen "ls /"
			

autossh.exp

#!/usr/bin/expect -fset ipaddress [lindex $argv 0]
set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
set command [lindex $argv 2]
set timeout 30
spawn ssh $ipaddress
expect {
    "yes/no" { send "yes\r";exp_continue }
    "password:" { send "$passwd\r" }
}
expect ""

send "$command \r"

send "exit\r"

expect eof
exit
			

批量執行

password.lst
192.168.0.1 passwd
192.168.0.2 passwd
192.168.0.3 passwd
			
#!/bin/bash

cat password.lst | while read line
do
	host=$(echo $line|awk '{print $1}')
	passwd=$(echo $line|awk '{print $2}')
	expect autossh.exp $host $passwd
	sleep 2
done
			

6.3. SCP

#! /usr/bin/expect -f
spawn scp 1 neo@192.168.0.1:
expect "*password:"
send "your password\r"

expect eof
		
		
#!/bin/expect

spawn scp x.x.x.x

for {} {1} {} {
	expect {
		"password:" {
	        send "YourPassWord"
	    }
	}
}
		
		
		
spawn scp 1 neo@172.16.0.1:
for { set i 1 } {$i<5} {incr i} {
	expect {
		"*password:" {send "koven\r"}
		"*(yes/no)*" {send "yes\r"}
	}
}
		
		
comments powered by Disqus