mirror of
https://github.com/easingthemes/ssh-deploy.git
synced 2025-06-22 04:22:10 +10:00
update Known Hosts
This commit is contained in:
parent
fdc1b9a24d
commit
9aabe10cb3
5 changed files with 30 additions and 10 deletions
|
@ -1,25 +1,43 @@
|
|||
const { join } = require('path');
|
||||
const { execSync } = require('child_process');
|
||||
const { writeToFile } = require('./helpers');
|
||||
|
||||
const getPrivateKeyPath = (filename) => {
|
||||
const KNOWN_HOSTS = 'known_hosts';
|
||||
const getPrivateKeyPath = (filename = '') => {
|
||||
const { HOME } = process.env;
|
||||
const dir = join(HOME || __dirname, '.ssh');
|
||||
const knownHostsPath = join(dir, KNOWN_HOSTS);
|
||||
return {
|
||||
dir,
|
||||
filename,
|
||||
path: join(dir, filename)
|
||||
path: join(dir, filename),
|
||||
knownHostsPath
|
||||
};
|
||||
};
|
||||
|
||||
const addSshKey = (content, deployKeyName) => {
|
||||
const { dir, filename } = getPrivateKeyPath(deployKeyName);
|
||||
writeToFile({ dir, filename: 'known_hosts', content: '' });
|
||||
writeToFile({ dir, filename: KNOWN_HOSTS, content: '' });
|
||||
console.log('✅ [SSH] known_hosts file ensured', dir);
|
||||
writeToFile({ dir, filename, content, isRequired: true });
|
||||
console.log('✅ [SSH] key added to `.ssh` dir ', dir, filename);
|
||||
};
|
||||
|
||||
const updateKnownHosts = (host) => {
|
||||
const { knownHostsPath } = getPrivateKeyPath();
|
||||
console.log('✅ [SSH] Adding host to `known_hosts` ....', host, knownHostsPath);
|
||||
try {
|
||||
execSync(`ssh-keyscan -H ${host} >> ${knownHostsPath}`, {
|
||||
stdio: 'inherit'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('✅ [SSH] Adding host to `known_hosts` ERROR', host, error.message);
|
||||
}
|
||||
console.log('✅ [SSH] Adding host to `known_hosts` DONE', host, knownHostsPath);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
getPrivateKeyPath,
|
||||
updateKnownHosts,
|
||||
addSshKey
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue