1.0.1 • Published 2 years ago

edge-router-utils v1.0.1

Weekly downloads
-
License
CC0
Repository
github
Last release
2 years ago

edge-router-utils

Management of Ubiquity UniFy EdgeRouter. Module extends SSH2Promise class, and adds few methods specific for working with EdgeRouters.

Installation

npm install edge-router-utils

Configuring router

  1. Log into your EdgeRouter console:
  • either click CLI icon in topright of GUI window
  • or use your existing SSH/Telnet connection
  1. Generate authentication key pair for accessing router and add public key for authorisation:
cd /tmp
ssh-keygen -t rsa -b 4096 -C "edge-router-utils" -N "" -f auth.key
configure
set system login user edge-router-utils authentication plaintext-password `head -2 auth.key | tail -1`
set system login user edge-router-utils authentication public-keys edge-router-utils type ssh-rsa
set system login user edge-router-utils authentication public-keys edge-router-utils key `sed -E "s/ssh-rsa ([^[:space:]]+).*/\1/" auth.key.pub`
set system login user edge-router-utils level admin
commit
save
exit
  1. Copy content of your private key /tmp/auth.key and save it on your local computer in auth.key file somewhere on your system, usually close to your NodeJS script:
cat /tmp/auth.key
  1. Optionally copy also content of your public key /tmp/auth.key.pub and save it on your local computer in auth.key.pub file somewhere on your system, usually close to your NodeJS script:
/tmp/auth.key.pub
  1. Delete the key pair from router:
rm /tmp/auth.key*

Note: If you already have key pair, you could also use following command to copy private key for authorisation easily on Mac and Linux:

ssh-copy-id -i auth.key -o PreferredAuthentications=password -o PubkeyAuthentication=no edge_username@edge_ip_address

Usage

'use strict'
const EdgeRouter = require('edge-router-utils')
const CONNECTION = {
  host: '192.168.0.1',
  username: 'admin',
  identity: '/path/to/auth.key',
}
async function run(){
  const router = new EdgeRouter(CONNECTION)
  try{
    await router.connect()
    console.log('DHCP:', await router.dhcp())
    console.log('DNS:', await router.dns())
  }catch(e){
    console.error('Error:',e.message)
  }
  try{ await router.close() }catch(e){}
}
run()

API

require('edge-router-utils')

Key methods derived from SSH2Promise

Note: these are described for convenience only and the description is only partial. Consult SSH2Promise for all details and additional methods available.

constructor(< array >|< object >sshConfig, < (Promise) >disableCache)

Creates and returns a new SSH2Promise instance. Single or multiple sshconfigs can be passed. sshConfig passed to SSH2Promise is aligned to ssh2 library. It has few extra options other than ssh2 configuration.

  • host - string - Hostname or IP address of the server. Default: 'localhost'

  • port - integer - Port number of the server. Default: 22

  • username - string - Username for authentication. Default: (none)

  • password - string - Password for password-based user authentication. Default: (none)

  • privateKey - mixed - Buffer or string that contains a private key for either key-based or hostbased user authentication (OpenSSH format). Default: (none)

  • passphrase - string - For an encrypted private key, this is the passphrase used to decrypt it. Default: (none)

  • identity - to directly pass the path of private key file.

close() - (Promise)

Close the sshconnection and associated tunnels.

Added methods specific for edge-router-utils

loadConfig(< boolean >reload) - (Promise)

Loads EdgeRouter configuration to local cache for other methods. If already loaded, cached version will be returnned unless reload parameter is true. Usually not needed to be called, as other methods call it themselves automatically.

dhcp() - (Promise)

Returns EdgeRouter DHCP clients. It automatically calls this.loadConfig() if config has not been loaded. Returned resolved promise is a hash in a form below:

{
  '192.168.1.4': { mac: '12:34:56:78:9a:bc', host: 'my-laptop', static: 1 },
  '192.168.1.5': { mac: '34:56:78:9a:bc:12', host: 'my-desktop', static: 1 },
  '192.168.1.101': { mac: '56:78:9a:bc:12:34', host: '' }
}

Property static indicates this is a static assignment of IP address, otherwise it is dynamic lease.

dns() - (Promise)

Returns EdgeRouter static DNS records. It automatically calls this.loadConfig() if config has not been loaded. Returned resolved promise is a hash in a form below:

{
  '192.168.1.4': [ 'my-laptop', 'my-laptop-alias' },
  '192.168.1.5': { 'my-desktop' }
}

First item in array is configured hostname, all others are configured aliases for this hostname.