Beispiel #1
0
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library 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
#    Lesser General Public License for more details.
"""
Use the 'cu' command to connect to a serial port.

"""

import proctools
import expect

try:
    CU = proctools.which("cu")
except ValueError:
    raise ImportError, "cu program not found!"

CU_OPTIONS = '--parity=none --nostop'


class CUExpect(expect.Expect):
    def command(self, cmd):
        """Perform a cu command. Leaves cu in online mode."""
        self.send("\r~%s" % (cmd, ))

    def send_break(self):
        self.send("\r~#")

    def death_callback(self, deadcu):
Beispiel #2
0
#!/usr/bin/python
# vim:ts=4:sw=4
# $Id: //Branches/Stratatest/NextGen/Titan-1.0.0/external/pyNMS/lib/redir.py#1 $
# License: LGPL
"""
Simple Python wrapper for the 'redir' program.
Note that to use this to redirect privileged TCP ports as a regular user you
must make your 'redir' program suid to root. This is a bad security practice,
but is necessary for some scenarios. just be aware.

"""

import proctools

try:
	REDIR = proctools.which("redir")
except ValueError:
	raise ImportError, "'redir' program not found in PATH"

def redir(lport, cport, laddr=None, caddr=None, extraopts=None):
	"""redir(lport, cport, laddr=None, caddr=None, extraopts=None)
Redirect local port to client port, possible to another host is caddr is given.
Optionally bind to a specific IP if laddr is also given.
	"""
	opts = "--lport=%d --cport=%d" % (lport, cport)
	if laddr:
		opts += " --laddr=%s" % (laddr,)
	if caddr:
		opts += " --caddr=%s" % (caddr,)
	cmd = "%s %s %s" % (REDIR, opts, extraopts or "")
	proc = proctools.spawnpipe(cmd, merge=0)
Beispiel #3
0
#
#    This library 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
#    Lesser General Public License for more details.

"""
Use the 'cu' command to connect to a serial port.

"""

import proctools
import expect

try:
    CU = proctools.which("cu")
except ValueError:
    raise ImportError, "cu program not found!"

CU_OPTIONS = '--parity=none --nostop'

class CUExpect(expect.Expect):
    def command(self, cmd):
        """Perform a cu command. Leaves cu in online mode."""
        self.send("\r~%s" % (cmd,))
    
    def send_break(self):
        self.send("\r~#")

    def death_callback(self, deadcu):
        if self._log:
Beispiel #4
0
#!/usr/bin/python
# -*- coding: ascii -*-
# vim:ts=4:sw=4:softtabstop=0:smarttab
# License: LGPL
# Keith Dart <*****@*****.**>
# $Id: //Branches/Stratatest/NextGen/Titan-1.0.0/external/pyNMS/lib/sudo.py#1 $

"""
Run things as root (or another user) via sudo.

"""
import proctools

try:
	SUDO = proctools.which("sudo")
except ValueError:
	raise ImportError, "'sudo' program not found in PATH"

def sudo(command, user=None, password=None, extraopts=None, logfile=None):
	opts = "-S %s" % (IF(user, "-u %s" % (user,), ""),)
	cmd = "%s %s %s %s" % (SUDO, opts, extraopts or "", command)
	proc = proctools.spawnpipe(cmd, logfile=logfile, merge=0)
	if password:
		proc.readerr(9) # discard password prompt
		proc.write("%s\r" % (password,))
		proc.readerr(1) # discard newline
	return proc

def sudo_reset():
	proc = proctools.spawnpipe("%s -k" % (SUDO,), merge=0)
	proc.read()
Beispiel #5
0
# License: LGPL
# Keith Dart <*****@*****.**>
# $Id: //Branches/Stratatest/NextGen/Titan-1.0.0/external/pyNMS/lib/telnetproc.py#1 $

"""
Factory for getting a wrapper for the telnet program.

"""


import proctools
import expect


try:
	TELNET = proctools.which("telnet")
except ValueError:
	raise ImportError, "telnet program not found!"

TELNET_OPTIONS = '-8c'

class TelnetExpect(expect.Expect):
	def command(self, cmd):
		"""Perform telnet command. Leaves telnet in online mode."""
		self.send(chr(29)) # XXX settable escape char
		self.expect("telnet>")
		self.send(cmd+"\r")

	def send_break(self):
		self.command("send brk")
Beispiel #6
0
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library 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
#    Lesser General Public License for more details.
"""
Factory for getting a wrapper for the telnet program.

"""

import proctools
import expect

try:
    TELNET = proctools.which("telnet")
except ValueError:
    raise ImportError, "telnet program not found!"

TELNET_OPTIONS = '-8c'


class TelnetExpect(expect.Expect):
    def command(self, cmd):
        """Perform telnet command. Leaves telnet in online mode."""
        self.send(chr(29))  # XXX settable escape char
        self.expect("telnet>")
        self.send(cmd + "\r")

    def send_break(self):
        self.command("send brk")
Beispiel #7
0
#!/usr/bin/python
# vim:ts=4:sw=4
# $Id: //Branches/Stratatest/NextGen/Titan-1.0.0/external/pyNMS/lib/netcat.py#1 $
# License: LGPL

"""
Wrapper for the netcat program. Allows TCP port forwarding to stdio. If your
netcat (nc) is suid to root, you can forward privileged ports, such as SMTP.

"""

import sys
import proctools

NETCAT = proctools.which("nc")

TESTED_VERSIONS = ["[v1.10]"]


def get_netcat(host, port, callback=None, logfile=None, extraoptions=""):
	"""get_netcat(host, port, [prompt], [callback], [logfile], [extraoptions])
Returns a Netcat object (an Expect subclass) attached to a netcat client.

The logfile parameter should be a file-like object (has a 'write' method).
"""
	cmd = "%s %s %s %s" %(NETCAT, extraoptions, host, port)
	pm = proctools.get_procmanager()
	proc = pm.spawnpipe(cmd, callback=callback, logfile=logfile, merge=0)
	return proc

def netcat_server(port, callback=None, logfile=None, extraoptions=""):
Beispiel #8
0
#!/usr/bin/python
# -*- coding: ascii -*-
# vim:ts=4:sw=4
# License: LGPL
# Keith Dart <*****@*****.**>
# $Id: //Branches/Stratatest/NextGen/Titan-1.0.0/external/pyNMS/lib/alsaplayer.py#1 $

"""
Start and control an alsaplayer deamon instance.
"""

import proctools
import os

try:
	PLAYER = proctools.which("alsaplayer")
except ValueError:
	raise ImportError, "alsaplayer program not found!"

DEVICE = os.environ.get("ALSAPLAYERDEVICE", "default")

def alsaplayer(session=0, name="alsaplayer", device=DEVICE, extraopts="", logfile=None):
	opts = "-i daemon -q -n %s -s '%s' -d %s --nosave" % (session, name, device)
	CMD = "%s %s %s" % (PLAYER, opts, extraopts)
	aplayer = proctools.spawnpipe(CMD, logfile=logfile)
	return aplayer

get_alsaplayer = alsaplayer

def _test(argv):
	return alsaplayer()
Beispiel #9
0
Wrapper for the ssh program. 

Provides get_ssh, ssh_command, and scp functions.

"""

import sys, os

import proctools
import expect
EXACT = expect.EXACT
GLOB = expect.GLOB
REGEX = expect.REGEX

try:
	SSH = proctools.which("ssh")
	SCP = proctools.which("scp")
	KEYGEN = proctools.which("ssh-keygen")
	KEYSCAN = proctools.which("ssh-keyscan")
except ValueError:
	raise ImportError, "ssh program not found!"

class SSHRetry(RuntimeError):
	pass
add_exception(SSHRetry)

#				   |01234567890123456789
TESTED_VERSIONS = ["OpenSSH_3.4p1, SSH protocols 1.5/2.0, OpenSSL 0x0090605f",
				   "OpenSSH_3.5p1, SSH protocols 1.5/2.0, OpenSSL 0x0090701f",
				   "OpenSSH_3.6.1p2, SSH protocols 1.5/2.0, OpenSSL 0x0090701f",
				   "OpenSSH_3.8.1p1, OpenSSL 0.9.7d 17 Mar 2004",
Beispiel #10
0
#!/usr/bin/python

"""
Use nsupdate to update a DNS server
Added to the DNS module by Keith Dart <*****@*****.**>

"""

import sys, os
import proctools

NSUPDATE = proctools.which("nsupdate")

class NSUpdate(proctools.ProcessPipe):
	def __init__(self, logfile=None,  env=None, callback=None, merge=1):
		proctools.ProcessPipe.__init__(self, NSUPDATE, logfile,  env, callback, merge)
		self.zone=None
		self._ttl = 86400 # default ttl

	def _send_command(self, line):
		self.write(line)
		self.write("\n")
	
	def send(self):
		"""Sends the current message."""
		self.write("\n\n")

	def server(self, servername, port=53):
		"""server servername [ port ]
              Sends all dynamic update requests to the name server servername.
              When no server statement is provided, nsupdate will send updates
Beispiel #11
0
#!/usr/bin/python
# -*- coding: ascii -*-
# vim:ts=4:sw=4
# License: LGPL
# Keith Dart <*****@*****.**>
# $Id: //Branches/Stratatest/NextGen/Titan-1.0.0/external/pyNMS/lib/xselection.py#1 $

"""
Use wxcut and wxpaste to get and set the X selection.

"""

import proctools

try:
	WXCOPY = proctools.which("wxcopy")
	WXPASTE = proctools.which("wxpaste")
except ValueError:
	raise ImportError, "wxcopy or wxpaste program not found! Install WindowMaker for these."

WXCOPY_OPTIONS = '-clearselection'
WXPASTE_OPTIONS = ''


def wxcopy(text, callback=None, logfile=None, extraoptions="", async=False):
	pm = proctools.get_procmanager()
	command = "%s %s %s" % (WXCOPY, WXCOPY_OPTIONS, extraoptions)
	copy = pm.spawnpipe(command, logfile=logfile, async=async)
	copy.write(text)
	copy.close()
	return copy.wait()