vipnet_getter 0.11 → 0.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/vipnet_getter.rb +52 -38
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 53175c56a11abe74e7481275a13ca3070e37e050
4
- data.tar.gz: b2e9f65e4dfdc39f3861ef460a3bb27ddd026ec6
3
+ metadata.gz: f6afa6e90cb797a0e96abd0ccd2717549a4120cc
4
+ data.tar.gz: 8e09d23854f0cfa3518f2ff5430ae6a36f71d2bf
5
5
  SHA512:
6
- metadata.gz: 67e0425e2875304e96ec3c1f53e7d5988faaac767187468c879c9ea3e51cf6fd07660dfeaa1b8ee72c71511502045a38c733414d13a5bf0f03e961f4f6fcb6c8
7
- data.tar.gz: de3f14189c75a4c8f5f3b5f56d6c2e4a6f838e4ec948b676c07f3c988d13472fbb3172ff391e012699c21bccc3af3dc838a9b8514d8655a55f13fbe71e924820
6
+ metadata.gz: 6f2a8db6d7f17ac66ea63a2764ca93898201e5182bd0a02811c7f02ce2ef95e45651a638e64b5907aadbd23eacb8a694ca1ec034924f834c74340da650d04277
7
+ data.tar.gz: 631ace20bbdc0708ec467349b21b860c7cf10b8a027f94cd2d05ea11f75e591c5009ef6a0eb213a598ca55022b8e0d7f046e824621f60629caf9fd1054dbe70a
@@ -1,6 +1,8 @@
1
+ # encoding: UTF-8
2
+
1
3
  class VipnetGetter
2
4
  ENTER_KEYCODE = 13
3
- PGDN_KEYCODE = 34
5
+ PGDN_KEYCODE = 34
4
6
 
5
7
  def self.iplirconf(params)
6
8
  hostname = params[:hostname]
@@ -8,7 +10,7 @@ class VipnetGetter
8
10
  username = params[:username] || "vipnet"
9
11
  output_file_path = params[:output_file_path]
10
12
 
11
- # adding digest to known_hosts if needed
13
+ # Adding digest to "known_hosts" if needed.
12
14
  require "ruby_expect"
13
15
  exp_ssh_digest = RubyExpect::Expect.spawn("/usr/bin/ssh #{username}@#{hostname}")
14
16
  exp_ssh_digest.timeout = 3
@@ -20,43 +22,52 @@ class VipnetGetter
20
22
  end
21
23
  end
22
24
 
23
- iplirconf = String.new
24
25
  logged = false
26
+ puts "Logging to #{username}@#{hostname}..."
25
27
 
26
- # logging
27
- exp = RubyExpect::Expect.spawn("/usr/bin/ssh #{username}@#{hostname}")
28
+ # "TERM=xterm" solves "WARNING: terminal is not fully functional" issue when running script with cron
29
+ # TODO: test.
30
+ exp = RubyExpect::Expect.spawn("TERM=xterm luit -encoding KOI8-R /usr/bin/ssh #{username}@#{hostname}")
28
31
  exp.timeout = 3
32
+ iplirconf_content = ""
29
33
  exp.procedure do
30
34
  each do
31
35
  expect /password:\s*$/ do
32
36
  send password
33
- puts "entering password"
37
+ puts "Entering password..."
34
38
  end
35
- # for some reason if pw is not incorrect, script wait for timeout and don't expect ">" for "iplir show config"
36
- # thus, commented code below doesn't work
39
+
40
+ # For some reason, if password is incorrect, script waits for timeout
41
+ # and don't expect ">" for "iplir show config".
42
+ # Thus, commented code below doesn't work
37
43
  # expect /Permission denied, please try again/ do
38
- # puts "permission denied"
44
+ # puts "permission denied"
39
45
  # end
46
+
40
47
  expect />\s*$/ do
41
- send "iplir show config"
42
- # when running with cron, message appears:
43
- # WARNING: terminal is not fully functional
44
- #- (press RETURN)
48
+ command = "iplir show config"
49
+ send command
50
+ puts command
51
+
52
+ # When running with cron, message appears:
53
+ # "WARNING: terminal is not fully functional
54
+ # - (press RETURN)"
55
+ # So we imitate pressing <Enter>.
45
56
  send ENTER_KEYCODE
46
57
  logged = true
47
- puts "iplir show config"
48
58
  end
49
59
  end
50
- return nil if !logged
60
+ return nil unless logged
51
61
 
52
- # reading iplirconf until "default= auto" line appeared
62
+ # Reading iplirconf until "default= .*" or /tunneldefault= .*/ lines appeared.
63
+ # ("default" for vipnet v3 and "tunneldefault" for vipnet v4.)
53
64
  retflag = false
54
65
  while !retflag
55
66
  retval = any do
56
67
  expect /.*:/m do
57
- iplirconf += last_match.to_s
68
+ iplirconf_content << last_match.to_s
58
69
  last_match.to_s.split("\n").each do |line|
59
- if line =~ /default= auto/
70
+ if line =~ /^default=\s.*/ || line =~ /^tunneldefault=\s.*/
60
71
  retflag = true
61
72
  break
62
73
  end
@@ -67,30 +78,33 @@ class VipnetGetter
67
78
  end
68
79
  end
69
80
 
70
- # remove regular trash caused by "send PGDN_KEYCODE"
71
- iplirconf = iplirconf.gsub(":\e[K\r\e[K:\e[K3\b3\e[K4\b4\r\e[K", "")
72
- # remove trash in the beginning
73
- iplirconf = iplirconf.gsub("iplir show config\r\n\e[?1049h\e[?1h\e=\r", "")
74
- # remove trash in the end
75
- iplirconf = iplirconf[/.*default= auto\r\n/m, 0]
76
- # replace \r\n by \n
77
- iplirconf = iplirconf.gsub("\r", "")
78
- # remove trash caused by send ENTER_KEYCODE
79
- iplirconf = iplirconf.gsub(":\x1B[K\x1B[K:\x1B[K1\x081\x1B[K3\x083\x1B[K", "")
80
- # remove trash caused by "terminal is not fully functional" issue when running script with cron
81
- iplirconf = iplirconf.gsub("::3\x083", "")
82
- iplirconf = iplirconf.gsub("4\x084", "")
83
- iplirconf = iplirconf.gsub("iplir show config\nWARNING: terminal is not fully functional\n- (press RETURN):1\x081", "")
84
- iplirconf = iplirconf.gsub("4\x084", "")
85
- # add return if necessary
86
- iplirconf += "\n" if iplirconf[-4..-1] != "\n\n"
81
+ # Encode and remove unsupported characters.
82
+ iplirconf_content.force_encoding("UTF-8")
83
+ .gsub!("\r", "")
84
+ .gsub!(/[^\w\s\p{Cyrillic}=\(\)\[\]:\.\,@\#$%\^\-!]/, "")
85
+
86
+ # Remove trash in the beginning.
87
+ iplirconf_content.gsub!(/iplir show config.*1h=/m, "")
88
+ .gsub!("[7moptvipnetuseriplir.conf[27m[K[K:[K11[K33[K", "")
89
+
90
+ # Remove trash in the middle.
91
+ iplirconf_content.gsub!(/\[K\d+\[K\d+\[K/, "") # [K33[K44[K
92
+ iplirconf_content.gsub!(/:\[K\[K:/, "") # :[K[K:
93
+
94
+ # Remove trash in the end.
95
+ iplirconf_content.gsub!(/\[7m\(END\).*\[K\[K:/, "")
96
+
97
+ # Remove trailing spaces and add carriage return.
98
+ iplirconf_content.strip!
99
+ iplirconf_content << "\n\n"
100
+
87
101
  if output_file_path
88
- File.open(output_file_path, 'w') { |f| f.write(iplirconf) }
102
+ File.open(output_file_path, "w:KOI8-R") { |f| f.write(iplirconf_content) }
89
103
  output_file_path
90
104
  else
91
105
  require "tempfile"
92
- iplirconf_file = Tempfile.new("iplirconf")
93
- iplirconf_file.write(iplirconf)
106
+ iplirconf_file = Tempfile.new("iplirconf", encoding: "KOI8-R")
107
+ iplirconf_file.write(iplirconf_content)
94
108
  iplirconf_file.flush
95
109
  iplirconf_file.path
96
110
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vipnet_getter
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.11'
4
+ version: '0.12'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Morozov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-09 00:00:00.000000000 Z
11
+ date: 2017-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby_expect
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1'
27
- description: Allows to get configuration files like iplir.conf (and more) from ViPNet
27
+ description: Allows to get configuration files like iplir.conf (and more) from ViPNet
28
28
  products such as HW1000; no "enable" and "admin escape" needed.
29
29
  email: ntcomp12@gmail.com
30
30
  executables: []
@@ -55,5 +55,5 @@ rubyforge_project:
55
55
  rubygems_version: 2.6.6
56
56
  signing_key:
57
57
  specification_version: 4
58
- summary: Gem for getting data from ViPNet products
58
+ summary: Gem for getting data from ViPNet products
59
59
  test_files: []