--- ea/ssh_test.py 2012/10/09 16:14:55 17 +++ ea/ssh_test.py 2012/10/09 16:17:03 18 @@ -1,141 +1,141 @@ -# -# 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 - -""" -################################################################### -# Checks name in out # -# because output includes hostname with \n and it fails otherwise# -################################################################### -""" - -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 'open popen' - fout = sub.Popen(cmd,bufsize=-1,shell=True,stdout=sub.PIPE, stderr=sub.STDOUT,close_fds=True) - #print 'exit 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 'Number of trials exeeded. 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 'Number of trials exceeded. 40' - if j < 40 and name not in out: - print 'Host:', name, 'is not ssh available', j - - -def ssh_qhost(ip): - "Checking 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 'Exit 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 'Give password:' -# #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() -# - - - +# +# 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 + +""" +################################################################### +# Checks name in out # +# because output includes hostname with \n and it fails otherwise# +################################################################### +""" + +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 'open popen' + fout = sub.Popen(cmd,bufsize=-1,shell=True,stdout=sub.PIPE, stderr=sub.STDOUT,close_fds=True) + #print 'exit 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 'Number of trials exeeded. 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 'Number of trials exceeded. 40' + if j < 40 and name not in out: + print 'Host:', name, 'is not ssh available', j + + +def ssh_qhost(ip): + "Checking 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 'Exit 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 'Give password:' +# #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() +# + + +