因为工作环境就是在linux下,需要登录到服务器上,而当键盘、鼠标闲置时间过长的时候往往会被服务器给自动断开,前面做的一些事情就被打断了,这样就很麻烦,所以得想办法来防止被服务器给踢出来。

一种方法是修改服务器段,在/etc/ssh/sshd_config里配置ClientAliveCountMax,以分为单位,默认是3分钟,即闲置3分钟就会被自动断开,可以修改时间长一点,比如30分钟:
ClientAliveCountMax=30

另一种方法比较通用,原理就是模拟键盘动作,在闲置时间之内模拟地给个键盘响应,如下:
工具:expect
代码如下:

#!/usr/bin/expect








set

timeout 60




spawn ssh

user@

host   interact {




timeout 300

{

send "x20"

}







}

保存为文本文件xxx,然后用expect执行:
expect xxx

接着按提示输入密码就可以了,这样每隔300秒就会自动打一个空格(x20),具体的时间间隔可以根据具体情况设置。
用这个还能解决乱码问题,比如服务器是用GB18030编码的,那只要把
spawn ssh user@host

换成
spawn luit -encoding GB18030 ssh user@host

其他的还有包括自动登录什么的,感兴趣的可以自己研究一下。

另外,如果是在windows下,则secureCRT就自带了反空闲的功能,打开选项->会话选项->终端,如图:
secureCRT
在发送字串的地方打个空格就可以了。

 

 

=========

zz  ssh自动断开解决办法

 

用putty/SecureCRT连续3分钟左右没有输入, 就自动断开, 然后必须重新登陆, 很麻烦.

在网上查了很多资料, 发现原因有多种, 环境变量TMOUT引起,ClientAliveCountMax和ClientAliveInterval设置问题或者甚至是防火墙的设置问题. 所以可以这么尝试:

1, echo $TMOUT

如果显示空白,表示没有设置, 等于使用默认值0, 一般情况下应该是不超时. 如果大于0, 可以在如/etc/profile之类文件中设置它为0.

Definition: TMOUT: If set to a value greater than zero, the value is
interpreted as the number of seconds to wait for input after issuing
the primary prompt. Bash terminates after waiting for that number of
seconds if input does not arrive.

2. ClientAliveInterval 60

在/etc/ssh/sshd_config中增加ClientAliveInterval 60,
ClientAliveInterval指定了服务器端向客户端请求消息的时间间隔, 默认是0, 不发送.而ClientAliveInterval
60表示每分钟发送一次, 然后客户端响应, 这样就保持长连接了.这里比较怪的地方是:不是客户端主动发起保持连接的请求(如FTerm,
CTerm等),而是需要服务器先主动.

另外,至于ClientAliveCountMax, 使用默认值3即可.ClientAliveCountMax表示服务器发出请求后客户端没有响应的次数达到一定值, 就自动断开. 正常情况下, 客户端不会不响应.

ClientAliveCountMax

Sets the number of client alive messages (see below) which may be

sent without sshd(8) receiving any messages back from the client.

If this threshold is reached while client alive messages are

being sent, sshd will disconnect the client, terminating the ses-

sion. It is important to note that the use of client alive mes-

sages is very different from TCPKeepAlive (below). The client

alive messages are sent through the encrypted channel and there-

fore will not be spoofable. The TCP keepalive option enabled by

TCPKeepAlive is spoofable. The client alive mechanism is valu-

able when the client or server depend on knowing when a connec-

tion has become inactive.

The default value is 3. If ClientAliveInterval (see below) is

set to 15, and ClientAliveCountMax is left at the default, unre-

sponsive SSH clients will be disconnected after approximately 45

seconds. This option applies to protocol version 2 only.

ClientAliveInterval

Sets a timeout interval in seconds after which if no data has

been received from the client, sshd(8) will send a message

through the encrypted channel to request a response from the

client. The default is 0, indicating that these messages will

not be sent to the client. This option applies to protocol ver-

sion 2 only.

3. 启用putty keepalive

putty -> Connection -> Seconds between keepalives ( 0 to turn off ), 默认为0, 改为60.

 

4.SecureCRT设置反空闲

option->session option->Terminal->Anti-idle->Send protocol NO-OP every__seconds

每隔几秒发送空字符串 保持链接不断开

 

=========

 

ssh出于安全考虑,设置了闲置后自动断开功能,这给长时间进行远程操作及利用ssh穿qiang的同学带来了极大的不便,参考了网上的N种方法,实现如下
#!/usr/bin/expect





# Auto login ssh




 


set

user "username"

;


set

passwd

"password"

;


set

host "ip"

;


set

id1 1

;


 


while

(

1

)

{




set

isconnected 0

;


spawn /

usr/

bin/

ssh

-D7070

$user

@

$host

;


set

id1 $spawn_id




match_max 100000

;


set

timeout 60

;


expect {




"password:"

{




send "$passwd

/r

"


;


expect "jailshell"

{




set

isconnected 1

;


puts "connected to $host

succeed"


;


puts $spawn_id

;


}




}




 


"closed"

{




set

isconnected 0

;


#puts "connected to $host fail"




 


}




 


}




 


if

{

$isconnected

== 0

}

{




close;


puts "SSH server connect fail,retrying..."

;


continue

;


 


}




 


while

(

1

)

{




set

isconnected 0

;


interact {




timeout 22

{




set

timeout 10

;


send "echo im active/r

"


;


expect "*im active*"

{

set

isconnected 1

}




if

{

$isconnected

== 1

}

{




puts "on line/r

"


;


continue

;


}

else

{




puts "off line/r

"





break

;


}




}




close;


puts "SSH server connect fails,retrying..."

;


}





===========







编辑或新建~/.ssh/config



引用:
Host *
ServerAliveInterval 60
ServerAliveCountMax 10

上面两个数字根据自己的需要调节。第一个表示多长时间向服务器发送一次数据,单位是秒。第二个表示多少次没有收到服务器回复则断开链接。

相关: