Changeset 1211 for bin/sshjour

Show
Ignore:
Timestamp:
10/14/09 20:55:42 (5 months ago)
Author:
vasi
Message:

Timeout when read hangs

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • bin/sshjour

    r1205 r1211  
    11#!/usr/bin/ruby 
     2 
    23host, port, alternative, altport = ARGV 
    34host = host + '.local' 
    45 
    5 key = IO.popen("ssh-keyscan #{host} 2>/dev/null") { |f| f.read } 
    66found, fport = alternative, altport 
    7 if $?.success? 
    8         known = IO.readlines(ENV['HOME'] + '/.ssh/known_hosts') 
    9         found, fport = host, port if known.include?(key) 
     7 
     8# Scan for a local host 
     9IO.popen('-') do |pipe| 
     10        if pipe # parent 
     11                fork { sleep 1; Process.kill 'ALRM', pipe.pid rescue nil } # timeout 
     12                key = pipe.read 
     13                 
     14                Process.wait(pipe.pid) 
     15                if $?.success? 
     16                        known = IO.readlines(ENV['HOME'] + '/.ssh/known_hosts') 
     17                        found, fport = host, port if known.include?(key) 
     18                end 
     19        else 
     20                $stderr.close 
     21                exec 'ssh-keyscan', host 
     22        end 
    1023end 
    1124 
     
    1528end 
    1629 
     30#puts found 
    1731exec('nc', found, fport || '22')