expect grammar - WinChua/blog GitHub Wiki
命令行交互
puts $argc // 输出命令行参数个数, 不包含程序文件名称
puts [lrange $argv 0 $argc] // 获取所有的参数值
for ip [lrange $argv 0 $argc] {
puts ${ip}
}
foreach number { 1 2 3 4 5 } {
puts ${number}
}
for {set i 0} {$i<4} {incr i} {
puts "$i"
}
if { "$test" == "apple" } {
puts "$test"
} else {
puts "not apple"
}
例子
- 登录所有ip
spawn tmux
sleep 0.2
foreach ip [lrange $argv 1 $argc] {
send \02
send %
sleep 0.05
}
sleep 0.5
foreach ip [lrange $argv 0 $argc] {
send \02
send o
send "es ${ip}\r"
sleep 0.05
}
send \02
send ":set synchronize-panes\r"
interact
#! /usr/bin/env expect
set aip [lindex $argv 0]
set alia [lindex $argv 1]
set passwd [ exec bash /data/home/yongcai/expect/qip.sh ${aip} ]
spawn ssh user_00@${aip}
expect {
"yes/no" {send "yes\r";exp_continue}
"assword" { send "${passwd}\r"}
}
expect {
"user_00" {send "IP=\$\(ifconfig eth1|awk '/inet / {print \$2}'\)\r"}
}
expect {
"user_00" {send "export PS1=\"\[\\u@\(${alia}:\$IP)\ \\W]$ \"\r" }
}
##expect {
##"user_00" {send "export PS1=\"[\\u@\\h\(\\[\\e[31m\\]$IP\\[\\e[m\\]) \\W]\\$ \"\r"}
##}
interact
#send "IP=\$(ifconfig eth1|awk '/inet / {print \$2}')\r"
#send "export PS1=\"[\u@\h(\[\e[31m\]$IP\[\e[m\]) \W]\\$ \"\r"