wtnabe (owner)

Revisions

gist: 79379 Download_button fork
public
Public Clone URL: git://gist.github.com/79379.git
Embed All Files: show embed
ssh_and_su_example.rb #
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
# -*- coding: utf-8 -*-
 
require 'pty'
require 'expect'
 
=begin
 
ssh & su example for expect.rb
 
variables
 
c : config hash
cmd : command what you want to exec
(you can recieve with Highline.ask)
 
=end
 
def exec_as_root( cmd, c )
  $expect_verbose = true
  host = c['prompt_host']
 
  PTY.spawn( ssh_cmdline( c ) ) { |r, w|
    w.sync = true
 
    # store host info ?
    r.expect( /\(yes\/no\)\?/, 3 ) { |s|
      w.puts "yes"
    }
    r.expect( /password: $/ ) {
      w.puts c['pass']
      sleep 2
    }
    r.expect( "#c['user']@#{host}" ) {
      w.puts "su"
    }
    r.expect( /^Password: $/ ) {
      w.puts c['root_pass']
    }
    r.expect( "root@#{host}" ) {
      w.puts cmd
    }
    r.expect( "root@#{host}" ) {
      w.puts "exit"
    }
  }
end
 
def ssh_cmdline( c )
  return "ssh -p #{c['port']} #{c['user']}@#{c['host']}"
end