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
相關文章
相關標籤/搜索