Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If it is a single device, you should use a solution that handles the low-level <code>socket</code> interactions for you... python has several like <a href="http://www.noah.org/wiki/pexpect" rel="nofollow noreferrer"><code>pexpect</code></a>, <a href="https://github.com/knipknap/exscript/wiki" rel="nofollow noreferrer">exscript</a>, or <a href="http://www.lag.net/paramiko/" rel="nofollow noreferrer"><code>paramiko</code> (ssh only)</a>... if it is multiple devices and you need asynchronous communication, use @zeekay's answer (although async programming, particularly with twisted, is unpleasant if you're not already familiar with it).</p> <p>I answered a question with the way you telnet and send a list of commands to a single device here...</p> <p><a href="https://stackoverflow.com/questions/5602080/catching-a-dying-process-in-pexpect/5602629#5602629">Catching a dying process in pexpect</a></p> <p>The answer above makes the code efficient, but is more challenging for a beginner to understand... the following code is simpler, it makes a telnet connection on TCP/23, waits for <code>*</code>, sends a command, and puts the response to the command in <code>mydata1</code>...</p> <pre><code>import pexpect as px import sys def send_this(child, retcode, COMMAND): if retcode == 2: child.sendline(COMMAND) else: raise RuntimeError, "Could not complete login, due to socket error" def expect_this(child, EXPR, timeout = 10): return child.expect([px.TIMEOUT, px.EOF, EXPR], timeout = timeout) HOST = '192.168.49.49' CMD1 = '1I' PROMPT = '\*' #Note: you might not need the backslash TIMEOUT = 10 child = px.spawn('telnet %s' % HOST) retcode = expect_this(child, PROMPT) send_this(child, retcode, CMD1) retcode = expect_this(child, PROMPT) mydata1 = child.before # mydata has the output from CMD1 </code></pre> <p>Regardless of the solution, the most important thing is tweaking your timeouts so you don't have to concern yourself with the long cell-network delays (which are sometimes over 5 seconds in my experience).</p> <p>The other nasty dynamic in cellular communication is that many consumer devices <em>routinely</em> change their IP address when they are moving (due to their reliance on dhcp)... there isn't much TCP can do if this happens... if you don't have a static address the connection will drop when the device pulls a new ip address.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload