4

use expect SSH Auto login

 2 years ago
source link: https://www.liurongxing.com/expect-ssh-auto-login.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

use expect SSH Auto login

作者: JavasBoy 分类: Archlinux, Linux, Script 发布时间: 2012-05-07 22:56 ė 19,943 views 6 没有评论

一、要安装这两个东东。
CentOS下

1
yum install tcl
tcl.x86_64 1:8.5.7-6.el6
2
yum install expect
expect.x86_64 0:5.44.1.15-2.el6

ArchLinux下

1
pacman -S tcl
2
pacman -S expect

自动登了ssh脚本

vim sshlogin.exp
#!/usr/bin/expect -f
#auto ssh login
set timeout 20
spawn ssh -qTfnN -D 7070 你的登录用户@服务器ip地址
expect "*password:"
send "you passwd\r"
interact

chmod +x sshlogin.exp
二、实现开机自动登录到tty4。
以下是在ArchLinux上操作的,其它系统也差不多。
1、安装自动登录tty的软件

pacman -S mingetty

mingetty-1.08-3-x86_64

vi /etc/inittab

找到关于tty4的,在Archlinux里是c4:2345:respawn:/sbin/agetty…这样的一行改成

c4:23:respawn:/sbin/mingetty --autologin javasboy tty4

把javasboy换成你要登录的用户,且能运行上面脚本的权限。
2、在javasboy用户下的~/.bashrc里面加上:

tty | grep -q tty4 && /home/javasboy/bin/sshlogin.exp

这样tty4就能开机运行这个命令了。

还可以通过crond定时检测7070端口和ssh进程是否运行来重新启动,这样就能实现断线自动重连的功能了。
在此感谢SUN的无私帮助。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK