La lecture de la sortie de commande en Haut à l'aide de Paramiko
Je suis en train d'écrire un script en Python pour la connexion en ssh et lire la sortie des commandes exécutées. Je suis à l'aide de paramiko paquet pour cette. Je suis en train d'exécuter la commande "top" et obtenez sa sortie imprimée sur la console. Cependant, je ne suis pas capable de faire cela. S'il vous plaît trouver l'extrait de code:
import sys
import time
import select
import paramiko
host = 'localhost'
i = 1
#
# Try to connect to the host.
# Retry a few times if it fails.
#
while True:
print 'Trying to connect to %s (%i/30)' % (host, i)
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port=22, username='dummy', password='dummy')
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 == 30:
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)
print '-------------------------------'
# Send the command (non-blocking)
stdin, stdout, stderr = ssh.exec_command("top -n 1")
# 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)
print '-------------------------------'
# 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)
print '-------------------------------'
#
# Disconnect from the host
#
print "Command done, closing SSH connection"
ssh.close()
De sortie:
Essayez de vous connecter à localhost (1/30)
Connecté à localhost
Linux
-------------------------------
Linux
Commande fait, la fermeture de la connexion SSH
Je ne suis pas sûr, où je fais de mal. Je suis en mesure d'obtenir en sortie des autres commandes linux. Mais vous ne savez pas, pourquoi top sortie de la commande n'est pas imprimé.
OriginalL'auteur N Deepak Prasath | 2014-08-03
Vous devez vous connecter pour publier un commentaire.
top
utilise normalement des malédictions pour l'affichage plutôt que de simplement l'impression. Essayez le-b
pour les lots option avec-n 1
vous avez (en haut les options varient selon la plate-forme, consultez la page de manuel). Et à l'avenir, essayez d'isoler le problème le plus - si vous étiez à invoquertop
viassh
sur la ligne de commande sans votre script, vous auriez encore un problème.Oui je l'ai fait. Est-ce que votre hôte
top
l'appui de la-b
option?OriginalL'auteur Jason S
comme Jason S souligné
fonctionne très bien.
OriginalL'auteur tintin