-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexec.rb
More file actions
executable file
·54 lines (48 loc) · 1.24 KB
/
Copy pathexec.rb
File metadata and controls
executable file
·54 lines (48 loc) · 1.24 KB
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
46
47
48
49
50
51
52
53
54
#!/usr/bin/env ruby
require 'libssh'
GC.stress = true
puts "libssh #{LibSSH::LIBSSH_VERSION}"
puts LibSSH.version
session = LibSSH::Session.new
# session.log_verbosity = :debug
session.host = 'barkhorn'
session.parse_config
session.add_identity('%d/id_ed25519')
session.connect
if session.server_known == LibSSH::SERVER_NOT_KNOWN
pubkey = session.get_publickey
print "Connect to #{pubkey.type_str} #{pubkey.sha1_hex} ? (y/N) "
$stdout.flush
yesno = $stdin.gets.chomp
if yesno != 'y'
raise
end
session.write_knownhost
puts 'Wrote known_hosts'
elsif session.server_known != LibSSH::SERVER_KNOWN_OK
raise
end
session.userauth_none
list = session.userauth_list
unless list.include?(:publickey)
raise "publickey is unsupported: #{list}"
end
if session.userauth_publickey_auto != LibSSH::AUTH_SUCCESS
raise 'authorization failed'
end
bufsiz = 16384
channel = LibSSH::Channel.new(session)
channel.open_session do
channel.request_exec('ps auxf')
until channel.eof?
LibSSH::Channel.select([channel], [], [], nil)
out = channel.read_nonblocking(bufsiz)
if out && !out.empty?
$stdout.write(out)
end
err = channel.read_nonblocking(bufsiz, stderr: true)
if err && !err.empty?
$stderr.write(err)
end
end
end