[Python] Paramiko - Ciclo for e Array (script)
Remo Cosini
redmine.csi a gmail.com
Mar 6 Ott 2015 14:31:55 CEST
Grazie mille per la correzione,
mi vado a documentare sull'except "senza niente" che sembra una cosa mal
fatta :(
2015-10-06 14:29 GMT+02:00 Marco De Paoli <depaolim a gmail.com>:
> 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
>
>
> _______________________________________________
> Python mailing list
> Python a lists.python.it
> http://lists.python.it/mailman/listinfo/python
>
>
-------------- parte successiva --------------
Un allegato HTML è stato rimosso...
URL: <http://lists.python.it/pipermail/python/attachments/20151006/206a91b9/attachment-0001.html>
Maggiori informazioni sulla lista
Python