如何连接到远程 Windows 机器以使用 python 执行命令? [英] How to connect to a remote Windows machine to execute commands using python?

查看:41
本文介绍了如何连接到远程 Windows 机器以使用 python 执行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,我正在尝试制作一个连接到远程 Windows 机器并在那里执行命令并测试端口连接性的脚本.

这是我正在编写的代码,但它不起作用.基本上,我想要它返回本地机器数据,而不是远程数据.

导入 wmi导入操作系统导入子流程进口重新导入套接字,系统定义主():主机=远程机器"用户名 =adminaam"密码=通行证!"服务器=连接(主机,用户名,密码)s = socket.socket()s.settimeout(5)打印 server.run_remote('主机名')类连接:def __init__(self, host, username, password, s = socket.socket()):self.host=hostself.username=用户名self.password=密码self.s=s尝试:self.connection= wmi.WMI(self.host, user=self.username, password=self.password)self.s.connect(('10.10.10.3', 25))打印已建立连接"除了:打印无法连接到机器"def run_remote(自我,cmd,异步=假,最小化=真):call=subprocess.check_output(cmd, shell=True,stderr=subprocess.STDOUT )打印调用主要的()

解决方案

您可以使用以下两种方法将一台计算机连接到网络中的另一台计算机:

  • 使用 WMI 库.
  • 网络使用方法.
<小时>

WMI

以下是使用 wmi 模块连接的示例:

ip = '192.168.1.13'用户名 = '用户名'密码 = '密码'从套接字导入 *尝试:print("建立与 %s 的连接" %ip)连接= wmi.WMI(ip,用户=用户名,密码=密码)print("连接建立")除了 wmi.x_wmi:print("你的"+getfqdn(ip)+"的用户名和密码错误.")

<小时>

网络使用

第二种方法是使用netuse模块.

通过 Netuse,您可以连接到远程计算机.您可以访问远程计算机的所有数据.可以通过以下两种方式:

  1. 通过虚拟连接进行连接.

    导入win32api导入 win32netip = '192.168.1.18'用户名 = 'ram'密码 = 'ram@123'use_dict={}use_dict['remote']=unicode('\\\\192.168.1.18\C$')use_dict['password']=unicode(password)use_dict['username']=unicode(username)win32net.NetUseAdd(None, 2, use_dict)

    断开连接:

    导入win32api导入 win32netwin32net.NetUseDel('\\\\192.168.1.18',username,win32net.USE_FORCE)

  2. 在本地系统中安装远程计算机驱动器.

    导入win32api导入 win32net导入 win32netcon,win32wnet用户名='用户'密码 ='psw'尝试:win32wnet.WNetAddConnection2(win32netcon.RESOURCETYPE_DISK, 'Z:','\\\\192.168.1.18\\D$', None, 用户名, 密码, 0)print('连接建立成功')除了:print('连接未建立')

    在本地系统中卸载远程计算机驱动器:

    导入win32api导入 win32net导入 win32netcon,win32wnetwin32wnet.WNetCancelConnection2('\\\\192.168.1.4\\D$',1,1)

<块引用>

在使用 netuse 之前,您还应该使用 python 在系统中安装 pywin32.

<小时>

来源:连接远程系统.

I am new to Python and I am trying to make a script that connects to a remote windows machine and execute commands there and test ports connectivity.

Here is the code that I am writing but it is not working. Basically, I want to and it returns with the local machine data, not the remote one.

import wmi
import os
import subprocess
import re
import socket, sys

def main():

     host="remotemachine"
     username="adminaam"
     password="passpass!"
     server =connects(host, username, password)
     s = socket.socket()
     s.settimeout(5)
     print server.run_remote('hostname')

class connects:

    def __init__(self, host, username, password, s = socket.socket()):
        self.host=host
        self.username=username
        self.password=password
        self.s=s

        try:
            self.connection= wmi.WMI(self.host, user=self.username, password=self.password)
            self.s.connect(('10.10.10.3', 25))
            print "Connection established"
        except:
            print "Could not connect to machine"


   def run_remote(self, cmd, async=False, minimized=True):
       call=subprocess.check_output(cmd, shell=True,stderr=subprocess.STDOUT )
       print call

main() 

解决方案

You can connect one computer to another computer in a network by using these two methods:

  • Use WMI library.
  • Netuse method.

WMI

Here is the example to connect using wmi module:

ip = '192.168.1.13'
username = 'username'
password = 'password'
from socket import *
try:
    print("Establishing connection to %s" %ip)
    connection = wmi.WMI(ip, user=username, password=password)
    print("Connection established")
except wmi.x_wmi:
    print("Your Username and Password of "+getfqdn(ip)+" are wrong.")


netuse

The second method is to use netuse module.

By Netuse, you can connect to remote computer. And you can access all data of the remote computer. It is possible in the following two ways:

  1. Connect by virtual connection.

    import win32api
    import win32net
    ip = '192.168.1.18'
    username = 'ram'
    password = 'ram@123'
    
    use_dict={}
    use_dict['remote']=unicode('\\\\192.168.1.18\C$')
    use_dict['password']=unicode(password)
    use_dict['username']=unicode(username)
    win32net.NetUseAdd(None, 2, use_dict)
    

    To disconnect:

    import win32api
    import win32net
    win32net.NetUseDel('\\\\192.168.1.18',username,win32net.USE_FORCE)
    

  2. Mount remote computer drive in local system.

    import win32api
    import win32net
    import win32netcon,win32wnet
    
    username='user'
    password='psw'
    
    try:
        win32wnet.WNetAddConnection2(win32netcon.RESOURCETYPE_DISK, 'Z:','\\\\192.168.1.18\\D$', None, username, password, 0)
        print('connection established successfully')
    except:
        print('connection not established')
    

    To unmount remote computer drive in local system:

    import win32api
    import win32net
    import win32netcon,win32wnet
    
    win32wnet.WNetCancelConnection2('\\\\192.168.1.4\\D$',1,1)
    

Before using netuse you should have pywin32 install in your system with python also.


Source: Connect remote system.

这篇关于如何连接到远程 Windows 机器以使用 python 执行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆