ruby自动加域脚本

简介:

公司在异地有分公司。那边要加入域。然后权限等等。。。还有就是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,如需转载请自行联系原作者
相关文章
|
自然语言处理 Ruby
Ruby脚本解释流程
Ruby提供了ripper这个工具,我们可利用此工具分析ruby代码,方便定位性能问题。 以下内容都是以如下代码为例: x > 100 ? 'foo' : 'bar' Ruby在执行程序前会将代码转化为更加结构化的语句 第一步:将代码切分为一个一个的词条 ripper的tokenize的方法可以将代
151 0
|
测试技术 C++ Ruby
C++程序中嵌入Ruby脚本系统
Ruby,一种为简单快捷面向对象编程(面向对象程序设计)而创的脚本语言,由日本人松本行弘(まつもとゆきひろ,英译:Yukihiro Matsumoto,外号matz)开发,遵守GPL协议和Ruby License。
1562 0
|
Ruby
(转)分享一个技巧,利用批处理调用ruby脚本(可能你为路径苦恼)
#关闭命令显示 @echo off #提示信息 echo Now,listing the controller,please not shutdown the DOS File! #切换到当前路径,.
591 0
|
测试技术 数据安全/隐私保护 Ruby
自己动手破解rar密码-ruby脚本实现
破解密码,上策是社工的方式,下策则是暴力破解了。这里使用暴力破解,字典文件放在txt中;通过调用winrar的相关参数进行操作。经过测试,当字典中密码为100个时,破解耗时8秒(个人配置:i5-3210 2.
935 0
|
2月前
|
Ruby
|
1月前
|
数据采集 Web App开发 数据处理
Ruby网络爬虫教程:从入门到精通下载图片
Ruby网络爬虫教程:从入门到精通下载图片
|
2月前
|
JSON 数据格式 Ruby
|
2月前
|
JSON Ubuntu Linux