Ubuntu中让SSH自动重连,简单的办法是安装autossh和expect。autossh负责自动重连,expect负责自动输入密码。
安装:
sudo apt-get install autossh
sudo apt-get install expect
sudo apt-get install expect
新建一个sh脚本,例如:/etc/autossh.sh,内容:
#!/bin/bash
HOST="xx.xxx.com"
USER="yourname"
PASS="yourpassword"
CMD=$@
VAR=$(expect -c "
spawn /usr/bin/autossh -M 2000 -N -v -D 127.0.0.1:7070 $USER@$HOST $CMD
match_max 100000
expect \"*?assword:*\"
send -- \"$PASS\r\"
send -- \"\r\"
expect eof
")
echo "==============="
echo "$VAR"
HOST="xx.xxx.com"
USER="yourname"
PASS="yourpassword"
CMD=$@
VAR=$(expect -c "
spawn /usr/bin/autossh -M 2000 -N -v -D 127.0.0.1:7070 $USER@$HOST $CMD
match_max 100000
expect \"*?assword:*\"
send -- \"$PASS\r\"
send -- \"\r\"
expect eof
")
echo "==============="
echo "$VAR"
运行/etc/autossh.sh,就自动登录了。