升級列表自動生成腳本-創(chuàng)新互聯(lián)

一個自動生成升級文件列表的腳本,可以運行在WIN或者linux

創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供靖遠網站建設、靖遠做網站、靖遠網站設計、靖遠網站制作等企業(yè)網站建設、網頁設計與制作、靖遠企業(yè)網站模板建站服務,10余年靖遠做網站經驗,不只是建網站,更提供有價值的思路和整體網絡服務。

要判斷中間的版本號,取大的

文件最終樣式如下

productname:ABCikn7
version:2.23.81
uname:ABCikn7_2.23.81_20120101.upt
usize:3598290
dname:ABCikn7_2.23.81_20120101.dd5
dsize:33
url1:ftp://ftp.163.com/down/

productname:xx001A
version:4.0.4
uname:xx001A_4.0.4_20131114.upt
usize:14008320
dname:xx001A_4.0.4_20131114.dd5
dsize:40
url1:ftp://ftp.163.com/down/
url2:ftp://ftp.265.com/down/

#!/usr/bin/ruby
require 'net/ftp'
ip = ["1.2.3.4","2.11.12.2"] #server IP add
url = ["url1:ftp://ftp.163.com/down/","url2:ftp://ftp.265.com/down/"]
update_file = ["e:\\update.txt", "/tmp/update.dat"] #Win or Linux
file_hash = {}
text = ''
def os_family 
  case RUBY_PLATFORM 
    when /ix/i, /ux/i, /gnu/i, 
         /sysv/i, /solaris/i, 
         /sunos/i, /bsd/i 
      "unix" 
    when /win/i, /ming/i 
      "windows" 
    else 
      "other" 
  end 
end
if os_family == "unix"
    text_file = update_file[1]
elsif os_family == "windows"
    text_file = update_file[0]
else puts "unkown os"
    Process.exit(1)
end
def file_list(ip)
    #get ftp dir list
    files = []
    ip.size.times{|x|
        ftp = Net::FTP.open(ip[x])
        ftp.login('username','password')
        ftp.passive = true
        files << ftp.list('down')
        ftp.close
    }
    return files
end
def key_values(type,n,name_size) #get type_x.x.x_date.upt
    dd, up = '', ''
    if n == 1
        regexp = "#{type}_\\d+\\.\\d+\\.\\d+_\\d+."
    else
        regexp = "#{type}_\\d+\\."
    end
    2.times {
        name_size.each_key { |m|
            if /#{regexp}dd5/.match(m)
                dd = m.to_s
            elsif /#{regexp}upt/.match(m)
                up = m.to_s
            end
        }
    }
    return dd, name_size[dd], up, name_size[up]
end
def formatstr(dd,dd_size,up,up_size)   
    type, ver = '', ''
    tmp = []
    tmp = dd.split('_')
    type = tmp[0]
    ver = tmp[1]
msg =<<EOF
productname:#{type}
version:#{ver}
uname:#{up}
usize:#{up_size}
dname:#{dd}
dsize:#{dd_size}
EOF
return msg
end
def sorter(type,n,name_size)
    tmp = []
    a, a1, b, b1 = '', '', '', ''
    name_size.select {|x,y| tmp<<x if /#{type}_\d+\.\d+\.\d+_\d+\.upt/.match(x)}
    a = tmp[0].split('_')[1].split('.')
    a1 = "%d%04d%04d" % [a[0],a[1],a[2]]
    (n-1).times {
        b = tmp[n-1].split('_')[1].split('.')
        b1 = "%d%04d%04d" % [b[0],b[1],b[2]]
        if a1.to_i < b1.to_i
            a1 = b1
        end
    }
    return type + "_" + a1[-9..0].to_i.to_s + "." + a1[-8..-5].to_i.to_s + "." + a1[-4..-1].to_i.to_s
end
def update_text_create(list,x,url) 
    type = {}
    tmp_hash = {}
    log = ''
    name_size = {}
    list.each {|a|
        #get type hash
        tmp_array = a.split(' ')
        if /^\.|^\.\./.match(tmp_array[8]).nil? and /^total/.match(tmp_array[0]).nil?
            if type.has_key?(tmp_array[8].split('_')[0])
                type[tmp_array[8].split('_')[0]] = type[tmp_array[8].split('_')[0]] + 1
            else
                type[tmp_array[8].split('_')[0]] = 1
            end
            name_size[tmp_array[8]] = tmp_array[4]
        end
    }
        #get type
        type.each{|b,c|
            if c/2 == 1 #only one type
                dd5, dd_size, upt, upt_size = key_values(b,c/2,name_size)
                tmp_hash[formatstr(dd5, dd_size, upt, upt_size)] = url[x]
            else   
                dd5, dd_size, upt, upt_size = key_values(sorter(b,c/2,name_size),c/2,name_size)
                tmp_hash[formatstr(dd5, dd_size, upt, upt_size)] = url[x]         
            end
        }
        return tmp_hash
end
list = file_list(ip)
list.size.times {|x|
   file_hash[x] = update_text_create(list[x],x,url)
}
file_hash[0].each {|x,y|
    if file_hash[1].has_key?(x)
        file_hash[0][x] = y + "\n" + file_hash[1][x]
        file_hash[1].delete(x)
    end
}
def writefile(str,filename)
aFile = File.new(filename,"w")
       aFile.puts str
aFile.close
end
file_hash[0].merge(file_hash[1]).each {|x,y|
    text = text + "\n" + x + y + "\n"
}
writefile(text,text_file)

創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網絡助力業(yè)務部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準確進行流量調度,確保服務器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務器買多久送多久。

文章標題:升級列表自動生成腳本-創(chuàng)新互聯(lián)
URL網址:http://muchs.cn/article28/dshocp.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網站營銷、服務器托管、虛擬主機網站策劃、網站導航

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

手機網站建設