[Python] Paramiko - Ciclo for e Array (script)

Marco De Paoli depaolim a gmail.com
Mar 6 Ott 2015 14:29:19 CEST


ciao Remo,

2015-10-06 12:40 GMT+02:00 Remo Cosini <redmine.csi a gmail.com>:

> Salve a tutti,
> sono nuovo di python e finora sono riuscito a cavarmela da autodidatta.
> Ho trovato uno script (che fa uso di paramiko) e l'ho modificato secondo
> mie esigenze. Avevo necessitÓ di testare degli accessi SSH (con user e pwd
> giÓ cablata) partendo da un array e mettendo
> il tutto dentro un ciclo, ma sembra non andare, l'errore che leggo Ŕ il
> seguente:
>
> Could not SSH to ['localhost', '127.0.0.1'], waiting for it to start
> Could not connect to ['localhost', '127.0.0.1']. Giving up
>
> Sembra non prendere i valori host uno per volta, ma tutti insieme, ecco lo
> script:
>
> import sys
> import time
> import select
> import paramiko
> i = 1
> host = ['localhost', '127.0.0.1', '192.168.2.2']
> for item in host:
>

da questo momento in poi dovresti usare "item" e non "host"

        while True:
>                 print 'Trying to connect to %s (%i/3)' % (host, i)
>                 try:
>                         ssh = paramiko.SSHClient()
>
> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>                         ssh.connect(host, port=22, username='centos',
> password='reverse')
>

qui usi "host", ma dovevi usare "item"


>                         print "Connected to %s" % host
>                         break
>                 except paramiko.AuthenticationException:
>                         print "Authentication failed when connecting to
> %s" % host
>                         sys.exit(1)
>                 except:
>

ti consiglio di evitare l'except "senza niente"
https://realpython.com/blog/python/the-most-diabolical-python-antipattern/


>                         print "Could not SSH to %s, waiting for it to
> start" % host
>                         i += 1
>                         time.sleep(2)
>                 # If we could not connect within time limit
>                 if i == 3:
>                         print "Could not connect to %s. Giving up" % host
>                         sys.exit(1)
>         # Send the command (non-blocking)
>         stdin, stdout, stderr = ssh.exec_command("uname")
>         # Wait for the command to terminate
>         while not stdout.channel.exit_status_ready():
>                 # Only print data if there is data to read in the channel
>                 if stdout.channel.recv_ready():
>                         rl, wl, xl = select.select([stdout.channel], [],
> [], 0.0)
>                         if len(rl) > 0:
>                                 # Print data from stdout
>                                 print '-------------------------------'
>                                 print stdout.channel.recv(1024),
>         # Send the command (non-blocking)
>         stdin, stdout, stderr = ssh.exec_command("hostname")
>         # Wait for the command to terminate
>         while not stdout.channel.exit_status_ready():
>                 # Only print data if there is data to read in the channel
>                 if stdout.channel.recv_ready():
>                         rl, wl, xl = select.select([stdout.channel], [],
> [], 0.0)
>                         if len(rl) > 0:
>                                 # Print data from stdout
>                                 print stdout.channel.recv(1024),
>         # Send the command (non-blocking)
>         stdin, stdout, stderr = ssh.exec_command("hostname -I")
>         # Wait for the command to terminate
>         while not stdout.channel.exit_status_ready():
>                 # Only print data if there is data to read in the channel
>                 if stdout.channel.recv_ready():
>                         rl, wl, xl = select.select([stdout.channel], [],
> [], 0.0)
>                         if len(rl) > 0:
>                                 # Print data from stdout
>                                 print stdout.channel.recv(1024),
>                         print '-------------------------------'
>         #
>         # Disconnect from the host
>         #
>         print "Command done, closing SSH connection."
>         ssh.close()
>
> Qualcuno mi illumina?
>

ciao,
Marco
-------------- parte successiva --------------
Un allegato HTML Ŕ stato rimosso...
URL: <http://lists.python.it/pipermail/python/attachments/20151006/4b068428/attachment.html>


Maggiori informazioni sulla lista Python