File size: 1,122 Bytes
cf0f72e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env node

(function() {
  'use strict';

  require('terminal-colors');

  var cp = require('child_process'),
      ssh = require('ssh-client'),
      host = process.argv[2];

  function help() {
    var pkg = require('./package');
    console.log('# sshpair (sshync', pkg.version + ')');
    console.log('# by', pkg.author, '\n');
    console.log('sshpair <user@host:port>');
    process.exit();
  }

  if(!host || host.indexOf('@') === -1)
    help();

  host = host.split('@');
  var host_ip = host.pop(),
      host_user = host.pop();

  cp.exec('echo -e "y\n" | ssh-keygen -q -N "" -f ~/.ssh/id_rsa', () => {});
  console.log('generated ssh key to', '~/.ssh/id_rsa.pub'.blue);

  var client = ssh(host_user, host_ip, () => {
    client.exec('mkdir -p ~/.ssh', (error, stdout, stderr) => {
      if (error)
        throw error;

      client.putFile('~/.ssh/id_rsa.pub', '~/.ssh/authorized_keys', (error, stdout, stderr) => {
        if (error)
          throw error;

        console.log('~/.ssh/id_rsa.pub'.blue, '=>', '~/.ssh/authorized_keys'.green);
        process.exit();
      });
    });
  });
}());