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

Remo Cosini redmine.csi a gmail.com
Mar 6 Ott 2015 12:40:49 CEST


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:
        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')
                        print "Connected to %s" % host
                        break
                except paramiko.AuthenticationException:
                        print "Authentication failed when connecting to %s"
% host
                        sys.exit(1)
                except:
                        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?
-------------- parte successiva --------------
Un allegato HTML è stato rimosso...
URL: <http://lists.python.it/pipermail/python/attachments/20151006/353ea6ba/attachment.html>


Maggiori informazioni sulla lista Python