-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.py
52 lines (43 loc) · 1.44 KB
/
ssh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import re
import paramiko
from time import sleep
from log import log
import sys
import logging
def wait_SSH_up(host,myuser):
log("Waiting for SSH to come up")
#logging.basicConfig(level=logging.DEBUG)
while(True):
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
client.connect(host, username=myuser,timeout=10)
stdin, stdout, stderr = client.exec_command("echo")
exit_status = stdout.channel.recv_exit_status()
client.close()
return (exit_status == 0)
except paramiko.AuthenticationException:
print("Authentication failed when connecting to ", host)
sys.exit(1)
except:
sleep(2)
log('.')
def tail_cloudinit(host,myuser):
log("Tailing cloud init log*")
# Wait until cloud-init is finished to stop the tail
cloudinit_finished = re.compile(r"Cloud-init[\.\s|\w]{7,14}finished", re.I)
# runs for each line
def is_finished(line):
return r.match(line)
# ssh login and tail the cloud init logfile
client = paramiko.SSHClient()
# Set SSH key parameters to auto accept unknown hosts
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the host
client.connect(host, username=myuser, timeout=10)
stdin, stdout, stderr = client.exec_command("tail -f /var/log/cloud-init-output.log")
for line in stdout:
log(' | ' + line[:-1])
if cloudinit_finished.match(line):
log("Cloud-init finished, server should be up... *rejoice!*")
break