The following piece of code checks whether a ssh root login is possible on a remote host.
class MyAlarm(Exception): pass def alarm_handler(signum, frame): raise MyAlarm def checkHostRootLogin( host): ”' - returns True, if the host replies to a root login (executing 'hostname') - the time-out is 3 seconds ”' import subprocess import signal signal.signal(signal.SIGALRM, alarm_handler) try: # # the ssh authentication flags are needed for cronjobs # p = subprocess.Popen( ['ssh', "-x", "-o", "PubkeyAuthentication=yes", "-o", "GSSAPIAuthentication=no", "root@%s" % host, "hostname > /dev/null" ]) except Exception as e: print( "checkHostRooLogin: exception occured", repr( e)) signal.alarm( 3) try: p.wait() signal.alarm(0) except MyAlarm: p.kill() p.wait() return False return True