Python Forum
Problem executing a script on a remote host - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problem executing a script on a remote host (/thread-35031.html)



Problem executing a script on a remote host - tester_V - Sep-24-2021

Greetings!

I have a remote host(windows) and a system(windows based) connected to it by a private network.
The host has Python 27 on it.
Once a day a Python script runs (scheduled run) on it,
It is mapping the system C: drive as an A: drive and copies files to the host from some directory,
also it does some other stuff. It runs fine, no problems.

I wanted to run the script on the host remotely from my server (windows, with Python 3.9) instead of running it from the Windows scheduler.

I found if I call the script it runs (does some other stuff) but it does not copy files. Sad
No errors. When I run manually it does everything, including copying files. Undecided
Any idea who to fix this?
Here is a script to execute a script on a remote host:
import wmi
from socket import *
ip = 'xx.x.xxx.x'
username = 'user'
password = 'pass'

try:
    print("Establishing connection to %s" %ip)
    connection = wmi.WMI(ip, user=username, password=password)
    print("Connection established")
    connection.Win32_Process.Create(CommandLine=r"C:\Python27\python.exe C:\02\SubPrOc-Get-L_TESTING.py")
except wmi.x_wmi:
    print("Your Username and Password of "+getfqdn(ip)+" are wrong.")
exit   
Thank you.


RE: Problem executing a script on a remote host - tester_V - Sep-24-2021

I found a different script online to 'remote' to a host. it is better.
import wmi, time
ip = 'xx.xx.xx.xxx'
username = "user"
password = "password!"
SW_SHOWNORMAL = 1
from socket import *
print ("Establishing connection to %s" %ip)
c = wmi.WMI(ip, user=username, password=password)
process_startup = c.Win32_ProcessStartup.new()
process_startup.ShowWindow = SW_SHOWNORMAL
process_id, result = c.Win32_Process.Create(CommandLine=r"C:\02\SubPrOc-Get-L_TESTING.py",ProcessStartupInformation=process_startup)
if result == 0:
  print ("Process started successfully: %d" % process_id)
else:
  print("Problem creating process: %d" % result)


it produces an error when trying to connect to a remote host:
'"Problem creating process: 8"


RE: Problem executing a script on a remote host - tester_V - Sep-25-2021

Update.
found why I had an error when creating a process. The script was missing the Python directory.
Also added 'process terminate' to the script.

But still the same problem. When Python script is executed manually it runs fine and when executed remotely it fails to copy files.
import wmi, time
ip = 'xx.xx.xx.xxx'
username = "user"
password = "password!"
SW_SHOWNORMAL = 1
from socket import *
print ("Establishing connection to %s" %ip)
c = wmi.WMI(ip, user=username, password=password)
process_startup = c.Win32_ProcessStartup.new()
process_startup.ShowWindow = SW_SHOWNORMAL
process_id, result = c.Win32_Process.Create(CommandLine=r"C:\Python27\python.exe C:\02\SubPrOc-Get-L_TESTING.py",ProcessStartupInformation=process_startup)
if result == 0:
    print ("Process started successfully: %d" % process_id)
    for process in c.Win32_Process () :
        print(process.ProcessId, process.Name)
        
        if process.ProcessId== process_id :
            print (f" proccess to KILL ---> {process.ProcessId}")  
            process.Terminate()            
        if process.Name=='conhost.exe' :
            print(f" Proccess Name to Kill -> {process.Name}")
            process.Terminate()  
else:
  print("Problem creating process: %d" % result)



RE: Problem executing a script on a remote host - tester_V - Sep-26-2021

Update.
Here is a file I'm trying to execute remotely. It runs fine, no errors if executed manually, no errors.
but if called remotely it does nothing. Sad
I'm not sure how to debug it. Cry

script:
import subprocess,socket,shutil,time 
import os.path

drl ='A'
e_ip ='XX.X.X.X'
mp_string = "net use"+" "+drl+":"+" \\\\"+e_ip+"\\"+"C$"	# <-- String to mount
dr_one ="\\\\"+e_ip+"\\"+"C$"+"\\"+"02"+"\\"+"logs"	# <-- First Directory to get logs from

delete_st = "net use"+" "+drl+":"+" "+"/delete /yes"

sys_name = socket.gethostname()
print(sys_name)                   # <-- System name

make_dp ='C:\\02\\dt\\'+sys_name  # <-- Directory to Copy Files to...
if not os.path.exists(make_dp):
    os.mkdir(make_dp)  

monitor = 0	
with open('C:\\02\\dt\\Num_OF_Files_Copied.txt','w') as num_of_f_copied :  # <- Log files for the script 
    num_of_f_copied.write(sys_name+'\n')
    try:
        subprocess.call(mp_string,shell=True)   # <-- Mounting Cell

        if dr_one :
            print(' Dir Exits -',dr_one)			
            num_of_f_copied.write(' Dir Exits -'+dr_one+'\n')		
            
            for items in os.listdir(dr_one) :  		
                items=items.strip()
                itemspath = os.path.join(dr_one,items)				
                #print(' ALL ITEMS', itemspath)  
			
                if os.path.isfile(itemspath):
                    items=items.strip()
                    num_of_f_copied.write(' ITEMS -'+items+'\n') 			
                    #print(' File Only ->', items)			
                    if 'Monitor.' in items : 	# <-- Monitor is in the name of the files
                        print(' Monitor Files -> ',itemspath)
                        num_of_f_copied.write(' MONITORS -> '+itemspath+'\n')
                        
                        try :  						
                            shutil.copy(itemspath,make_dp)
                            num_of_f_copied.write(' Copying Files -> '+itemspath+'\n')								
                            monitor+=1
                        except OSError as cpf :
                            print(' Failed to copy files','\n')
                            num_of_f_copied.write(' Failed to copy files'+'\n')							
    except OSError as er :
        print(" Error scanning or Copying ->", er)			
print(' Number of files copied ->',monitor)
subprocess.call(delete_st)	
Thank you.