New golang dev, curious about ssh.Client reconnect.

polaris · · 887 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello all,</p> <p>Though i&#39;ve been a developer for ~12 years now, i&#39;m new to Golang. I&#39;m currently writing a small tool to help automate some developer environment stuff for the company. </p> <p>I&#39;ve had no problems creating a new *ssh.Client, but I&#39;d like to automatically reconnect in the event of a network timeout. I know I can check against ssh.Conn to see if an initial connection has been made but that doesn&#39;t help with timeouts. I figure I can also check for errors on read/write then reconnect if needed, but I feel like there has to be an easier way to check if the connection still exists.</p> <p>A few of the examples I came across on StackOverflow suggested reading a single byte and if that didn&#39;t return an error, you&#39;d know the connection was still up. But that seemed odd to me as I wouldn&#39;t want to take a byte off the stream if something else ended up actually needing it.</p> <p>Thanks all!</p> <hr/>**评论:**<br/><br/>davecheney: <pre><p>I believe that the ssh protocol, and the go client support keep alive messages. Any error you get from the client should be considered fatal.</p></pre>pappogeomys: <pre><p>The ssh client does handle keepalive requests from the server, but doesn&#39;t implement the client side automatically. The library intended for users to send these requests manually using a global SendRequest.</p> <p>IIRC you send an empty global request with any request-name that the server doesn&#39;t recognize (and I <em>think</em> you wait on the reply, but please try it out.)</p> <pre><code>ok, _, err := conn.SendRequest(&#34;keepalive@golang.org&#34;, true, nil) </code></pre></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

887 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传