Skip to content

expect grammar

WinChua edited this page Jul 2, 2020 · 4 revisions

命令行交互

  • 获取命令行参数信息:
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"
}

例子

  1. 登录所有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"

Clone this wiki locally