Skip to content

Instantly share code, notes, and snippets.

@metasta
Created March 31, 2010 06:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metasta/349996 to your computer and use it in GitHub Desktop.
Save metasta/349996 to your computer and use it in GitHub Desktop.
Ruby Scripts
#!/usr/bin/ruby -
def main
url = 'http://dyn.value-domain.com/cgi-bin/dyn.fcg?'
dm, hs, pw = 'example.com', 'www', 'xxxx'
ip = `wget -q -O - #{url}ip`
require 'optparse'
ARGV.options {|opt|
opt.on('-d', '--domain=DOMAIN', 'domain (example.com)' ){|v| dm = v}
opt.on('-h', '--host=HOSTNAME', 'host (www, www2, etc.)'){|v| hs = v}
opt.on('-p', '--password=PASS', 'password' ){|v| pw = v}
opt.on('-i', '--ip=IP' , 'ip address' ){|v| ip = v}
opt.parse!
}
r = `wget -q -O - "#{url}d=#{dm}&p=#{pw}&h=#{hs}&i=#{ip}"`
abort r if r != "status=0\nOK\n"
end
main
#!/usr/bin/ruby -Ku
def compress
l = Dir.glob('cstrike/demos/*.dem').reverse
l.shift
l.each {|f|
mtime = File.mtime(f)
z = f.sub(/dem$/,'zip')
`zip -mq #{z} #{f}`
File.utime(mtime, mtime, z)
}
end
def delete_old
`find ./cstrike/demos -name '*.zip' -mtime +20 -print0 | xargs -0r rm`
end
def print_list
prefix = 'sv_'
url = 'http://hltv.haiz.in/' + File.basename(Dir.pwd).sub(prefix, '') + '/'
style = ' style="color:#aaaaaa"'
l = Dir.glob('cstrike/demos/*.zip').sort!{|a,b| File.mtime(b)<=>File.mtime(a)}
date = head = File.mtime(l.first).strftime('%Y-%m-%d')
tail = File.mtime(l.last).strftime('%Y-%m-%d')
o = Array.new; sum = 0
l.each {|f|
prev = date
m, n, s = File.mtime(f), File.basename(f), File.size(f) / 1048576.0
date, t = m.strftime('%Y-%m-%d'), m.strftime('%H:%M')
o.push('</p><h4>' + date + '</h4><p>') if date != prev
o.push('%s%s %s <a href="%s%s"%s>%s</a> (%.2f MB)%s<br />' %
[ s < 1 ? '<span'+style+'>' : '', date, t, url, n,
s < 1 ? style : '', n, s, s < 1 ? '</span>' : ''])
sum += s
}
puts '<p>%s &rarr; %s (%s files, %.2f MB)</p>' % [tail, head, l.size, sum]
puts '<p>', o, '</p>'
end
def main
require 'optparse'
z = d = l = false
ARGV.options {|opt|
opt.on('-z', '--zip', 'compress demo files'){|v| z = true}
opt.on('-d', '--delete', 'delete old files'){|v| d = true}
opt.on('-l', '--list', 'list zip files'){|v| l = true}
opt.parse!
}
l = true unless (z || d || l)
abort $0 + ': directory "./cstrike/demos" not found' unless File.directory?("./cstrike/demos")
compress if z
delete_old if d
print_list if l
end
main
#!/usr/bin/ruby -Ku
def print_prefix
print <<'HEADER'
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
<head>
<meta http-equiv="Content-Type"
content="application/xhtml+xml; charset=UTF-8" />
<title>Message of the Day</title>
<style type="text/css">
body {
margin-left: 8px;
background: #000000;
font-family: 'Meiryo', 'Verdana', 'Tahoma', sans-serif;
font-size: 80%;
color: #FFB000;
line-height: 1.2em;
}
h2, h3, h4 {
font-weight: normal;
font-size: 90%;
}
a { color: #FFFFFF; text-decoration: underline; }
p { margin: 0 0 16px 16px; }
ul { margin: 0; list-style-type: none; font-size: 67%;}
dl { margin: 0; }
dt, dd { margin-left:32px; font-size: 85%; }
h1, p#msg {
margin: 24px 0 12px 0;
font-weight: normal;
font-size: 200%;
line-height: 1.2em;
}
</style>
</head>
<body>
HEADER
end
def print_headline(headline)
puts '<p id="msg">' + headline.to_s + '</p>'
end
def print_desc(desc, url)
puts '<p>'+desc.to_s+'<br /><a href="'+url.to_s+'">'+url.to_s+'</a></p>'
end
def print_rule(rule)
puts '<dl><dd>Rule: '+ rule.to_s + '</dd></dl>'
end
def print_maps
m = 'Maps:'
File.open("cstrike/mapcycle.txt").each {|l|
m += ' '+l.chomp.gsub(/^de_/,'')
}
puts '<dl><dd>' + m + '</dd></dl>'
end
def print_recent(feedurl)
require 'rexml/document'
require 'time'
doc = REXML::Document.new `wget -q -O - '#{feedurl}'`
puts '<dl><dt>Recent:</dt></dl><ul>'
doc.elements.each('///item/title'){ |t|
l = t.next_element
d = Time.parse(l.next_element.next_element.text).strftime('%Y-%m-%d')
puts '<li>' + d + ' <a href="' + l.text + '">' + t.text + '</a></li>'
}
puts '</ul>'
end
def print_suffix
puts "</body>\n</html>"
end
def main
abort $0 + ': please run in HLDS root dir' unless File.directory?("./cstrike")
require 'optparse'
h = "Welcome!"
d = "This is the server playing Counter-Strike. Enjoy!"
u = "http://counter-strike.net/"
r = n = f = nil
ARGV.options {|opt|
opt.on('-h','--headline=VAL' ,'headline (large font)' ){|v|h=v}
opt.on('-d','--description=VAL','description (keep simple)'){|v|d=v}
opt.on('-u','--url=VAL' ,'your site\'s full url' ){|v|u=v}
opt.on('-r','--rule=VAL' ,'game rule (keep simple)' ){|v|r=v}
opt.on('-n','--no-mapcycle' ,'do not show maps' ){|v|n=true}
opt.on('-f','--feed-url=VAL' ,'your update history RSS' ){|v|f=v}
opt.parse!
}
print_prefix
print_headline(h)
print_desc(d, u)
print_rule(r) unless r
print_maps unless n
print_recent(f) unless f
print_suffix
end
main
#!/usr/bin/ruby -Ku
#
# locate はOR検索なので、スペースでAND検索するようなコマンドがほしい。つまり、
#
# $ search hoge fuga foo bar ... (a)
#
# を実行して
#
# $ locate hoge | grep fuga | grep foo | grep bar ... (b)
#
# と同じ効果が得られるようなコマンドを作りたい。
# rubyで(b)のシェルスクリプトを自動生成して、その後そのスクリプトを実行することにする。
#
# ただし、(b)の形のままでは不十分。
# 今ある画像ファイルを探して "one" "two" "three" の3つの単語で検索すれば
#
# /home/me/Pictures/three_two_one_zero.jpg
#
# は確かに見つけられるが、その他にも
#
# /home/zone/Documents/three/two/test.txt
#
# など、どうでもいいものがたくさん引っかかる。
# これを防ぐには、検索対象をbasenameだけに限定すればよい。つまり
#
# $ locate -b 'one' | grep 'two[^/]*$' | grep 'three[^/]*$' ... (c)
#
# こんな感じ。
#
abort(File.basename $0 + ": 探すパターンが指定されていません") if ARGV.size < 1
q = ARGV.sort{|a, b| b.length <=> a.length}
LCT = "/usr/bin/locate -bi -- '" + q[0] + "'"
GRP = "/bin/grep -i -- '" + q.join("[^/]*$' | /bin/grep -i -- '") + "[^/]*$'"
HED = "/usr/bin/head -n 500"
# DEBUG
# abort("#{LCT} | #{GRP} | #{HED}")
system "#{LCT} | #{GRP} | #{HED}"
exit $?
#!/usr/bin/ruby -Ku
def a2s_player(s, host = 'localhost', port = 27015)
s.send([-1, 'U', -1].pack('lal'), 0, host, port)
i = s.recv(1400).unpack('x5l').pop()
s.send([-1, 'U', i].pack('lal'), 0, host, port)
i, r = s.recv(1400).unpack('x5cH*')
d = [r].pack('H*').unpack('cZ*lf' * i)
a = Array.new()
i.times {|j| k = 4*j
a[j] = {'name'=>d[k+1], 'score'=>d[k+2], 'time'=>d[k+3]}
}
return a
end
def a2s_info(s, host = 'localhost', port = 27015)
s.send([-1, 'T', 'Source Engine Query'].pack('laZ*'), 0, host, port)
h = Hash::new
( h['protocol'], h['name'], h['map'], h['dir'], h['game'],
h['appid'], h['player'], h['max'], h['bot'], h['dedicate'],
h['os'], h['password'], h['secure'], h['version'] ) =
s.recv(1400).unpack('x5CZ*Z*Z*Z*SC3aaC2Z*x*')
return h
end
def print_player(l)
l.sort! {|a,b| b['score'] <=> a['score'] }
puts ' # Name Score Time'
l.size.times {|i|
n, s, t = [ l[i]['name'], l[i]['score'], l[i]['time'] ]
puts '%2d %-32s %3d %5d:%02d' % [ i+1, n, s, t / 60, t % 60]
}
end
def print_info(h)
h.each_pair {|k, v| puts '%8s: %s' % [k, v.to_s] }
end
def main
require 'optparse'
player = info = false
ARGV.options {|opt|
opt.on('-p', '--player', 'show player list (default)'){|v| player = true }
opt.on('-i', '--info', 'show server info' ){|v| info = true }
opt.banner += ' [host:port]'
opt.parse!
}
player = true unless (player || info)
require 'socket'
server = (ARGV.size > 0) ? ARGV : ['localhost:27015']
server.each {|addr|
puts addr if server.size > 1
UDPSocket.open {|s|
print_player( a2s_player(s, *addr.split(':')) ) if player
print_info( a2s_info(s, *addr.split(':')) ) if info
}
}
end
main
#!/usr/bin/ruby -
print "document.write('"
while l = gets
l.gsub!("'"){"\\'"}
l.gsub!('"'){'\\"'}
print l.gsub(/\s+/,' ')
end
puts "');"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment