3 This file is used to make connections
4 Include ssh & exchange public-key to each other so that
5 it can run without password
14 from foundation import foundation
19 self.loginfo = foundation()
21 def AddKnownHost( self, ipaddr, username, password ):
23 Add an user to known host,so that onos can login in with onos $ipaddr.
26 username: login user name
27 password: login password
29 print( "Now Adding an user to known hosts " + ipaddr )
30 login = pexpect.spawn( "ssh -l %s -p 8101 %s"%( username, ipaddr ) )
33 index = login.expect( ['assword:', 'yes/no', pexpect.EOF, \
36 login.sendline( password )
37 login.sendline( "logout" )
38 index = login.expect( ["closed", pexpect.EOF] )
40 self.loginfo.log( "Add SSH Known Host Success!" )
42 self.loginfo.log( "Add SSH Known Host Failed! Please Check!" )
48 def Gensshkey( self ):
50 Generate ssh keys, used for some server have no sshkey.
52 print "Now Generating SSH keys..."
53 #Here file name may be id_rsa or id_ecdsa or others
54 #So here will have a judgement
55 filelist = os.listdir( '~/.ssh' )
58 self.loginfo.log("SSH keys are exsit in ssh directory.")
60 keysub = pexpect.spawn("ssh-keygen -t rsa")
63 Result = keysub.expect( ["Overwrite", "Enter", pexpect.EOF, \
70 self.loginfo.log("Generate SSH key failed.")
72 self.loginfo.log( "Generate SSH key success." )
74 def GetRootAuth( self, password ):
78 password: root login password
80 print( "Now changing to user root" )
81 login = pexpect.spawn( "su - root" )
84 index = login.expect( ['assword:', "failure", \
85 pexpect.EOF, pexpect.TIMEOUT] )
87 login.sendline( password )
89 self.loginfo.log("Change user to root failed.")
93 def ReleaseRootAuth( self ):
97 print( "Now Release user root" )
98 login = pexpect.spawn( "exit" )
99 index = login.expect( ['logout', \
100 pexpect.EOF, pexpect.TIMEOUT] )
102 self.loginfo.log("Release root user success.")
104 self.loginfo.log("Release root user failed.")
108 def AddEnvIntoBashrc( self, envalue ):
110 Add Env var into /etc/profile.
112 envalue: environment value to add
114 print "Now Adding bash environment"
115 fileopen = open( "/etc/profile", 'r' )
118 findContext = fileopen.readline( )
119 result = findContext.find( envalue )
124 envAdd = open( "/etc/profile", 'a+' )
125 envAdd.writelines( "\n" + envalue )
127 self.loginfo.log( "Add env to bashrc success!" )
129 def OnosConnectionSet (self):
131 Intergrate for ONOS connection setup
134 self.AddKnownHost( self.OC1, "karaf", "karaf" )
135 self.AddKnownHost( self.OC2, "karaf", "karaf" )
136 self.AddKnownHost( self.OC3, "karaf", "karaf" )
137 currentpath = os.getcwd()
138 filepath = os.path.join( currentpath, "onos/tools/dev/bash_profile" )
139 self.AddEnvIntoBashrc("source " + filepath + "\n")
140 self.AddEnvIntoBashrc("export OCT=" + self.OCT)
141 self.AddEnvIntoBashrc("export OC1=" + self.OC1)
142 self.AddEnvIntoBashrc("export OC2=" + self.OC2)
143 self.AddEnvIntoBashrc("export OC3=" + self.OC3)
144 self.AddEnvIntoBashrc("export OCN=" + self.OCN)
145 self.AddEnvIntoBashrc("export OCN2=" + self.OCN2)
146 self.AddEnvIntoBashrc("export localhost=" + self.localhost)