diff --git a/examples/cpu.py b/examples/cpu.py
index 0b7e67bef60d87f95052aebdcb21316bcb9bf6a6..630fa2ed64b6aa7f5c967eaf12b9b346c7b7863f 100755
--- a/examples/cpu.py
+++ b/examples/cpu.py
@@ -7,23 +7,11 @@
 from mininet.net import Mininet
 from mininet.node import CPULimitedHost
 from mininet.topolib import TreeTopo
-from mininet.util import custom
+from mininet.util import custom, waitListening
 from mininet.log import setLogLevel, output, info
 
 from time import sleep
 
-def waitListening(client, server, port):
-    "Wait until server is listening on port"
-    if not client.cmd('which telnet'):
-        raise Exception('Could not find telnet')
-    cmd = ('sh -c "echo A | telnet -e A %s %s"' %
-           (server.IP(), port))
-    while 'Connected' not in client.cmd(cmd):
-        output('waiting for', server,
-               'to listen on port', port, '\n')
-        sleep(.5)
-
-
 def bwtest( cpuLimits, period_us=100000, seconds=5 ):
     """Example/test of link and CPU bandwidth limits
        cpu: cpu limit as fraction of overall CPU time"""
diff --git a/examples/sshd.py b/examples/sshd.py
index 27166cb909837a80221e920614b430e559584943..02d82a9f0d9cd3346b739d73af2c0177f220a3bb 100755
--- a/examples/sshd.py
+++ b/examples/sshd.py
@@ -24,6 +24,7 @@
 from mininet.node import Node
 from mininet.topolib import TreeTopo
 from mininet.link import Link
+from mininet.util import waitListening
 
 def TreeNet( depth=1, fanout=2, **kwargs ):
     "Convenience function for creating tree networks."
@@ -59,6 +60,10 @@ def sshd( network, cmd='/usr/sbin/sshd', opts='-D',
     connectToRootNS( network, switch, ip, routes )
     for host in network.hosts:
         host.cmd( cmd + ' ' + opts + '&' )
+    print "*** Waiting for ssh daemons to start"
+    for server in network.hosts:
+        waitListening( server=server, port=22, timeout=5 )
+
     print
     print "*** Hosts are running sshd at the following addresses:"
     print
diff --git a/mininet/util.py b/mininet/util.py
index 3dbbea6c758cb80ce5c0268b91e74efafd7c41a1..41acfccc4dcc5b8e9d018dc1c4c7f1d78b7610b0 100644
--- a/mininet/util.py
+++ b/mininet/util.py
@@ -10,6 +10,7 @@
 from fcntl import fcntl, F_GETFL, F_SETFL
 from os import O_NONBLOCK
 import os
+from functools import partial
 
 # Command execution support
 
@@ -61,12 +62,6 @@ def errRun( *cmd, **kwargs ):
        stderr: STDOUT to merge stderr with stdout
        shell: run command using shell
        echo: monitor output to console"""
-    # Allow passing in a list or a string
-    if len( cmd ) == 1:
-        cmd = cmd[ 0 ]
-        if isinstance( cmd, str ):
-            cmd = cmd.split( ' ' )
-    cmd = [ str( arg ) for arg in cmd ]
     # By default we separate stderr, don't run in a shell, and don't echo
     stderr = kwargs.get( 'stderr', PIPE )
     shell = kwargs.get( 'shell', False )
@@ -74,6 +69,14 @@ def errRun( *cmd, **kwargs ):
     if echo:
         # cmd goes to stderr, output goes to stdout
         info( cmd, '\n' )
+    if len( cmd ) == 1:
+        cmd = cmd[ 0 ]
+    # Allow passing in a list or a string
+    if isinstance( cmd, str ) and not shell:
+        cmd = cmd.split( ' ' )
+        cmd = [ str( arg ) for arg in cmd ]
+    elif isinstance( cmd, list ) and shell:
+        cmd = " ".join( arg for arg in cmd )
     popen = Popen( cmd, stdout=PIPE, stderr=stderr, shell=shell )
     # We use poll() because select() doesn't work with large fd numbers,
     # and thus communicate() doesn't work either
@@ -538,3 +541,25 @@ def ensureRoot():
         print "*** Mininet must run as root."
         exit( 1 )
     return
+
+def waitListening( client=None, server='127.0.0.1', port=80, timeout=None ):
+    "Wait until server is listening on port"
+    run = ( client.cmd if client else
+                partial( quietRun, shell=True ) )
+    if not run( 'which telnet' ):
+        raise Exception('Could not find telnet' )
+    serverIP = server if type( server ) is str else server.IP()
+    cmd = ( 'sh -c "echo A | telnet -e A %s %s"' %
+           ( serverIP, port ) )
+    time = 0
+    while 'Connected' not in run( cmd ):
+        if timeout:
+            if time >= timeout:
+                error( 'could not connect to %s on port %d\n'
+                       % ( server, port ) )
+                break
+        output('waiting for', server,
+               'to listen on port', port, '\n')
+        sleep( .5 )
+        time += .5
+