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

Annotation of /ea/ssh_test.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (view) (download) (as text)

1 : agomez 1 '''
2 :     Created on 30/01/2012
3 :    
4 :     @author: R. Valin
5 :     '''
6 :     import os
7 :     import time
8 :     import subprocess as sub
9 :    
10 :     """
11 :     ###################################################################
12 :     # Es necesario hacer comparacion name in out y no al reves #
13 :     # ya que salida devuelve hostname con \n y si no no se cumple cond#
14 :     ###################################################################
15 :     """
16 :    
17 :     def ssh_open(ip, name):
18 :    
19 :     #cmd = 'ssh -o "StrictHostKeyChecking no"'+ip+' hostname'
20 :     cmd = 'ssh -v '+ip+' hostname'
21 :     print cmd
22 :     #p=os.popen(cmd)
23 :     #a=os.system(cmd)
24 :     out = '**'
25 :     j = 0
26 :     print name
27 :     while name not in out and j<40:
28 :     try:
29 :     #print 'abro popen'
30 :     fout = sub.Popen(cmd,bufsize=-1,shell=True,stdout=sub.PIPE, stderr=sub.STDOUT,close_fds=True)
31 :     #print 'salgo popen'
32 :     out,err =fout.communicate(input=None)
33 :     # print 'communicate'
34 :     j = j + 1;
35 :     except ValueError, msg:
36 :     print 'Error', msg, err
37 :     else:
38 :     if name not in out and j < 40:
39 :     print 'Host not available', j
40 :     time.sleep(float(5))
41 :     elif name not in out and j >= 40:
42 :     print 'Sobrepasado el numero de intentos. 40'
43 :     if j < 40 and name in out:
44 :     print 'Host:', name, 'is ssh available', ip
45 :    
46 :     def ssh_close(ip, name):
47 :    
48 :     cmd = 'ssh -o "StrictHostKeyChecking no" '+ip
49 :     #p=os.popen(cmd)
50 :     #a=os.system(cmd)
51 :     fout = sub.Popen(cmd,bufsize=-1,shell=True,stdout=sub.PIPE, stderr=sub.STDOUT)
52 :     out,err =fout.communicate()
53 :     #out = fout.read()
54 :     #print 'out', out
55 :     #out ='pontevedra'
56 :     #out = '**'
57 :     j = 1
58 :     while name in out and j <40:
59 :     try:
60 :     fout = sub.Popen(cmd,bufsize=-1,shell=True,stdout=sub.PIPE, stderr=sub.STDOUT,close_fds=True)
61 :     out,err =fout.communicate(input=None)
62 :     j = j + 1;
63 :     except ValueError, msg:
64 :     print 'Error', msg, err
65 :     else:
66 :     if name in out and j < 40:
67 :     print 'Host available', out, ip, j, err
68 :     time.sleep(float(5))
69 :     elif name in out and j >= 40:
70 :     print 'Sobrepasado el numero de intentos. 40'
71 :     if j < 40 and name not in out:
72 :     print 'Host:', name, 'is not ssh available', j
73 :    
74 :    
75 :     def ssh_qhost(ip):
76 :     "Comprobacion del qhost"
77 :     #
78 :     cmd = 'ssh -q '+ip+' qhost -xml'
79 :     out='command not found'
80 :     while 'command not found' in out:
81 :     fout = sub.Popen(cmd,bufsize=-1,shell=True,stdout=sub.PIPE, stderr=sub.STDOUT)
82 :     out,err =fout.communicate()
83 :    
84 :     #print 'Salida qhost', out
85 :     return out
86 :    
87 :    
88 :    
89 :    
90 :     #ssh_open('193.144.44.218', 'pontevedra')
91 :     #import paramiko
92 :    
93 :     #def ssh_open(ip):
94 :     #
95 :     # #cmd='ssh '+ip
96 :     # #print(os.system(cmd))
97 :     #
98 :     # # ssh config file
99 :     # config = paramiko.SSHConfig()
100 :     # file1=open('/home/cesga/.ssh/config', 'r')
101 :     # print ip
102 :     # config.parse(file1)
103 :     # o = config.lookup('ip')
104 :     # print 'nada', o
105 :     # #print 'Dame el passwd para la clave'
106 :     # #passwd=input()
107 :     # #print passwd
108 :     # try:
109 :     # #result=ssh.connect(ip, password=passwd)
110 :     # ssh = paramiko.SSHClient()
111 :     # ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
112 :     # ssh.connect(ip)
113 :     # #except StandardError:
114 :     # except StandardError, msg:
115 :     # print 'Error:', msg
116 :     #
117 :     # else:
118 :     # try:
119 :     # stdin, stdout, stderr=ssh.exec_command("hostname")
120 :     # except StandardError, msg:
121 :     # print 'Error:', msg
122 :     # for i in stdout:
123 :     # print i
124 :     # ssh.close()
125 :     #
126 :    
127 :    
128 :    

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

Powered By FusionForge