一、简介
正常情况我们在项目中遇到的都是Linux环境的服务器,也有部分项目会用到Windows server环境的服务器,当我们远程登录服务器,在服务器中安装服务比较多,看的人眼花缭乱,不太好管理时,我们可以使用代码框架化,把这些功能写到集成框架中或者测试工具中去操作,那么接下来我们就介绍,Python代码如何实现。
二、代码实现
名称 | 语法 | 描述 |
---|---|---|
sc create | sc create 服务名称 binpath= 启动服务路径 type= share start= 启动方式 DisplayName= 显示名称 | 创建新的服务 |
sc start | sc start 服务名称 | 启动服务 |
sc stop | sc stop服务名称 | 停止服务 |
sc delete | sc delete 服务名称 | 启动服务 |
sc query | sc query服务名称 | 获取服务运行状态 |
#!/usr/bin/python
# -*- coding: utf-8 -*-
#@Users: LiMu
#@Files:telnet.py
#@Times: 2023/3/16
#@Software:PyCharm
import time
import winrm
from ServerNameDict import ServerNameDict
class Telnet85(object):
def __init__(self,user:str,password:str):
self.Session = winrm.Session('http://YourTelnetPcIp:5985/wsman', auth=(user,password))
def CreateServer(self,ServerName:str,ExePath:str,StartType:str,Description:str):#为某应用创建服务
# StartType:demand(手动)、disabled(禁用),auto(自动)
NewCreate = self.Session.run_cmd("sc create {} binpath= '{}' type= share start= {} DisplayName= {}".format(ServerName, ExePath,StartType,ServerName))
print("{}服务创建:\n{}".format(ServerName, NewCreate.std_out.decode('gbk')))
NewDescription = self.Session.run_cmd("sc description {} {}".format(ServerName,Description))
print("{}服务描述添加:\n{}".format(ServerName, NewDescription.std_out.decode('gbk')))
def StartServer(self,ServerMessage:dict,all:str=None):#单独或者批量启动服务
if all == "all":
for i in ServerMessage.get("ServerName"):
NewStart = self.Session.run_cmd("sc start {}".format(i))
print("{}应用{}服务启动:\n{}".format(ServerMessage.get("GameName"),i,NewStart.std_out.decode('gbk')))
else:
GameName = ServerMessage.get("GameName")
commint = ServerMessage.get("ServerName")[0]
NewStart = self.Session.run_cmd("sc start {}".format(commint))
print("{}应用{}服务启动:\n{}".format(GameName,commint, NewStart.std_out.decode('gbk')))
def StopServer(self,ServerMessage:dict,all:str=None):#单独或者批量停止服务
if all == "all":
for j in ServerMessage.get("ServerName"):
NewStop = self.Session.run_cmd("sc stop {}".format(j))
print("{}应用{}服务停止:\n{}".format(ServerMessage.get("GameName"),j,NewStop.std_out.decode('gbk')))
else:
GameName = ServerMessage.get("GameName")
commint = ServerMessage.get("ServerName")[0]
NewStop = self.Session.run_cmd("sc stop {}".format(commint))
print("{}应用{}服务停止:\n{}".format(GameName,commint, NewStop.std_out.decode('gbk')))
def DeleteServer(self,ServerName:str):#删除电脑某个服务
NewDelete = self.Session.run_cmd("sc delete {}".format(ServerName))
print("{}服务删除成功:\n{}".format(ServerName, NewDelete.std_out.decode('gbk')))
def ConfigServer(self,ServerName:str,StartType:str):#修改电脑服务启动类型
#StartType:demand(手动)、disabled(禁用),auto(自动)
NewConfig = self.Session.run_cmd("sc config {} start= {}".format(ServerName,StartType))
print("{}服务修改配置完成:\n{}".format(ServerName, NewConfig.std_out.decode('gbk')))
def GetServer(self,ServerName:str):#获取电脑某个服务运行状态
NewQuery = self.Session.run_cmd("sc Query {}".format(ServerName))
print("{}服务运行状态:\n{}".format(ServerName, NewQuery.std_out.decode('gbk')))
def CmdServer(self,CmdMsg:str):#DOS命令操作
CmdServer = self.Session.run_cmd(CmdMsg)
print("执行命令运行结果如下所示:")
print(CmdServer.std_out.decode("UTF-8")) #使用UTF-8否则很多情况会出行乱码
if __name__ == '__main__':
CrateClass = Telnet85("you pc login user","your password")
# CreateServer = CrateClass.CreateServer("aaaaa","D:\XjqxzCross\crossserver\CrossServer.exe","disabled","叉叉服务")
# CrateClass.DeleteServer("aaaaa")
StopServer = CrateClass.StopServer(ServerNameDict.Liangjian4_Interface())
time.sleep(5) #服务器停止和重启同时请求时要加睡眠时间,时间长短和电脑性能有关。
StartServer = CrateClass.StartServer(ServerNameDict.Liangjian4_Interface())
# CrateClass.GetServer(ServerNameDict.Liangjian6_Interface().get("ServerName")[0])
# CrateClass.CmdServer("type E:\LiangJianSix\Liangjian6Interface\Log\\2023\\3\\2023-03-29-17.Info.txt")