def test_sshCopyId(self): with TemporaryDirectory() as tmp: secKey = os.path.join(tmp, 'key') pubKey = secKey + '.pub' authKeys = os.path.expanduser('~/.ssh/authorized_keys') authKeysSic = os.path.join(tmp, 'sic') if os.path.exists(authKeys): shutil.copyfile(authKeys, authKeysSic) os.remove(authKeys) # create new key sshtools.sshKeyGen(secKey) self.assertTrue(os.path.isfile(pubKey)) with open(pubKey, 'rt') as f: pubKeyValue = f.read() try: # test copy pubKey self.assertTrue(sshtools.sshCopyId(pubKey, self.cfg.user(), 'localhost', askPass = '******')) self.assertTrue(os.path.exists(authKeys)) with open(authKeys, 'rt') as f: self.assertIn(pubKeyValue, f.readlines()) finally: # restore original ~/.ssh/authorized_keys file without test pubKey if os.path.exists(authKeysSic): shutil.copyfile(authKeysSic, authKeys)
def test_sshCopyId(self): with TemporaryDirectory() as tmp: secKey = os.path.join(tmp, 'key') pubKey = secKey + '.pub' authKeys = os.path.expanduser('~/.ssh/authorized_keys') authKeysSic = os.path.join(tmp, 'sic') if os.path.exists(authKeys): shutil.copyfile(authKeys, authKeysSic) os.remove(authKeys) # create new key sshtools.sshKeyGen(secKey) self.assertIsFile(pubKey) with open(pubKey, 'rt') as f: pubKeyValue = f.read() try: # test copy pubKey self.assertTrue(sshtools.sshCopyId(pubKey, self.cfg.user(), 'localhost', askPass = '******')) self.assertExists(authKeys) with open(authKeys, 'rt') as f: self.assertIn(pubKeyValue, f.readlines()) finally: # restore original ~/.ssh/authorized_keys file without test pubKey if os.path.exists(authKeysSic): shutil.copyfile(authKeysSic, authKeys)