2016-10-02 3 views
2

Ich automatisiere einige Konfigurationsschritte auf CentOS. Um dies zu tun, muss ich das System auch neu starten. Ich starte den "reboot" -Befehl via python pexepct, aber ich muss warten, bis das System hochgefahren ist, damit das restliche Skript ausgeführt werden kann. Dafür habe ich dieses kleine Stück Code geschrieben.Python pexpect Prüfe, ob der Server aktiv ist

while True: 
     result = commands.getoutput("ping -c 4 192.168.36.134") 
     if result.find("Unreachable") == 1: 
      result = False 
      print 'Rebooting the Systems.' 
     else: 
      result = True 
      print 'Connected!' 
      break 

Gibt es einen besseren Weg, dies zu tun? Können wir dies auch mit dem Pexepct selbst erreichen?

Antwort

3

Sie können dies versuchen:

import socket 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
# wait host to finish reboot, check specific port connection (usually ssh port if you want to exec remote commands) 
while True: 
    try: 
     s.connect(('hostname', 22)) 
     print "Port 22 reachable" 
     break 
    except socket.error as e: 
     print "rebooting..." 
# continue 
s.close() 

Dieses Beispiel ist effektiver, dann ping

Verwandte Themen