Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

28.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 發送指令

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

例 28.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
			
			

28.6.2. 模擬登錄 ssh

例 28.2. example for expect

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

例 28.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
			

例 28.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
			

28.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"}
	}
}
		
		

28.6.4. openssl 例子

		
expect -c  '
spawn  openssl req -newkey rsa:4096 -nodes -sha256 -keyout domain.key -x509 -days 3650 -out domain.crt
expect {
    "Country Name" { send "CN\r"; exp_continue}
    "State or Province Name" { send "Guangdong\r" ; exp_continue}
    "Locality Name" { send "Shenzhen\r"; exp_continue}
    "Organization Name" { send "netkiller\r"; exp_continue}
    "Organizational Unit Name" { send "Neo\r"; exp_continue}
    "Common Name" { send "netkiller.cn\r" ; exp_continue}
    "Email Address" { send "netkiller@msn.com\r" ; exp_continue}
    eof { exit }
}'