【进阶】在Windows Server上搭建py-kms自助激活系统

[toc]

前些天在内网服务器上搭建了'py-kms',但是发现一个问题,我在开始菜单里的'启动'里面添加上start.vbs之后,必须每次远程登录才能启动,也就是说,如果服务器自动更新重启,那么'py-kms'就无法启动。以下是我进行的各类尝试,作为记录。
前篇文章见这里:在Windows Server上搭建py-kms自助激活系统

尝试一:将py-kms制作为Windows服务(失败)

一、创建python服务

概述

由于py-kms依赖python环境,故而我们需要将python安装为windows服务
参考资料:http://blog.csdn.net/ghostfromheaven/article/details/8604738

步骤

1.Python程序作为Windows服务启动,需要安装pywin32包。
下载地址:http://sourceforge.net/projects/pywin32/files/pywin32/
由于服务器上的python版本为2.7,故我这里选择的是pywin32-221.win32-py2.7.exe这个版本
2.将以下代码另存为PythonService.py

# -*- coding: utf-8 -*-  
# SmallestService.py  
#  
# A sample demonstrating the smallest possible service written in Python.  

import win32serviceutil  
import win32service  
import win32event  
import SocketServer  
import logging  

import sys  
import SocketServer  
import logging  
import httplib, urllib, cookielib, urllib2  

import threading  
import time  

logging.basicConfig(level=logging.DEBUG,  
                    format='%(asctime)s %(levelname)s %(message)s',  
                    filename=r'log234.txt',  
                    filemode='a+')  

import socket, time  
import random, struct  
import urllib  

class SmallestPythonService(win32serviceutil.ServiceFramework):  
    _svc_name_ = "Sologin Monitor Service"  
    _svc_display_name_ = "Sologin Monitor Service"  

    def __init__(self, args):  
        self.threads = []  
        logging.error("add thread on port t")  
        win32serviceutil.ServiceFramework.__init__(self, args)  
        # Create an event which we will use to wait on.  
        # The "service stop" request will set this event.  
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)  

    def SvcStop(self):  
        # Before we do anything, tell the SCM we are starting the stop process.  
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)  
        # And set my event.  

        win32event.SetEvent(self.hWaitStop)  

    def SvcDoRun(self):  
        # 把你的程序代码放到这里就OK了  
        self.threads = []  

        logging.error("add thread on port ")  

        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)  

if __name__=='__main__':  
    win32serviceutil.HandleCommandLine(SmallestPythonService)   
    # 括号里的名字可以改成其他的,必须与class名字一致;  

3.打开cmd,用cd命令进入PythonService.py的目录里
并使用以下命令进行操作

#安装服务
python PythonService.py install

#让服务自动启动
python PythonService.py --startup auto install 

#启动服务
python PythonService.py start

#重启服务
python PythonService.py restart

#停止服务
python PythonService.py stop

#删除/卸载服务
python PythonService.py remove

二、创建py-kms服务(失败)

概述

下载Vbs_To_Exe将原有的start.vbs转换为exe文件
然后使用sc create [service name]命令将其创建为windows服务

步骤

1.下载Vbs_To_Exe,并将start.vbs转换为exe文件

2.cmd命令创建服务

#创建服务
sc create "py-kms" binpath= "D:\kms\new\start.exe" start= auto displayname= "py-kms自动激活服务"
#添加服务描述
sc description py-kms "基于python的kms自动激活服务" 
#删除服务
`sc delete "py-kms"`

结果

但是在创建服务成功后,我一直无法顺利的启动服务,折腾很久也无法解决,故而转向使用组策略脚本来进行开机自启

尝试二:使用gpedit.msc创建启动脚本(成功)

在组策略中添加脚本

步骤

1.Win+X打开运行,输入gpedit.msc,选中计算机配置——Winddows配置——脚本(启动),将start.vbs添加进去即可
2.保险起见,我决定在用户配置中也添加一个start.vbs,作为备份,步骤和上面相同

测试

参考:http://blog.csdn.net/gsls200808/article/details/50326201
1.下载vlmcsd
2.找到目录内\binaries\Windows\intel下的的vlmcs-Windows-x86.exe对于64位的找vlmcs-Windows-x64.exe
将其改名为vlmcs.exe
cd跳转到改目录下
vlmcs.exe -v -l 3 192.168.1.1
-v输出详细信息
-l 3表示发送Windows Server 2008 Datacenter的激活请求,具体版本对应列表可以通过vlmcs.exe -x查看
192.168.1.1表示KMS服务器的域名

输出出现

Connecting to 192.168.1.1:1688 ... successful

大功告成!

消息盒子
# 您需要首次评论以获取消息 #
# 您需要首次评论以获取消息 #

只显示最新10条未读和已读信息