# # BonFIRE Virtual Clusters on Federated Clouds Demonstration Kit # # Copyright (c) Fundacion Centro Tecnologico de Supercomputacion de Galicia 2012 # # License Apache Software # # The research leading to these results has received funding from # the European Community's Seventh Framework Programme (FP7/2007-2013) # under agreement number 257386 # # This software is provided with ABSOLUTELY NO WARRANTY # ''' Created on 30/01/2012 @author: R. Valin ''' import os import time import subprocess as sub """ ################################################################### # Es necesario hacer comparacion name in out y no al reves # # ya que salida devuelve hostname con \n y si no no se cumple cond# ################################################################### """ def ssh_open(ip, name): #cmd = 'ssh -o "StrictHostKeyChecking no"'+ip+' hostname' cmd = 'ssh -v '+ip+' hostname' print cmd #p=os.popen(cmd) #a=os.system(cmd) out = '**' j = 0 print name while name not in out and j<40: try: #print 'abro popen' fout = sub.Popen(cmd,bufsize=-1,shell=True,stdout=sub.PIPE, stderr=sub.STDOUT,close_fds=True) #print 'salgo popen' out,err =fout.communicate(input=None) # print 'communicate' j = j + 1; except ValueError, msg: print 'Error', msg, err else: if name not in out and j < 40: print 'Host not available', j time.sleep(float(5)) elif name not in out and j >= 40: print 'Sobrepasado el numero de intentos. 40' if j < 40 and name in out: print 'Host:', name, 'is ssh available', ip def ssh_close(ip, name): cmd = 'ssh -o "StrictHostKeyChecking no" '+ip #p=os.popen(cmd) #a=os.system(cmd) fout = sub.Popen(cmd,bufsize=-1,shell=True,stdout=sub.PIPE, stderr=sub.STDOUT) out,err =fout.communicate() #out = fout.read() #print 'out', out #out ='pontevedra' #out = '**' j = 1 while name in out and j <40: try: fout = sub.Popen(cmd,bufsize=-1,shell=True,stdout=sub.PIPE, stderr=sub.STDOUT,close_fds=True) out,err =fout.communicate(input=None) j = j + 1; except ValueError, msg: print 'Error', msg, err else: if name in out and j < 40: print 'Host available', out, ip, j, err time.sleep(float(5)) elif name in out and j >= 40: print 'Sobrepasado el numero de intentos. 40' if j < 40 and name not in out: print 'Host:', name, 'is not ssh available', j def ssh_qhost(ip): "Comprobacion del qhost" # cmd = 'ssh -q '+ip+' qhost -xml' out='command not found' while 'command not found' in out: fout = sub.Popen(cmd,bufsize=-1,shell=True,stdout=sub.PIPE, stderr=sub.STDOUT) out,err =fout.communicate() #print 'Salida qhost', out return out #ssh_open('193.144.44.218', 'pontevedra') #import paramiko #def ssh_open(ip): # # #cmd='ssh '+ip # #print(os.system(cmd)) # # # ssh config file # config = paramiko.SSHConfig() # file1=open('/home/cesga/.ssh/config', 'r') # print ip # config.parse(file1) # o = config.lookup('ip') # print 'nada', o # #print 'Dame el passwd para la clave' # #passwd=input() # #print passwd # try: # #result=ssh.connect(ip, password=passwd) # ssh = paramiko.SSHClient() # ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # ssh.connect(ip) # #except StandardError: # except StandardError, msg: # print 'Error:', msg # # else: # try: # stdin, stdout, stderr=ssh.exec_command("hostname") # except StandardError, msg: # print 'Error:', msg # for i in stdout: # print i # ssh.close() #