module Ssh4iot

Constants

VERSION

Public Instance Methods

add_user(user) click to toggle source
# File lib/ssh4iot.rb, line 26
def add_user(user)
  sh "useradd #{user} && mkdir /home/#{user} && chown -R #{user}#{user} /home/#{user}"
end
clear_docker() click to toggle source
# File lib/ssh4iot.rb, line 16
def clear_docker
  sh "docker rm -f #{params['container']}"
end
keygen() click to toggle source
# File lib/ssh4iot.rb, line 30
def keygen
  sh "ssh-keygen -q -t rsa -f ~/.ssh/id_rsa -N ''"
end
register_key() click to toggle source
# File lib/ssh4iot.rb, line 33
def register_key
  sh "cat ~/.ssh/id_rsa.pub | ssh demo@198.51.100.0 \"mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >>  ~/.ssh/authorized_keys\""
end
remove_user(user) click to toggle source
# File lib/ssh4iot.rb, line 23
def remove_user(user)
  sh "deluser --remove-home #{user}"
end
reverse_proxy(port,key_path) click to toggle source
# File lib/ssh4iot.rb, line 37
def reverse_proxy(port,key_path)
  # https://www.rubydoc.info/github/net-ssh/net-ssh/Net/SSH
  # https://net-ssh.github.io/ssh/v1/chapter-3.html#s2

  Net::SSH.start(
      parameters['host'], parameters['user'],
      :keys => [ key_path ],
  #:compression => "zlib"
  ) do |session|
    session.forward.remote(params["port"],  parameters['host'], port)
    session.loop { true }
  end

end
start_server() click to toggle source
# File lib/ssh4iot.rb, line 19
def start_server
  sh "docker build -t #{params['container']} . && docker run -d --restart unless-stopped -v ~/ssh4iot:/home --expose 80 -p #{parameters['port']}:22 -it --name #{params['container']} --label traefik.backend=ssh4iot --label traefik.frontend.rule=\"Host:#{parameters['host']};PathPrefixStrip:/ssh4iot\" --label traefik.frontend.entryPoints=http,https #{params['container']} sh"
end