windows远程登录应用

一、远程登录简介

        当公司的服务器使用的Windows server时,我们要远程进行自动化的业务管理时,无法是用手动远程登录远程服务器的操作界面去操作,这样也显得很鸡肋,而且绝大多数服务器都是Linux,所以对于Windows服务相关的资料不是很多,就会导致很多朋友手足无措,这里就介绍一些管理简单的Windows服务相关的管理,以下是我们要用到一些命令。

名称语法解释
netnet use查看PC的远程链接
scsc *** SeverName操作PC服务的情况
dirdir path用于查看PC文件信息
copydir path1 path2复制粘贴文件夹或者文件

二、远程服务链接创建

三、远程服务器操作

四、其他远程方法

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)
···









 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值