一、远程登录简介
当公司的服务器使用的Windows server时,我们要远程进行自动化的业务管理时,无法是用手动远程登录远程服务器的操作界面去操作,这样也显得很鸡肋,而且绝大多数服务器都是Linux,所以对于Windows服务相关的资料不是很多,就会导致很多朋友手足无措,这里就介绍一些管理简单的Windows服务相关的管理,以下是我们要用到一些命令。
名称 | 语法 | 解释 |
---|---|---|
net | net use | 查看PC的远程链接 |
sc | sc *** SeverName | 操作PC服务的情况 |
dir | dir path | 用于查看PC文件信息 |
copy | dir path1 path2 | 复制粘贴文件夹或者文件 |
二、远程服务链接创建
- 前言描述:远程服务链接创建我们也要使用到net命令,下面我就根据实际操作实例来讲解。
- 创建命令:net use host "password" /user:"username"
- 查看命令:net use
三、远程服务器操作
- 服务启动:sc host start ServerName
- 服务停止:sc host stop ServerName
- 文件查看:dir host\DiskNo.$\path
- 文件复制:copy LocalPath host\DiskNo.$\path
- 注意:以上可以点击链接查看实例,命令也只是例举了有代表性的命令,批量命令基本上都可以通过以上方法操作远程服务器,这里就不一一做介绍了。
四、其他远程方法
4.1.网络需要更改成专用网络
4.2.Windows需要打开WinRM服务,Server系统默认打开,默认端口5985
- 远程端服务器依次执行以下命令开启打开WinRM服务
①WinRM服务查看:winrm enumerate winrm/config/listener
②开启远程管理权限:winrm quickconfig
③配置基本验证服务:winrm set winrm/config/service/auth '@{Basic="true"}'
④配置非加密服务:winrm set winrm/config/service '@{AllowUnencrypted="true"}'
⑤查看WinRM配置:winrm get winrm/config
4.3.Python代码远程调用
#!/usr/bin/python
# -*- coding: utf-8 -*-
#@Users: LiMu
#@Files:telnet.py
#@Times: 2023/3/16
#@Software:PyCharm
import winrm
class Telnet85(object):
def __init__(self,user,password):
self.Session = winrm.Session('http://10.1.0.85:5985/wsman', auth=(user,password))
def RunCmd(self,cmd):
response = self.Session.run_cmd(cmd)
print("Execute the command to return the status code:{}".format(response.status_code))
return response.std_out.decode('gbk')
if __name__ == '__main__':
crateclass = Telnet85("Administrator","admin==123456")
response = crateclass.RunCmd("dir e:\\")
print(response)
D:\Python37\python.exe E:\pythonFiles\Interfacetest\tools\telnet85.py
执行命令返回状态码:0
Volume in drive E is ????
Volume Serial Number is EA3E-CB7C
Directory of E:\
2023/03/13 10:27 <DIR> APublicServer
2023/03/15 17:13 <DIR> LiangjianThreeFiveServer
2023/02/23 14:21 <DIR> LiangJianThreeFourServer
2023/02/23 14:22 <DIR> LiangJianThreeThreeServer
2022/11/30 14:40 <DIR> SheDiaoThreeServer
2022/11/01 11:20 <DIR> XianJianFiveServer
0 File(s) 0 bytes
6 Dir(s) 995,034,525,696 bytes free
Process finished with exit code 0
五、总结
代码远程执行Windows命令还有其他方式,例如:服务器上搭建SSH sever,通过ssh远程连接,Python有个专门的ssh模块去操作,Linux环境下默认就有SSH sever,所以不需要刻意去搭建这项服务,下面我赋上Linux的代码模型,Windows环境也大同小异,这里就不做介绍了。
#!/usr/bin/python
# -*- coding: utf-8 -*-
#@Users: LiMu
#@Files:linux_handle.py
#@Times: 2021/10/11
#@Software:PyCharm
import os
import sys
import paramiko
class linux_class(object):
def __init__(self):
#获取Linux的账户信息
self.iAcount = {"hosts":"10.1.0.87","users":"root","pwd":"****"}
def linux_connect(self,linux_command):
# 创建链接Linux的ssh实例
ssh_noun = paramiko.SSHClient()
ssh_noun.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 登录linux系统
connect = ssh_noun.connect(hostname=self.iAcount.get("hosts"),port=22,username=self.iAcount.get("users"), password=self.iAcount.get("pwd"),timeout=20)
# 访问Linux并执行命令
stdin, stdout, stderr = ssh_noun.exec_command(linux_command)
# 获取执行命令的结果
# resposne_stdin = stdin.read().decode('utf-8')
# resposne_stderr = stderr.read().decode('utf-8')
resposne_stdout = stdout.read().decode('utf-8')
return resposne_stdout
if __name__ == '__main__':
new_selcet = linux_class()
resd = new_selcet.linux_connect("cat /woodsong/test.sh")
print(resd)
···