#!/usr/pkg/bin/python """send a udp or tcp packet to specified host and port not nearly as useful as netcat but hopefully easier to use""" Copyright = """ fakebind -- return chosen IP for every name lookup Copyright (C) 2004 John Comeau This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. """ errormessage = "Not all needed libraries found, upgrade or check path: " try: True # not defined in older Python releases except: True, False = 1, 0 try: import sys, os, types, re, pwd sys.path.append(os.path.join(pwd.getpwuid(os.geteuid())[5], 'lib', 'python')) errormessage = errormessage + repr(sys.path) from com.jcomeau import gpl, jclicense except: try: sys.stderr.write("%s\n" % errormessage) except: print errormessage raise # get name this program was called as self = os.path.split(sys.argv[0])[1] command = os.path.splitext(self)[0] # chop any suffix (extension) # now get name we gave it when we wrote it originalself = re.compile('[0-9A-Za-z]+').search(Copyright).group() # globals and routines that should be in every program # (yes, you could import them, but there are problems in that approach too) def DebugPrint(*whatever): return False # defined instead by pytest module, use that for debugging # other globals, specific to this program from socket import * def send(*args): """send a packet give usage message if host and port not specified """ try: protocol, host, portstring, message = args except: sys.stderr.write('Usage: %s PROTOCOL HOST PORT "MESSAGE"\n' % command) sys.exit(1) port = int(portstring) socket_type = SOCK_STREAM if protocol == 'udp': socket_type = SOCK_DGRAM sender = socket(AF_INET, socket_type) sender.bind(("0.0.0.0", 0)) # note double parens, must be tuple sender.connect((host, port)) if os.path.exists(message): message = open(message).read() sender.send(message) if __name__ == '__main__': # if this program was imported by another, the above test will fail, # and this following code won't be used... function = command; args = sys.argv[1:] # default case if command == originalself: try: if len(args) and eval('type(%s) == types.FunctionType' % args[0]): function = sys.argv[1]; args = sys.argv[2:] except: pass eval('%s%s' % (function, repr(tuple(args)))) else: # if you want something to be done on import, do it here; otherwise pass pass