1

I am currenlty using machine A and I am trying to access machine B via Python to copy files from machine B to machine A.

I have already tried the methods explained here How to connect to a remote Windows machine to execute commands using python? , but with no luck as I cannot manage to even get access to the remote machine. I am open to other solutions, even better if using Python 3+. Here is an example of the code in use.

ip = r'\\IP.IP.IP.IP'
username = r'AccountUserName'
password = r'AccountPassword'
    # -------------------------------- with win32net

    import win32net
    import win32file

    data = {
        'remote': r'\\IP.IP.IP.IP\C$', 
        'local': 'C:',
        'username': username,
        'password': password 
    }

    win32net.NetUseAdd(None, 2, data)



    # -------------------------------- with wmi

    import wmi
    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.")
  • Using the win32net method

According to the documentation here https://learn.microsoft.com/en-us/windows/win32/api/lmuse/nf-lmuse-netuseadd If the function is to be run from the same computer the script is running from (A), then the first parameter f NetUseAdd can be left to NONE, but with that I get the error

    pywintypes.error: (87, 'NetUseAdd', 'The parameter is incorrect.')

Whilst if I change it with "127.0.0.1" I get the error

    pywintypes.error: (50, 'NetUseAdd', 'The request is not supported.')

And lastly, if I change it with the same IP that I am trying to access I get the error

    pywintypes.error: (1326, 'NetUseAdd', 'Logon failure: unknown user name or bad password.')
  • Using the wmi method

It gives the error

    Your Username and Password of \\IP.IP.IP.IP are wrong.
Conk
  • 73
  • 1
  • 5
  • Try using this :https://stackoverflow.com/questions/18961213/how-to-connect-to-a-remote-windows-machine-to-execute-commands-using-python – Tommy Lawrence Sep 09 '19 at 12:28
  • Thanks @TommyLawrence , but as explained in the presentation, I have tried it already. – Conk Sep 09 '19 at 13:38
  • Opps, sorry, I thought it was a different article, there are wrappers out there that will allow you to remote control the computer, try syscon. – Tommy Lawrence Sep 17 '19 at 13:51

1 Answers1

0

There can be multiple ways to achieve this. One of them is given below which makes use of inbuilt windows utilities.

import os

machine_b = {"ip":"10.197.145.244","user":"administrator","pwd":"abc1234"}
src = r"C:\Temp" # folder to copy from remote machine
dest = r"C:\Python27\build\temp" # destination folder on host machine
network_drive_letter = "Z:"
source_driver_letter = os.path.splitdrive(src)[0][0]
cmd = "netuse %s \\%s\%s$ %s /u:%s"%(network_drive_letter, machine_b["ip"],source_driver_letter,machine_b["pwd"],machine_b["user"])
os.system(cmd)
cmd = "robocopy %s %s /mir"%(src.replace(source_driver_letter,network_drive_letter),dest)
os.system(cmd)

You can improve this code by handling exceptions and replacing os.system with subprocess.Popen calls. Note: Be careful with /MIR switch as it can copy as well as delete files in the host machine. It creates mirror of destination folder.

Nitin_k29
  • 341
  • 1
  • 3
  • 7