Module: MCMD::NetTools

Defined in:
lib/nysol/mnettools.rb

Constant Summary collapse

@@ftpUse =
true
@@retry_scp =
5

Class Method Summary collapse

Class Method Details

.cmdRun(ip, uid, cmd, secret = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nysol/mnettools.rb', line 50

def self.cmdRun(ip,uid,cmd,secret=nil)
  sts = false
  @@retry_scp.times{|i|
    if secret then
      sts = system("ssh -i #{secret} #{uid}@#{ip} '#{cmd}'")
    else
      sts = system("ssh #{uid}@#{ip} '#{cmd}'")
    end
    break if sts
    sleep 2**i
    MCMD::warningLog("SSH CMDRUN RETRY(#{i+1}/#{@@retry_scp})")   

  }
  MCMD::warningLog("ERROR by SSH ") unless sts     
end

.cmdRun_b(ip, uid, cmd, tag = "", secret = nil) ⇒ Object

バックグランド実行



67
68
69
70
71
72
73
74
75
# File 'lib/nysol/mnettools.rb', line 67

def self.cmdRun_b(ip,uid,cmd,tag="" ,secret=nil)
  sts = false
  if secret then
    sts = system("ssh -i #{secret} #{uid}@#{ip} 'nohup #{cmd}'")
  else
    sts = system("ssh #{uid}@#{ip} 'nohup #{cmd}'")
  end
  MCMD::warningLog("ERROR by SSH(nohub)") unless sts     
end

.config_infoObject



46
47
48
# File 'lib/nysol/mnettools.rb', line 46

def self.config_info
  puts "R #{@@retry_scp} F #{@@ftpUse}"
end

.ftp(ip, uid, from, to, secret) ⇒ Object

Ftpファイル送信



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/nysol/mnettools.rb', line 109

def self.ftp(ip,uid,from,to,secret)
  sts =false
  unless File.file?(from) then
    raise "File #{from} not found. "
  end
  if to =~ /\/$/ then
    cmdRun(ip,uid,"mkdir -p #{to}")
  else
    cmdRun(ip,uid,"mkdir -p #{File.dirname(to)}")
  end  
  temp=MCMD::Mtemp.new
  fn = temp.file
  File.open("#{fn}","w"){|fw|
    fw.puts "open #{ip}"
    fw.puts "user #{uid} #{secret}"
    fw.puts "prompt"
    fw.puts "bi"
    fw.puts "put #{from} #{to}"
    fw.puts "bye"
  }

  @@retry_scp.times{
    sts = system("ftp -n < #{fn} > /dev/null")
    #sts = system("ftp -n < #{fn} ")
    break if sts
    sleep 2**i
    MCMD::warningLog("FTP SEND RETRY(#{i+1}/#{@@retry_scp})")   
  }
  MCMD::warningLog("send ERROR by ftp") unless sts
end

.ftp_r(ip, uid, from, to, secret) ⇒ Object

ftpファイル受信

MCMD::NetTools.ftp_r(ip,pcinfo[0],path.addSuffix(“/#i”),dicName,pcinfo[1])



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/nysol/mnettools.rb', line 172

def self.ftp_r(ip,uid,from,to,secret)
  tod = to
  if to =~ /\/$/ then
    system("mkdir -p #{to}")
  else       
    system("mkdir -p #{File.dirname(to)}")
    tod = File.dirname(to)
  end  
  temp=MCMD::Mtemp.new
  fn = temp.file
  File.open("#{fn}","w"){|fw|
    fw.puts "open #{ip}"
    fw.puts "user #{uid} #{secret}"
    fw.puts "prompt"
    fw.puts "lcd #{tod}"
    fw.puts "bi"
    from.each{|fr|
      fw.puts "cd #{File.dirname(fr)}"
      fw.puts "mget #{File.basename(fr)}"
    }
    fw.puts "bye"
  }
  @@retry_scp.times{|i|
    sts = system("ftp -n < #{fn} > /dev/null")
    break if sts
    sleep 2**i
    MCMD::warningLog("FTP RECV RETRY(#{i+1}/#{@@retry_scp})")   
  }
end

.recv(ip, uid, from, to, secret) ⇒ Object



202
203
204
205
206
207
208
# File 'lib/nysol/mnettools.rb', line 202

def self.recv(ip,uid,from,to,secret)
  if @@ftpUse then 
    ftp_r(ip,uid,from,to,secret)
  else
    scp_r(ip,uid,from,to)     
  end
end

.scp(ip, uid, from, to, secret = nil) ⇒ Object

SCPファイル送信



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/nysol/mnettools.rb', line 79

def self.scp(ip,uid,from,to,secret=nil)
  sts =false
  unless File.file?(from) then
    raise "File not found."
  end
  if to =~ /\/$/ then
    cmdRun(ip,uid,"mkdir -p #{to}",secret)
  else       
    cmdRun(ip,uid,"mkdir -p #{File.dirname(to)}",secret)
  end  
  if secret then
    @@retry_scp.times{|i|
      sts = system("scp -i #{secret} -C #{from} #{uid}@#{ip}:#{to}")
      break if sts
      sleep 2**i
      MCMD::warningLog("SCP SEND RETRY(#{i+1}/#{@@retry_scp})")    
    }
  else
    @@retry_scp.times{|i|
      sts = system("scp -C #{from} #{uid}@#{ip}:#{to}")
      break if sts
      sleep 2**i
      MCMD::warningLog("SCP SEND RETRY(#{i+1}/#{@@retry_scp})")    
    }
  end
  MCMD::warningLog("send ERROR by scp") unless sts
end

.scp_r(ip, uid, from, to, secret = nil) ⇒ Object

SCPファイル受信



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/nysol/mnettools.rb', line 153

def self.scp_r(ip,uid,from,to,secret=nil)
  if to =~ /\/$/ then
    system("mkdir -p #{to}")
  else       
    system("mkdir -p #{File.dirname(to)}")
  end  
  from.each{|fn|
    if secret then
      system("scp -i #{secret} -C #{uid}@#{ip}:#{fn} #{to}> /dev/null")
    else
      system("scp -C #{uid}@#{ip}:#{fn} #{to} > /dev/null")
    end
  }
end

.send(ip, uid, from, to, secret) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/nysol/mnettools.rb', line 140

def self.send(ip,uid,from,to,secret)
  if @@ftpUse then 
    ftp(ip,uid,from,to,secret)
  else
    scp(ip,uid,from,to)     
  end
end