ulvis.paste.net

Paste Search Dynamic
Recent pastes
Host Lookup
  1. require 'tk'
  2. require 'socket'
  3.  
  4. # Set colors.
  5. BG = '#AAAAFF'
  6. TkOption.add('*background', BG)
  7. TkOption.add('*activeBackground', '#CCCCFF')
  8. TkOption.add('*foreground', '#884400')
  9.  
  10. # A label which does the needed lookup.
  11. class HostnameLabel < TkLabel
  12.  
  13.   # Look up host name in the assocated entry widget source, and display
  14.   # in ourselves.
  15.   def show
  16.     hn = @source.get.strip
  17.     if hn == ''
  18.       ip = ''
  19.     else
  20.       begin
  21.         ip = IPSocket.getaddress(hn)
  22.       rescue
  23.         ip = '[unknown]'
  24.       end
  25.     end
  26.     configure('text' => ip)
  27.   end
  28.  
  29.   # Create the widget, and bind the return key to run the lookup method (show).
  30.   def initialize(root, entry)
  31.     super(root, 'text' => '', 'width' => 15)
  32.     @source = entry
  33.     entry.bind('Return', proc { self.show })
  34.   end      
  35. end        
  36.  
  37. # Root window
  38. root = TkRoot.new('background' => BG) { title 'Host Conversion' }
  39.  
  40. # Title label
  41. tit = TkLabel.new {
  42.   text "Host Name Conversion"
  43.   relief 'groove'
  44.   grid('row' => 0, 'column' => 0, 'columnspan' => 2, 'sticky' => 'news')
  45. }
  46.  
  47. # Name entry.
  48. entr = TkEntry.new {
  49.   width 25
  50.   grid('row' => 1, 'column' => 0, 'columnspan' => 2, 'sticky' => 'news')
  51. }
  52. dislab = nil    # This needs to exist since we refer to it in the bind.
  53. entr.bind('Button-1', proc { |e|
  54.             entr.delete(0,'end')
  55.             dislab.configure('text' => '')
  56.           })
  57.  
  58. # Reporting label.
  59. dislab = HostnameLabel.new(root, entr)
  60. dislab.grid('row' => 2, 'column' => 0, 'sticky' => 'news')
  61.  
  62. # Go button.
  63. but = TkButton.new {
  64.   text "Find"
  65.   command { dislab.show }
  66.   grid('row' => 2, 'column' => 1, 'sticky' => 'news')
  67. }
  68.  
  69. Tk.mainloop
Parsed in 0.021 seconds