Log In | Get Help   
Home My Page Projects Code Snippets Project Openings BonFIRE VCOC Demonstration Kit
Summary Activity SCM Files Wiki
[bonfiredemokit] View of /ea/ssh_test.py
[bonfiredemokit] / ea / ssh_test.py Repository:
ViewVC logotype

View of /ea/ssh_test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 18 - (download) (as text) (annotate)
Tue Oct 9 16:17:03 2012 UTC (11 years, 7 months ago) by agomez
File size: 3936 byte(s)
Changed comments to English and test
#
# 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()
#     




root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge