公司在异地有分公司。那边要加入域。然后权限等等。。。还有就是ruby学习一下。。。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
106
107
|
require
'net/ldap'
require
'net/telnet'
require
'net/http'
require
'openssl'
require
'base64'
require
'highline/import'
#隐藏输入字符 gem install highline
ALG
=
'DES-EDE3-CBC'
KEY
=
"masdsdff"
#你的密钥 只能8位
DES_KEY
=
"nseewW3E"
#任意固定的值
def
check_pass(auth)
puts
"Input Your Username"
username =
STDIN
.gets.chomp
password = ask(
"Input Your Password"
) { |q| q.echo =
"*"
}
ldap = Net::
LDAP
.
new
ldap.host =
"fw.163.net"
ldap.port =
389
result = ldap.bind_as(
:base
=>
"ou=People,dc=163,dc=net"
,
:filter
=>
"(uid=#{username}))"
,
:password
=> password
)
if
result
auth.include?(username) ?
true
:
false
else
false
end
end
def
check_network()
begin
Net::Telnet:
:new
(
"Host"
=>
"ad.163.net"
,
"Port"
=>
80
,
"Telnetmode"
=>
false
)
rescue
puts
"Can't Connect AD, Please Check Network."
sleep
10
exit
end
end
def
check_hostname()
if
ENV
[
"COMPUTERNAME"
].downcase.scan(
"hw"
).count >
0
puts
"Please Change Hostname And Restart PC"
sleep
10
exit
end
end
def
usage()
help=<<
EOF
Usage: script username password
EOF
puts help
end
def
add_domain(secret)
#'user1|user2||domain admin|domain admin pass|current pass'
result = `netdom.exe join
#{ENV["COMPUTERNAME"]} /domain:163.net /userd:#{secret[2].strip} /passwordd:#{secret[3].strip} /usero:#{ENV["USER"]} /passwordo:#{secret[4].strip}`
result.scan(
"success"
).count >
0
?
true
:
false
end
def
encode(str)
des = OpenSSL::Cipher::Cipher.
new
(
ALG
)
des.pkcs5_keyivgen(
KEY
,
DES_KEY
)
des.encrypt
cipher = des.update(str)
cipher << des.final
return
Base64.encode64(cipher)
#Base64编码,才能保存到数据库
end
def
decode(str)
str = Base64.decode64(str)
des = OpenSSL::Cipher::Cipher.
new
(
ALG
)
des.pkcs5_keyivgen(
KEY
,
DES_KEY
)
des.decrypt
des.update(str) + des.final
end
#-----------------------
check_network()
#usage() 本来用参数,现在改输入
secret = decode(Net::
HTTP
.get(
URI
(
'http://fw.163.net/auth.dat'
)).chomp).split(
"|"
)
if
check_pass(secret)
check_hostname()
if
add_domain(secret)
puts
"Add users to the local administrators group? (Y|N)"
if
STDIN
.gets.scan(/[yY]/).count >
0
puts
"Please enter a user to join the administrators group"
`net localgroup administrators
163
.net\\
#{STDIN.gets.chomp} /add`
end
puts
"Join a domain successfully"
else
puts
"Join a domain fail"
end
sleep
10
exit
else
puts
"1: username or password err; 2: unauthorized"
sleep
10
exit
end
|
然后用 ocra 编译为exe (ocra 直接 gem install ocra, 这个感觉比exerb、rubyscript2exe好)
同时程序需要 netdom.exe netapi32.dll
本文转自 nonono11 51CTO博客,原文链接:http://blog.51cto.com/abian/1416354,如需转载请自行联系原作者