Module: RemoteSh::SshHelper

Defined in:
lib/remote_sh/ssh_helper.rb

Class Method Summary collapse

Class Method Details

.attach(host, dir) ⇒ Object



8
9
10
# File 'lib/remote_sh/ssh_helper.rb', line 8

def attach(host, dir)
  system("ssh -t #{host} \"cd #{dir}; exec \$SHELL -l\"")
end

.close_local_port(host, port) ⇒ Object



42
43
44
# File 'lib/remote_sh/ssh_helper.rb', line 42

def close_local_port(host, port)
  system("ssh -q -S /tmp/remote_local_pf_#{port} -O exit #{host}")
end

.close_local_socket(host, socket) ⇒ Object



50
51
52
# File 'lib/remote_sh/ssh_helper.rb', line 50

def close_local_socket(host, socket)
  system("ssh -q -S /tmp/remote_local_pf_#{Digest::SHA1.hexdigest(socket)} -O exit #{host}")
end

.close_port(host, port) ⇒ Object



60
61
62
63
64
# File 'lib/remote_sh/ssh_helper.rb', line 60

def close_port(host, port)
  # normalized_port = normalize_port(port)
  # puts "closing #{port} forwarded to #{normalized_port}"
  system("ssh -q -S /tmp/remote_pf_#{port} -O exit #{host}")
end

.current_ports(host, blacklist_ports) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/remote_sh/ssh_helper.rb', line 17

def current_ports(host, blacklist_ports)
  `ssh #{host} "netstat -tuln | grep LISTEN"`
    .split("\n")
    .map(&:split)
    .map { _1[3].split(':').last }
    .uniq - blacklist_ports
end

.exec(host, dir, args) ⇒ Object



12
13
14
15
# File 'lib/remote_sh/ssh_helper.rb', line 12

def exec(host, dir, args)
  cmd = "cd #{dir}; #{args.join(' ')}"
  system("ssh -t -q #{host} \"#{cmd}\"")
end

.forward_local_port(host, port) ⇒ Object



37
38
39
40
# File 'lib/remote_sh/ssh_helper.rb', line 37

def forward_local_port(host, port)
  normalized_port = normalize_port(port)
  system("ssh -q -f -N -M -S /tmp/remote_local_pf_#{port} -R #{port}:localhost:#{normalized_port} #{host}")
end

.forward_local_socket(host, socket) ⇒ Object



46
47
48
# File 'lib/remote_sh/ssh_helper.rb', line 46

def forward_local_socket(host, socket)
  system("ssh -f -N -M -S /tmp/remote_local_pf_#{Digest::SHA1.hexdigest(socket)} -R #{socket}:#{socket} #{host}")
end

.forward_port(host, port) ⇒ Object



54
55
56
57
58
# File 'lib/remote_sh/ssh_helper.rb', line 54

def forward_port(host, port)
  normalized_port = normalize_port(port)
  # puts "forwarding #{port} to #{normalized_port}"
  system("ssh -f -N -M -S /tmp/remote_pf_#{port} -L #{normalized_port}:localhost:#{port} #{host}")
end

.normalize_port(port) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/remote_sh/ssh_helper.rb', line 25

def normalize_port(port)
  port = port.to_i

  if port == 80 || port == 443
    port += 8000
  elsif port <= 1024
    port += 10000
  end

  port.to_s
end