-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwrite.rb
More file actions
executable file
·54 lines (47 loc) · 1.27 KB
/
Copy pathwrite.rb
File metadata and controls
executable file
·54 lines (47 loc) · 1.27 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
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('sed -e s/foo/bar/')
channel.write("foobarbaz\n")
channel.send_eof
until channel.eof?
stdout_avail = channel.poll(timeout: 1)
if stdout_avail && stdout_avail > 0
$stdout.write(channel.read(bufsiz))
end
stderr_avail = channel.poll(stderr: true, timeout: 1)
if stderr_avail && stderr_avail > 0
$stderr.write(channel.read(bufsiz, stderr: true))
end
end
end