Package Peach :: Package Publishers :: Module icmp
[hide private]

Source Code for Module Peach.Publishers.icmp

  1   
  2  ''' 
  3  Default included ICMP publishers. 
  4   
  5  @author: Michael Eddington 
  6  @author: Adam Cecchetti 
  7  @version: $Id: icmp.py 795 2008-03-25 05:34:00Z meddingt $ 
  8  ''' 
  9   
 10  # 
 11  # Copyright (c) 2005-2008 Michael Eddington 
 12  # Copyright (c) 2004-2005 IOActive Inc. 
 13  # 
 14  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 15  # of this software and associated documentation files (the "Software"), to deal 
 16  # in the Software without restriction, including without limitation the rights  
 17  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 18  # copies of the Software, and to permit persons to whom the Software is  
 19  # furnished to do so, subject to the following conditions: 
 20  # 
 21  # The above copyright notice and this permission notice shall be included in     
 22  # all copies or substantial portions of the Software. 
 23  # 
 24  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 25  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 26  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 27  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 28  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 29  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 30  # SOFTWARE. 
 31  # 
 32   
 33  # Authors: 
 34  #   Michael Eddington (mike@phed.org) 
 35  #   Adam Cecchetti (adam@cecchetti.com) 
 36  # 
 37  # $Id: icmp.py 795 2008-03-25 05:34:00Z meddingt $ 
 38   
 39   
 40  import socket, time, sys 
 41  from Peach.publisher import Publisher 
 42   
43 -class Icmp(Publisher):
44 ''' 45 A simple Icmp publisher. 46 ''' 47 48 _host = None 49 _socket = None 50
51 - def __init__(self, host, timeout = 0.1):
52 ''' 53 @type host: string 54 @param host: Remote host 55 @type timeout: number 56 @param timeout: How long to wait for reponse 57 ''' 58 self._host = host 59 self._timeout = float(timeout)
60
61 - def stop(self):
62 ''' 63 Close connection if open. 64 ''' 65 self.close()
66
67 - def connect(self):
68 if self._socket != None: 69 # Close out old socket first 70 self._socket.close() 71 self._socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname('icmp')) 72 self._socket.connect((self._host,22))
73
74 - def close(self):
75 if self._socket != None: 76 self._socket.close() 77 self._socket = None
78
79 - def send(self, data):
80 ''' 81 Send data via sendall. 82 83 @type data: string 84 @param data: Data to send 85 ''' 86 self._socket.sendall(data)
87
88 - def receive(self, size = None):
89 ''' 90 Receive upto 10000 bytes of data. 91 92 @rtype: string 93 @return: received data. 94 ''' 95 96 if size != None: 97 return self._socket.recv(size) 98 99 else: 100 101 self._socket.setblocking(0) 102 103 timeout = self._timeout 104 beginTime = time.time() 105 ret = '' 106 107 try: 108 while True: 109 if len(ret) > 0 or time.time() - beginTime > timeout: 110 break 111 112 try: 113 ret += self._socket.recv(10000) 114 except socket.error, e: 115 if str(e).find('The socket operation could not complete without blocking') == -1: 116 raise 117 118 except socket.error, e: 119 print "Tcp::Receive(): Caught socket.error [%s]" % e 120 121 self._socket.setblocking(1) 122 return ret
123 124 # end 125