#!/bin/bash

#Description: auto login the remote server #Author:majinxu #Version:1.0 #CreateTime:2018-3-012 18:34:41

user="zhangsan" passwd="123456"

host_list=( "st1.qa.bj2.yongche.com" "st2.qa.bj2.yongche.com" "st3.qa.bj2.yongche.com" )

#seletc the host select host in ${host_list[@]}; do break done

#execute the ssh action expect -c " spawn ssh $user@$host expect { "yes/no" { send "yes\n"; exp_continue} "password:" { send "$passwd\n" } } interact "

解释:

  1. expect -c : -c参数跟字符串
  2. spawn ssh $user@$host 连接服务器
  3. expect { "yes/no" { send "yes\n"; exp_continue} "password:" { send "$passwd\n" } } 输入并接收用户名密码 是 "yes/no" { send "yes\n"; exp_continue} "password:" { send "$passwd\n" } } 转义后的结果
  4. interact 执行完成后保持交互状态,把控制权交给控制台