自动登陆ssh脚本

[root@localhost Desktop]# vim /mnt/ssh
[root@localhost Desktop]# cd /mnt/
[root@localhost mnt]# ls
ssh
[root@localhost mnt]# vim answer.exp
[root@localhost mnt]# cat /mnt/ssh

#!/bin/bash                       #指定运行环境
read -p "please user" -s USER     
read -p "please IP" -s IP
ssh ${USER}@${IP}                 #指定运行格式

[root@localhost mnt]# cat /mnt/answer.exp

#!/usr/bin/expect
set USER [ lindex $argv 0 ]
set PASSWD [ lindex $argv 1 ]
set IP [ lindex $argv 2 ]

spawn /mnt/ssh
expect {
    "please user" { send "$USER\r"; exp_continue }
    "yes" { send "yes"\r; exp_continue }
    "please IP" { send "$IP\r"; exp_continue }
    "password" { send "$PASSWD\r"; exp_continue }

[root@localhost mnt]# yum install expect.x86_64 -y

expect:自动应答环境

[root@localhost mnt]# ll
total 8
-rw-r–r–. 1 root root 302 Mar 9 04:09 answer.exp
-rw-r–r–. 1 root root 87 Mar 9 03:58 ssh
[root@localhost mnt]# chmod +x * #给与执行权限 x
[root@localhost mnt]# ll

total 8
-rwxr-xr-x. 1 root root 302 Mar 9 04:09 answer.exp
-rwxr-xr-x. 1 root root 87 Mar 9 03:58 ssh

[root@localhost mnt]# /mnt/answer.exp root redhat 172.25.254.66 #自动登陆

>spawn /mnt/ssh
please userplease IPThe authenticity of host '172.25.254.66 (172.25.254.66)' can't be established.
ECDSA key fingerprint is 75:ff:e2:ad:a9:b7:0f:4e:2c:1e:ce:e8:fb:c7:07:28.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.254.66' (ECDSA) to the list of known hosts.
root@172.25.254.66's password: 
Last login: Thu Mar  9 17:26:10 2017

你可能感兴趣的:(自动登陆ssh脚本)