本文目录导读:

在Shell脚本中配置SSH登录超时,主要有以下几种方法:
使用SSH命令选项直接设置
ConnectTimeout(连接超时)
# 设置连接超时时间为10秒 ssh -o ConnectTimeout=10 user@host # 在脚本中使用 ssh -o ConnectTimeout=10 -o BatchMode=yes user@host "command"
ServerAliveInterval(心跳检测)
# 每60秒发送一次心跳包 ssh -o ServerAliveInterval=60 user@host
组合使用
ssh -o ConnectTimeout=10 -o ServerAliveInterval=60 -o ServerAliveCountMax=3 user@host
在脚本中动态配置
#!/bin/bash
# 定义超时参数
SSH_TIMEOUT=10
SSH_ALIVE_INTERVAL=60
SSH_ALIVE_COUNT=3
# SSH连接函数
ssh_connect() {
local host=$1
local user=$2
local command=$3
ssh -o "ConnectTimeout=$SSH_TIMEOUT" \
-o "ServerAliveInterval=$SSH_ALIVE_INTERVAL" \
-o "ServerAliveCountMax=$SSH_ALIVE_COUNT" \
-o "StrictHostKeyChecking=no" \
-o "BatchMode=yes" \
"${user}@${host}" "${command}"
}
# 使用示例
if ssh_connect "example.com" "user" "uptime"; then
echo "SSH连接成功"
else
echo "SSH连接超时或失败"
fi
使用expect处理交互式超时
#!/bin/bash
ssh_with_timeout() {
local host=$1
local password=$2
local timeout=$3
expect << EOF
set timeout $timeout
spawn ssh -o "ConnectTimeout=$timeout" user@$host
expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$password\r" }
timeout { puts "连接超时"; exit 1 }
}
expect {
"#" { send "exit\r" }
"\$" { send "exit\r" }
timeout { puts "会话超时"; exit 1 }
}
expect eof
EOF
}
# 使用
ssh_with_timeout "example.com" "password123" 10
配置SSH客户端配置文件
创建或修改 ~/.ssh/config:
# 全局设置
Host *
ConnectTimeout 10
ServerAliveInterval 60
ServerAliveCountMax 3
TCPKeepAlive yes
# 特定主机设置
Host myserver
HostName example.com
User myuser
Port 22
ConnectTimeout 5
ServerAliveInterval 30
完整的脚本示例
#!/bin/bash
# SSH连接管理脚本
SSH_CONFIG=~/.ssh/config
# 检查SSH配置
check_ssh_config() {
if [ ! -f "$SSH_CONFIG" ]; then
echo "创建SSH配置文件..."
cat > "$SSH_CONFIG" << EOF
Host *
ConnectTimeout 10
ServerAliveInterval 60
ServerAliveCountMax 3
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
LogLevel ERROR
EOF
chmod 600 "$SSH_CONFIG"
fi
}
# SSH连接函数
ssh_timed_connect() {
local host=$1
local user=${2:-$USER}
local command=$3
local timeout=${4:-10}
echo "连接到 $host (超时: ${timeout}s)..."
if ssh -o "ConnectTimeout=$timeout" \
-o "BatchMode=yes" \
-o "StrictHostKeyChecking=no" \
"${user}@${host}" "${command}" 2>/dev/null; then
echo "连接成功"
return 0
else
echo "连接失败或超时"
return 1
fi
}
# 批量测试连接
test_connections() {
local hosts=("$@")
local timeout=5
for host in "${hosts[@]}"; do
if ssh_timed_connect "$host" "root" "uptime" "$timeout"; then
echo "✓ $host 连接正常"
else
echo "✗ $host 连接失败"
fi
done
}
# 主函数
main() {
check_ssh_config
case "$1" in
connect)
shift
ssh_timed_connect "$@"
;;
test)
shift
test_connections "$@"
;;
*)
echo "用法: $0 {connect|test} [选项]"
echo " connect host [user] [command] [timeout]"
echo " test host1 host2 ..."
exit 1
;;
esac
}
main "$@"
环境变量方法
#!/bin/bash
# 设置环境变量
export SSH_TIMEOUT=10
export SSH_ALIVE_INTERVAL=60
# 自定义ssh函数
ssh() {
local timeout=${SSH_TIMEOUT:-10}
local alive=${SSH_ALIVE_INTERVAL:-60}
command ssh -o "ConnectTimeout=$timeout" \
-o "ServerAliveInterval=$alive" \
"$@"
}
# 使用
ssh user@host "command"
关键参数说明
| 参数 | 说明 | 默认值 |
|---|---|---|
| ConnectTimeout | 连接超时时间(秒) | 无限制 |
| ServerAliveInterval | 心跳间隔(秒) | 0(禁用) |
| ServerAliveCountMax | 心跳失败次数 | 3 |
| TCPKeepAlive | TCP保活 | no |
| BatchMode | 批处理模式 | no |
最佳实践
- 批处理脚本:始终使用
BatchMode=yes避免交互式提示 - 超时设置:根据网络环境调整,5-15 秒
- 重试机制:添加重试逻辑提高可靠性
- 错误处理:捕获超时错误并给出明确提示
- 日志记录:记录连接状态便于排错
选择合适的配置方法取决于你的具体需求和脚本的复杂度。