Linux 使用expect交互方式ssh登录

#!/usr/bin/expect

# Ubuntu 18.04使用expect登录
set user "用户名"
set host "ip"
set password "密码"
set timeout 3000
spawn ssh $user@$host
expect  {
  "*yes/no*" {send "yes\r";exp_continue}
  "*password*" {
    send "$password\r";
    expect {
      "*please try again*" {
        puts "密码错误";
        exit
      }
      "*${user}@*" {
        # 你要执行的操作
        send "sudo apt-get update\r"
        # 将控制返回给终端
        interact
      }
    }
  }
}
This entry was posted in Linux. Bookmark the permalink.

发表回复