自动登陆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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值