<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#ffffff">
    Ok, naturalmente puoi fare come vuoi :)<br>
    Comunque ti dò un piccolo esempio di pexpect per fare un ping verso
    il dns di google nel router.<br>
    <br>
    import pexpect<br>
    <br>
    <br>
    telnet = pexpect.spawn("telnet 192.168.1.1")<br>
    telnet.expect("Login:")<br>
    telnet.sendline("username del tuo router")<br>
    telnet.expect("Password:")<br>
    telnet.sendline("password_del tuo router")<br>
    telnet.expect("#")<br>
    telnet.senline("ping -c 5 8.8.8.8")<br>
    telnet.expect("#")<br>
    telnet.interact()<br>
    <br>
    L'ho scritto al volo e potrebbero esserci errori, però in generale
    funziona cosi.<br>
    Io lo uso molto spesso in ambito lavorativo e funziona alla grande
    :)<br>
    <br>
    Ciao<br>
    <br>
    <br>
    Il 04/05/2011 10:22, Nicola Ferrari ha scritto:
    <blockquote
      cite="mid:BANLkTi=nBx6+ULN=9jwLkebB3ghVnmKSyg@mail.gmail.com"
      type="cite">
      <blockquote class="gmail_quote" style="margin-top: 0px;
        margin-right: 0px; margin-bottom: 0px; border-left: 1px solid
        rgb(204, 204, 204); padding-left: 1ex;">
        Perchè non provi pexpect?</blockquote>
      <div>Perchè telnetlib sembra più usata/documentata (almeno, visto
        i risultati che mi fornisce google quando ricerco
        python+telnet).</div>
      <div><br>
      </div>
      <div> </div>
      <meta http-equiv="content-type" content="text/html;
        charset=ISO-8859-1">
      <br>
      <div class="gmail_quote">2011/5/4 Andrea Tagliolini -Gmail <span
          dir="ltr"><<a moz-do-not-send="true"
            href="mailto:andreatagliolini@gmail.com">andreatagliolini@gmail.com</a>></span><br>
        <blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt
          0.8ex; border-left: 1px solid rgb(204, 204, 204);
          padding-left: 1ex;">
          <div text="#000000" bgcolor="#ffffff"> Perchè non provi
            pexpect?<br>
            <br>
            <a moz-do-not-send="true"
              href="http://pexpect.sourceforge.net/pexpect.html"
              target="_blank">http://pexpect.sourceforge.net/pexpect.html</a><br>
            <br>
            Ciao<br>
            <br>
            Il 04/05/2011 10:00, Nicola Ferrari ha scritto:
            <blockquote type="cite">
              <div>
                <div class="h5">Buongiorno a tutti.
                  <div>Tramite python (utilizzando l'esempio presente
                    sulla documentazione della libreria telnetlib)
                    riesco a connettermi, tramite telnet, al mio router.
                  </div>
                  <div><br>
                  </div>
                  <div>Ecco il codice del mio file "test.py"</div>
                  <div><br>
                  </div>
                  <div>
                    <div>import getpass</div>
                    <div>import sys</div>
                    <div>import telnetlib</div>
                    <div><br>
                    </div>
                    <div>HOST = "192.168.1.2"</div>
                    <div>user = raw_input("Utente: ")</div>
                    <div>password = getpass.getpass()</div>
                    <div><br>
                    </div>
                    <div>tn = telnetlib.Telnet(HOST)</div>
                    <div><br>
                    </div>
                    <div>tn.read_until("login: ")</div>
                    <div>tn.write(user + "\n")</div>
                    <div>if password:</div>
                    <div>    tn.read_until("Password: ")</div>
                    <div>    tn.write(password + "\n")</div>
                    <div><br>
                    </div>
                    <div>tn.write("help \n")</div>
                    <div>tn.write("exit\n")</div>
                    <div> <br>
                    </div>
                    <div>print tn.read_all()</div>
                  </div>
                  <div><br>
                  </div>
                  <div><br>
                  </div>
                  <div><br>
                  </div>
                  <div>Così facendo, mi vengono forniti tutti i
                    possibili comandi che posso lanciare.</div>
                  <div><br>
                  </div>
                  <div>~ # help </div>
                  <div><br>
                  </div>
                  <div>Built-in commands:</div>
                  <div>-------------------</div>
                  <div>        . : break cd chdir continue eval exec
                    exit export false hash</div>
                  <div>        help local pwd read readonly return set
                    shift source times trap</div>
                  <div>        true type ulimit umask unset wait [ [[
                    ash cat chgrp chmod chown</div>
                  <div>        cp date dd df echo egrep false fgrep free
                    grep halt ifconfig</div>
                  <div>        insmod kill killall klogd ln login ls
                    lsmod mesg mkdir modprobe</div>
                  <div>        more mv netstat nslookup ping pivot_root
                    poweroff ps pwd reboot</div>
                  <div>        rm rmdir rmmod route sh sleep sync
                    syslogd tail telnetd test</div>
                  <div>        tftp top touch traceroute true uname
                    uptime vi watch wget who</div>
                  <div>        whoami</div>
                  <div><br>
                  </div>
                  <div><br>
                  </div>
                  <div>Non riesco a capire come funziona il "write":</div>
                  <div>- scrive tutto quanto e poi, una volta lanciato
                    read_all esegue e legge i risultati</div>
                  <div>- esegue il comando fornito e il read_all legge
                    solamente i risultati??</div>
                  <div><br>
                  </div>
                  <div>E' per capire come poter fare un ping (per
                    esempio)</div>
                  <div><br>
                  </div>
                  <div>Grazie </div>
                  <div><br>
                  </div>
                </div>
              </div>
              <pre><fieldset></fieldset>
_______________________________________________
Python mailing list
<a moz-do-not-send="true" href="mailto:Python@lists.python.it" target="_blank">Python@lists.python.it</a>
<a moz-do-not-send="true" href="http://lists.python.it/mailman/listinfo/python" target="_blank">http://lists.python.it/mailman/listinfo/python</a>
</pre>
            </blockquote>
            <br>
          </div>
          <br>
          _______________________________________________<br>
          Python mailing list<br>
          <a moz-do-not-send="true" href="mailto:Python@lists.python.it">Python@lists.python.it</a><br>
          <a moz-do-not-send="true"
            href="http://lists.python.it/mailman/listinfo/python"
            target="_blank">http://lists.python.it/mailman/listinfo/python</a><br>
          <br>
        </blockquote>
      </div>
      <br>
      <br clear="all">
      <br>
      -- <br>
      Nicola Ferrari<br>
      website: <a moz-do-not-send="true"
        href="http://www.nicolaferrari.name">http://www.nicolaferrari.name</a><br>
      <br>
      skype: nick.ferro<br>
      <br>
      <pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
Python mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Python@lists.python.it">Python@lists.python.it</a>
<a class="moz-txt-link-freetext" href="http://lists.python.it/mailman/listinfo/python">http://lists.python.it/mailman/listinfo/python</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>