20.28 expect脚本远程登陆

expect脚本远程登陆

  • yum install -y expectlinux

  • 自动远程登陆vim

[root@aminglinux-02 sbin]# vim 1.expect

#! /usr/bin/expect
set host "192.168.133.132"
set passwd "123456"
spawn ssh root@$host   
expect {
"yes/no" { send "yes\r"; exp_continue}  //
"password:" { send "$passwd\r" }   
}
interact  //脚本结束
  • 在expect 定义变量,用set
  • 这个文件是就保证登陆信息的,清空的话,从新远程登陆ssh 会有提示 /root/.ssh/known_hosts
    exp_continue 表示继续 \r 表示换行 interact 继续停留在这台机器,不退出

加入执行权限bash

[root@aminglinux-02 sbin]# ./1.expect
-bash: ./1.expect: 权限不够
[root@aminglinux-02 sbin]# chmod a+x 1.expect
  • 成功登陆
[root@aminglinux-02 sbin]# ./1.expect
spawn ssh root@192.168.133.130
root@192.168.133.130's password:
Last failed login: Wed Sep 20 23:31:03 CST 2017 from 192.168.133.131 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Wed Sep 20 22:29:52 2017 from 192.168.133.1
[root@aminglinux-01 ~]# ^C
相关文章
相关标签/搜索