Add a utility to remove SSH keys from the known_hosts file

Change-Id: I136dfa50835eaae177fb516dcf9866798149ea2e
This commit is contained in:
Dougal Matthews 2015-05-14 15:46:51 +01:00
parent f8fc455cf5
commit a568228739
2 changed files with 30 additions and 0 deletions

View File

@ -273,3 +273,23 @@ class TestWaitForDiscovery(TestCase):
sleep=0.01)
self.assertEqual(result, False)
@mock.patch('subprocess.check_call')
@mock.patch('os.path.exists')
def test_remove_known_hosts(self, mock_exists, mock_check_call):
mock_exists.return_value = True
utils.remove_known_hosts('192.168.0.1')
mock_check_call.assert_called_with(['ssh-keygen', '-R', '192.168.0.1'])
@mock.patch('subprocess.check_call')
@mock.patch('os.path.exists')
def test_remove_known_hosts_no_file(self, mock_exists, mock_check_call):
mock_exists.return_value = False
utils.remove_known_hosts('192.168.0.1')
mock_check_call.assert_not_called()

View File

@ -315,3 +315,13 @@ def get_hiera_key(key_name):
p = subprocess.Popen(command, stdout=subprocess.PIPE)
out, err = p.communicate()
return out
def remove_known_hosts(overcloud_ip):
"""For a given IP address remove SSH keys from the known_hosts file"""
known_hosts = os.path.expanduser("~/.ssh/known_hosts")
if os.path.exists(known_hosts):
command = ['ssh-keygen', '-R', overcloud_ip]
subprocess.check_call(command)