summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorSteven Myint <git@stevenmyint.com>2013-10-04 10:23:57 -0700
committerSteven Myint <git@stevenmyint.com>2013-10-04 10:23:57 -0700
commit178adc68ec0e15fd05cfd2441ee925319028b50f (patch)
tree1c8a97fbde948ca9ea6cc5de90803ba6b8bb062c /examples
parenta12cbd428a1efa28092448962c48056f7d914e57 (diff)
downloadpexpect-git-178adc68ec0e15fd05cfd2441ee925319028b50f.tar.gz
Add some Python 3 support
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/ftp.py6
-rwxr-xr-xexamples/hive.py7
-rwxr-xr-xexamples/monitor.py9
-rwxr-xr-xexamples/passmass.py7
-rwxr-xr-xexamples/rippy.py7
-rwxr-xr-xexamples/ssh_tunnel.py7
-rwxr-xr-xexamples/sshls.py7
-rwxr-xr-xexamples/topip.py33
-rwxr-xr-xexamples/uptime.py6
9 files changed, 67 insertions, 22 deletions
diff --git a/examples/ftp.py b/examples/ftp.py
index 1a4d192..678d032 100755
--- a/examples/ftp.py
+++ b/examples/ftp.py
@@ -24,14 +24,14 @@ PEXPECT LICENSE
'''
-from __future__ import print_function
-
from __future__ import absolute_import
+from __future__ import print_function
+from __future__ import unicode_literals
import pexpect
import sys
-child = pexpect.spawn('ftp ftp.openbsd.org')
+child = pexpect.spawnu('ftp ftp.openbsd.org')
child.expect('(?i)name .*: ')
child.sendline('anonymous')
child.expect('(?i)password')
diff --git a/examples/hive.py b/examples/hive.py
index ed881ab..8b0d3db 100755
--- a/examples/hive.py
+++ b/examples/hive.py
@@ -103,6 +103,13 @@ except ImportError:
sys.stderr.write(" aptitude -y install python-pexpect\n")
exit(1)
+
+try:
+ raw_input
+except NameError:
+ raw_input = input
+
+
histfile = os.path.join(os.environ["HOME"], ".hive_history")
try:
readline.read_history_file(histfile)
diff --git a/examples/monitor.py b/examples/monitor.py
index a44ebfd..1504bbd 100755
--- a/examples/monitor.py
+++ b/examples/monitor.py
@@ -45,10 +45,17 @@ from __future__ import print_function
from __future__ import absolute_import
-import os, sys, time, re, getopt, getpass
+import os, sys, re, getopt, getpass
import traceback
import pexpect
+
+try:
+ raw_input
+except NameError:
+ raw_input = input
+
+
#
# Some constants.
#
diff --git a/examples/passmass.py b/examples/passmass.py
index f5bd04b..dc941bc 100755
--- a/examples/passmass.py
+++ b/examples/passmass.py
@@ -29,6 +29,13 @@ from __future__ import absolute_import
import pexpect
import sys, getpass
+
+try:
+ raw_input
+except NameError:
+ raw_input = input
+
+
USAGE = '''passmass host1 host2 host3 . . .'''
COMMAND_PROMPT = '[$#] '
TERMINAL_PROMPT = r'Terminal type\?'
diff --git a/examples/rippy.py b/examples/rippy.py
index 07c2c6d..c0494cb 100755
--- a/examples/rippy.py
+++ b/examples/rippy.py
@@ -71,6 +71,13 @@ from __future__ import absolute_import
import sys, os, re, math, stat, getopt, traceback, types, time
import pexpect
+
+try:
+ raw_input
+except NameError:
+ raw_input = input
+
+
__version__ = '1.2'
__revision__ = '$Revision: 11 $'
__all__ = ['main', __version__, __revision__]
diff --git a/examples/ssh_tunnel.py b/examples/ssh_tunnel.py
index 03a2e85..d761911 100755
--- a/examples/ssh_tunnel.py
+++ b/examples/ssh_tunnel.py
@@ -40,6 +40,13 @@ import pexpect
import getpass
import time
+
+try:
+ raw_input
+except NameError:
+ raw_input = input
+
+
# SMTP:25 IMAP4:143 POP3:110
tunnel_command = 'ssh -C -N -f -L 25:127.0.0.1:25 -L 143:127.0.0.1:143 -L 110:127.0.0.1:110 %(user)@%(host)'
host = raw_input('Hostname: ')
diff --git a/examples/sshls.py b/examples/sshls.py
index 4772f8e..9f751f3 100755
--- a/examples/sshls.py
+++ b/examples/sshls.py
@@ -29,6 +29,13 @@ from __future__ import absolute_import
import pexpect
import getpass, os
+
+try:
+ raw_input
+except NameError:
+ raw_input = input
+
+
def ssh_command (user, host, password, command):
'''This runs a command on the remote host. This could also be done with the
diff --git a/examples/topip.py b/examples/topip.py
index cd9fbc9..caed606 100755
--- a/examples/topip.py
+++ b/examples/topip.py
@@ -64,9 +64,9 @@ PEXPECT LICENSE
'''
-from __future__ import print_function
-
from __future__ import absolute_import
+from __future__ import print_function
+from __future__ import unicode_literals
# See http://pexpect.sourceforge.net/
import pexpect
@@ -74,7 +74,6 @@ import pxssh
import os
import sys
import time
-import re
import getopt
import pickle
import getpass
@@ -82,6 +81,13 @@ import smtplib
import traceback
from pprint import pprint
+
+try:
+ raw_input
+except NameError:
+ raw_input = input
+
+
TOPIP_LOG_FILE = '/var/log/topip.log'
TOPIP_LAST_RUN_STATS = '/var/run/topip.last'
@@ -109,10 +115,9 @@ def stats(r):
total = sum(r)
avg = float(total)/float(len(r))
sdsq = sum([(i-avg)**2 for i in r])
- s = list(r)
- s.sort()
- return dict(zip(['med', 'avg', 'stddev', 'min', 'max'],
- (s[len(s)//2], avg, (sdsq/len(r))**.5, min(r), max(r))))
+ s = sorted(list(r))
+ return dict(list(zip(['med', 'avg', 'stddev', 'min', 'max'],
+ (s[len(s)//2], avg, (sdsq/len(r))**.5, min(r), max(r)))))
def send_alert (message, subject, addr_from, addr_to, smtp_server='localhost'):
@@ -208,7 +213,7 @@ def main():
# run netstat (either locally or via SSH).
if use_localhost:
- p = pexpect.spawn('netstat -n -t')
+ p = pexpect.spawnu('netstat -n -t')
PROMPT = pexpect.TIMEOUT
else:
p = pxssh.pxssh()
@@ -235,20 +240,18 @@ def main():
ip_list = dict([ (key,value) for key,value in ip_list.items() if '192.168.' not in key])
ip_list = dict([ (key,value) for key,value in ip_list.items() if '127.0.0.1' not in key])
- # sort dict by value (count)
- #ip_list = sorted(ip_list.iteritems(),
- # lambda x,y:cmp(x[1], y[1]),reverse=True)
- ip_list = ip_list.items()
+ ip_list = list(ip_list.items())
if len(ip_list) < 1:
if verbose: print('Warning: no networks connections worth looking at.')
return 0
- ip_list.sort(lambda x,y:cmp(y[1],x[1]))
+ ip_list.sort(key=lambda x:x[1])
# generate some stats for the ip addresses found.
- if average_n <= 1:
+ if average_n is not None and average_n <= 1:
average_n = None
# Reminder: the * unary operator treats the list elements as arguments.
- s = stats(zip(*ip_list[0:average_n])[1])
+ zipped = zip(*ip_list[0:average_n])
+ s = stats(list(zipped)[1])
s['maxip'] = ip_list[0]
# print munin-style or verbose results for the stats.
diff --git a/examples/uptime.py b/examples/uptime.py
index 3187a56..2c1e481 100755
--- a/examples/uptime.py
+++ b/examples/uptime.py
@@ -22,9 +22,9 @@ PEXPECT LICENSE
'''
-from __future__ import print_function
-
from __future__ import absolute_import
+from __future__ import print_function
+from __future__ import unicode_literals
import pexpect
import re
@@ -49,7 +49,7 @@ import re
# 6:08PM up 4 days, 22:26, 1 user, load averages: 0.13, 0.09, 0.08
# This parses uptime output into the major groups using regex group matching.
-p = pexpect.spawn ('uptime')
+p = pexpect.spawnu('uptime')
p.expect('up\s+(.*?),\s+([0-9]+) users?,\s+load averages?: ([0-9]+\.[0-9][0-9]),?\s+([0-9]+\.[0-9][0-9]),?\s+([0-9]+\.[0-9][0-9])')
duration, users, av1, av5, av15 = p.match.groups()