12 from backup.pcsbackuputils import createFolder
14 sshPath = os.path.expanduser('~/.ssh/')
15 known_hosts = os.path.join(sshPath, 'known_hosts')
16 log_file = os.path.expanduser('~/.pcsuite/.ssh_log')
20 def create_route(host, port=22):
21 # Verify Auth with privateKey
23 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
25 sock.connect((host, port))
29 print 'No route to host'
32 def verify_exist_keys(host, port=22):
34 transport = _create_transport(host, port)
38 getKey = paramiko.RSAKey.from_private_key_file(sshPath + keyName)
39 transport.start_client()
40 transport.auth_publickey(user, getKey)
41 if transport.is_authenticated():
45 # 'Error in auth with publickey, try with password...'
49 def keyExchange(host, passwd, port=22):
50 if not os.path.exists(sshPath):
53 # Clean cached keys in ssh-agent
54 os.system('ssh-add -d')
57 transport = _create_transport(host, port)
62 if not _add_host_fingerprint(host):
66 if not _authenticate(user, passwd, transport):
70 if not _add_key_to_host(host, transport):
74 transport.stop_thread()
81 def _create_transport(host, port):
82 # Create a transport and initiate client mode
84 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
86 sock.connect((host, port))
87 except Exception, msg:
88 print 'Connect failed: ' + str(msg)
89 raise Exception('Error while create sockets.')
90 transport = paramiko.Transport(sock)
93 def _add_host_fingerprint(host):
94 if not os.path.exists(known_hosts):
95 os.system('touch %s' %known_hosts)
96 if os.system('ssh-keyscan -t rsa %s >> %s' %(host, known_hosts)) != 0:
100 def _generate_keys():
101 # Generate public and private RSAKey
102 keyFile = os.path.join(sshPath, keyName)
103 if not os.path.exists(keyFile):
104 privateKey = paramiko.RSAKey.generate(2048)
105 privateKey.write_private_key_file(keyFile)
106 login = pwd.getpwuid(os.geteuid())[0]
107 publicKey = '%s %s %s@%s' %(privateKey.get_name(),
108 privateKey.get_base64(),
109 login , socket.gethostname())
111 keyFile = open(keyFile + '.pub','w')
112 keyFile.write(publicKey)
115 print 'Error while save the public key'
119 privateKey = paramiko.RSAKey.from_private_key_file(keyFile)
120 login = pwd.getpwuid(os.geteuid())[0]
121 publicKey = '%s %s %s@%s' %(privateKey.get_name(),
122 privateKey.get_base64(),
123 login , socket.gethostname())
125 print 'Error while read the private key'
129 def _authenticate(user, passwd, transport):
130 # Try Auth with password
132 transport.start_client()
133 transport.auth_password(user, passwd)
135 print 'Verify user or password.'
137 if not transport.is_authenticated():
138 print 'Authentication fail'
142 exception = transport.get_exception()
145 except Exception, msg:
146 print 'Error in connection: ' + str(msg)
150 def _add_key_to_host(host, transport):
151 # Add publickey in host
152 if not transport.is_active():
153 print 'Channel is not active'
156 paramiko.util.log_to_file(log_file, 10)
157 channel = transport.open_session()
159 channel.exec_command('mkdir -p ~/.ssh; echo %s >> .ssh/authorized_keys' % (_generate_keys()))
160 except Exception, msg:
161 print 'Error while generate or add the keys.'