Skip to content

Commit b821fd6

Browse files
committed
Merge pull request alphagov#109 from alphagov/check-host-key
Add a task to check the host key of a machine
2 parents 7b8c546 + 5fe3d6f commit b821fd6

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

vm.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,22 @@ def poweroff():
107107
from nagios import schedule_downtime
108108
execute(schedule_downtime, env['host_string'])
109109
run("sudo poweroff")
110+
111+
@task
112+
@hosts('puppetmaster-1.management')
113+
def host_key(hostname):
114+
"""
115+
Check the SSH host key of a machine. This task runs on the Puppetmaster because
116+
it's the only machine that knows about all host keys.
117+
118+
Usage:
119+
fab production vm.host_key:backend-1.backend
120+
"""
121+
with hide('running', 'stdout'):
122+
ssh_key = run("grep {0} /etc/ssh/ssh_known_hosts | head -1".format(hostname))
123+
124+
if ssh_key == '':
125+
print 'Machine {0} not found in ssh_known_hosts file'.format(hostname)
126+
else:
127+
with hide('running'):
128+
run("ssh-keygen -l -f /dev/stdin <<< '{0}'".format(ssh_key))

0 commit comments

Comments
 (0)