vipnet_getter 0.2 → 0.3

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 +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4be22db591c17046ec0bb3cd8e5fc814fee22f9d
4
- data.tar.gz: 3690e4ab38c0e829da2b92e54dbe0497ef95e7bb
3
+ metadata.gz: 02791c509ed3cb9d56085ff5b6181e3f2d1c3989
4
+ data.tar.gz: b3844f588742eebe97a6daf5d195ca7a4db3f6ad
5
5
  SHA512:
6
- metadata.gz: ad3351234798ff742f846ee513b649261a25593e0b81e80627ce44f56ec6e48cf4e3abba38b6f21842bd297be2447863824f6addb03bedb61e019cbe545f46f8
7
- data.tar.gz: 96039f28b78e26d21e817d0eb230e4ce806e0b001454135c00dba407579aed54f2960004576c37d99e45106802808d71814a53b941e9e126f5027049bf87c50f
6
+ metadata.gz: 0b0dcf0252d31b90930080fe7206fe30f4f5464542429ae371d5ce9669498a5f558f0b030ab32ba460b8d2db9599d38407afb88298e0c210ea7d2da0f7adc0b9
7
+ data.tar.gz: facdced06f47a81cd910b98c9c8020e3eb28dca353a39aa12d0315d8b34825046d60fb13235c56632d2418d38282d8a3a81f37e58e87d81fa590f1353b269749
@@ -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.2'
4
+ version: '0.3'
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-12-04 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
@@ -52,7 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
52
  version: '0'
53
53
  requirements: []
54
54
  rubyforge_project:
55
- rubygems_version: 2.5.1
55
+ rubygems_version: 2.6.6
56
56
  signing_key:
57
57
  specification_version: 4
58
58
  summary: Gem for getting data from ViPNet™ products