天天看点

采用TCL脚本实现自动登陆服务器并执行脚本

#!/usr/bin/expect

#设置变量

set passwd "xxx;123"

puts {you want to deploy war , please enter the pool name:}

#从键盘获取输入内容

flush stdout

set pool_name [gets stdin]

puts "you want to deploy $pool_name"

set host "127.0.0.1"

set run_script "/depot/deploy.sh"

#switch判断语句

switch $pool_name {

xxx {

set host 10.10.10.10

}

default {

puts "pool name is wrong! exit!"

exit 1

}

}

puts "you want to deploy war to $host"

#开启新进程登陆服务器

spawn ssh [email protected]$host

#expect期望进程输入内容,send 自动输入内容

expect {

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

"password:" { send "$passwd\r" }

}

send "/bin/sh $run_script\r"

#人机交互,停留在进程中,等待用户做其它的操作

interact

继续阅读