Ok, naturalmente puoi fare come vuoi :)
Comunque ti dò un piccolo esempio di pexpect per fare un ping verso il dns di google nel router.

import pexpect


telnet = pexpect.spawn("telnet 192.168.1.1")
telnet.expect("Login:")
telnet.sendline("username del tuo router")
telnet.expect("Password:")
telnet.sendline("password_del tuo router")
telnet.expect("#")
telnet.senline("ping -c 5 8.8.8.8")
telnet.expect("#")
telnet.interact()

L'ho scritto al volo e potrebbero esserci errori, però in generale funziona cosi.
Io lo uso molto spesso in ambito lavorativo e funziona alla grande :)

Ciao


Il 04/05/2011 10:22, Nicola Ferrari ha scritto:

    Perchè non provi pexpect?

Perchè telnetlib sembra più usata/documentata (almeno, visto i risultati che mi fornisce google quando ricerco python+telnet).


2011/5/4 Andrea Tagliolini -Gmail <andreatagliol...@gmail.com <mailto:andreatagliol...@gmail.com>>

    Perchè non provi pexpect?

    http://pexpect.sourceforge.net/pexpect.html

    Ciao

    Il 04/05/2011 10:00, Nicola Ferrari ha scritto:
    Buongiorno a tutti.
    Tramite python (utilizzando l'esempio presente sulla
    documentazione della libreria telnetlib) riesco a connettermi,
    tramite telnet, al mio router.

    Ecco il codice del mio file "test.py"

    import getpass
    import sys
    import telnetlib

    HOST = "192.168.1.2"
    user = raw_input("Utente: ")
    password = getpass.getpass()

    tn = telnetlib.Telnet(HOST)

    tn.read_until("login: ")
    tn.write(user + "\n")
    if password:
        tn.read_until("Password: ")
        tn.write(password + "\n")

    tn.write("help \n")
    tn.write("exit\n")

    print tn.read_all()



    Così facendo, mi vengono forniti tutti i possibili comandi che
    posso lanciare.

    ~ # help

    Built-in commands:
    -------------------
            . : break cd chdir continue eval exec exit export false hash
            help local pwd read readonly return set shift source
    times trap
            true type ulimit umask unset wait [ [[ ash cat chgrp
    chmod chown
            cp date dd df echo egrep false fgrep free grep halt ifconfig
            insmod kill killall klogd ln login ls lsmod mesg mkdir
    modprobe
            more mv netstat nslookup ping pivot_root poweroff ps pwd
    reboot
            rm rmdir rmmod route sh sleep sync syslogd tail telnetd test
            tftp top touch traceroute true uname uptime vi watch wget who
            whoami


    Non riesco a capire come funziona il "write":
    - scrive tutto quanto e poi, una volta lanciato read_all esegue e
    legge i risultati
    - esegue il comando fornito e il read_all legge solamente i
    risultati??

    E' per capire come poter fare un ping (per esempio)

    Grazie


    _______________________________________________
    Python mailing list
    Python@lists.python.it  <mailto:Python@lists.python.it>
    http://lists.python.it/mailman/listinfo/python


    _______________________________________________
    Python mailing list
    Python@lists.python.it <mailto:Python@lists.python.it>
    http://lists.python.it/mailman/listinfo/python




--
Nicola Ferrari
website: http://www.nicolaferrari.name

skype: nick.ferro


_______________________________________________
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python

_______________________________________________
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python

Rispondere a