Module: Ssh4iot

Defined in:
lib/ssh4iot.rb,
lib/ssh4iot/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#add_user(user) ⇒ Object



26
27
28
# File 'lib/ssh4iot.rb', line 26

def add_user(user)
  sh "useradd #{user} && mkdir /home/#{user} && chown -R #{user}#{user} /home/#{user}"
end

#clear_dockerObject



16
17
18
# File 'lib/ssh4iot.rb', line 16

def clear_docker
  sh "docker rm -f #{params['container']}"
end

#keygenObject



30
31
32
# File 'lib/ssh4iot.rb', line 30

def keygen
  sh "ssh-keygen -q -t rsa -f ~/.ssh/id_rsa -N ''"
end

#register_keyObject



33
34
35
# File 'lib/ssh4iot.rb', line 33

def register_key
  sh "cat ~/.ssh/id_rsa.pub | ssh [email protected] \"mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >>  ~/.ssh/authorized_keys\""
end

#remove_user(user) ⇒ Object



23
24
25
# File 'lib/ssh4iot.rb', line 23

def remove_user(user)
  sh "deluser --remove-home #{user}"
end

#reverse_proxy(port, key_path) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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_serverObject



19
20
21
# 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