Python Forum
Problem executing a script on a remote host
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem executing a script on a remote host
#1
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.
Reply
#2
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"
Reply
#3
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)
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Triggering a ps1 script in remote windows server via http python request jasveerjassi 1 318 Jan-26-2024, 07:02 PM
Last Post: deanhystad
Question How can I import a variable from another script without executing it ThomasFab 12 7,558 May-06-2022, 03:21 PM
Last Post: bowlofred
  where to host my python script tomtom 1 1,233 Feb-09-2022, 06:45 AM
Last Post: ndc85430
  Script stop work after 3 actioins - PLEASE WHERE IS THE PROBLEM? rondon442 0 1,532 Sep-27-2021, 05:40 PM
Last Post: rondon442
  Failing to connect to a host with WMI tester_V 6 4,279 Aug-10-2021, 06:25 PM
Last Post: tester_V
  cant use ping, sudo or other commands in remote shell script. throwaway34 7 3,533 May-17-2021, 11:29 AM
Last Post: throwaway34
  problem with sphinx and file directory in script kiyoshi7 0 2,248 Mar-11-2021, 03:52 PM
Last Post: kiyoshi7
  problem about slope in python script for bitcoin trading fisher_garry 1 2,469 Sep-02-2020, 01:39 PM
Last Post: fisher_garry
  Problem running script within console koepjo 3 9,802 Mar-26-2020, 07:11 AM
Last Post: koepjo
  How to gather information from remote host using ansible module in python amritjsr 0 2,005 Jul-20-2019, 01:17 AM
Last Post: amritjsr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020