#!/usr/bin/env python """ $Id$ """ __license__ = """ Copyright (C) 2008 Diego Torres Milano 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA """ __name__ = 'android port redirection' __version__ = '0.3' __rev__ = '$Revision$' #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # redirections can be added to ~/android/redir.conf, one per line, # as an example, 'tcp:5000:6000' will route any packet sent to the host's # TCP port 5000 to TCP port 6000 of the emulated device # # default emulated device: host = 'localhost' port = 5554 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ import sys import os from optparse import OptionParser import pexpect usage = "usage: %prog [options] [{tcp|udp}:hostport:emuport] ..." parser = OptionParser(usage=usage, version="%prog " + __version__) parser.add_option("-v", "--verbose", dest="verbose", default=False, action="store_true", help="verbose output [default: %default]") parser.add_option("", "--host", dest="host", default=host, help="HOST where emulator is running [default: %default]") parser.add_option("-p", "--port", dest="port", default=port, help="emulator console PORT [default: %default]") parser.add_option("-n", "--dry-run", dest="dry_run", default=False, action="store_true", help="Don't execute the commands") (options, args) = parser.parse_args() redirs = [] cmd = 'telnet %s %d' % (options.host, options.port) try: for redir in open(os.path.expanduser("~") + "/.android/redir.conf"): redirs.append(redir[:-1]) except: pass for redir in args: redirs.append(redir) if options.verbose: print cmd if len(redirs) > 0: print "adding redirections:" for redir in redirs: print "\t" + redir if options.dry_run: sys.exit(0) if len(redirs) == 0: print >>sys.stderr, "ERROR: redirection list is empty." sys.exit(1) child = pexpect.spawn(cmd) child.expect("OK") for redir in redirs: child.sendline("redir add " + redir) r = child.expect(["OK", "KO: host port already active", "KO:.*"]) if r == 2: raise Exception(child.after) if options.verbose: child.sendline("redir list") child.expect("OK") print child.before child.sendline("quit")