工作技巧我用 LinuxLinux学习之路

SSH自动输入密码

2017-02-24  本文已影响356人  guozhao

概述

使用SSH登录服务器,通常分为两步:

  • #ssh username@hostname

今天要讨论的如何让SSH自动输入密码。


开始

-设置主机短名称

#vim ~/.ssh/conf ,其内容如下:
- Host host1 HostName IP地址 User 帐号 ServerAliveInterval 300 -

--通过短名称登录

#ssh host1

此时还会提示输入密码。


-自动输入密码

利用expect完成自动输入。
注:Expect是Linux实现与系统自动交互工具

--编写expect脚本

# sudo vim login ,内容如下:
- 1 #!/usr/bin/expect 2 3 set channel [lindex $argv 0] 4 5 6 if { "$channel"=="host1" || "$channel" == "host2" || "$channel" == "host3" } { 7 set passwd "1234" 8 } else { 9 set passwd "6789" 10 } 11 12 spawn ssh $channel 13 14 15 expect { 16 password: { 17 send "$passwd\r" 18 exp_continue 19 } 20 PASSCODE: { 21 send "580928" 22 interact 23 } 24 timeout { 25 send_user "connection timed out\n" 26 exit 27 } 28 eof { 29 send_user "connection to host failed: $expect_out(buffer)" 30 exit 31 } 32 } -

--登录服务器

#./login host1

应该可以成功登录了^^
一条命令就可以登录系统了。


上一篇 下一篇

猜你喜欢

热点阅读