I know how to use win32serviceutil to start, stop, and restart services, but how can I disable them, or set them on manual start? I'm fine with using any module compatible with Python 3.4.
Disable is one of windows service startup types. And it's different from normal windows service operations, such as start, stop, restart and so on.
You need to use --startup disabled as parameters for win32serviceutil. Here is detail description. Check Usage() function.
After setting startup type, you need to stop the service. And after that, that service will be disabled unless you change the startup type again.
Here is detail for windows service, if you find all detail about windows service startup type.
If this is the answer, please confirm it. Thanks!
I ended up using os.system and Windows' Sc.exe to do it. Like so:
import os
os.system("sc config " + servicename + " start=disabled")
I think we can use WMI(Windows Management Instrumentation) to disable the windows service with python module 'wmi'.
import wmi
c = wmi.WMI()
windows_update_services_list = [
"wuauserv",
"TrustedInstaller"
]
for service_name in windows_update_services_list:
service = c.Win32_Service(Name=service_name)[0]
service.ChangeStartMode(StartMode="Disabled")
other useful links:
win32-service
change service start mode
powershell commands:
Set-Service -Name <service name> -StartupType Disabled
Related
I am using pyVmomi for my automation. Now I am testing a VM, where the SSH is disabled by default, and I must change the password upon the first login:
You are required to change your password immediately (root enforced)
If I try connecting using pyVmomi's StartProgramInGuest, I get vim.fault.InvalidGuestLogin exception:
Failed to authenticate with the guest operating system using the supplied credentials
I am looking for a way to change the default password programmatically, (preferably) using pyVmomi
To start off, it seems like you failed to pass the correct credentials when calling the "StartProgramInGuest" function, you can specify and pass credentials to this function using Name-Password authentication, like below.
creds = vim.vm.guest.NamePasswordAuthentication(username='username', password='password)
Test this and make sure you successfully authenticated to the guest virtual machine. After you're able to successfully authenticate, you can use Process Manager, to create either a Linux process or Windows process to change your password. For example, here is a PowerShell process tested on a Windows 10 virtual machine, executed through StartProgramInGuest.
argument= vim.vm.guest.ProcessManager.ProgramSpec(programPath='powershell.exe -Command', arguments='"& {net user loginid newpassword;}"')
res = pm.StartProgramInGuest(vm, creds, argument)
Let me know if you need any clarification!
Up until now, whenever I have needed to store a "secret" for a simple python application, I have relied on environment variables. In Windows, I set the variables via the Computer Properties dialog and I access them in my Python code like this:
database_password = os.environ['DB_PASS']
The simplicity of this approach has served me well. Now I have a project that uses Oauth2 authentication and I have a need to store tokens to the environment that may change throughout program execution. I want them to persist the next time I execute the program. This is what I have come up with:
#fetch a new token
token = oauth.fetch_token('https://api.example.com/oauth/v2/token', code=secretcode)
access_token = token['access_token']
#make sure it persists in the current session
os.environ['TOKEN'] = access_token
#store to the system environment (Windows)
cmd = 'SETX /M TOKEN ' + access_token
os.system(cmd)
It gets the job done quickly for me today, but does not seem like the right approach to add to my toolbox. Does anyone have a more elegant way of doing what I am trying to do that does not add too many layers of complexity? If the solution worked across platforms that would be a bonus.
I have used the Python keyring module with great success. It's an interface to credential vaults provided by the operating system (e.g., Windows Credential Manager). I haven't used it on Linux, but it appears to be supported, as well.
Storing a password/token and then retrieving it can be as simple as:
import keyring
keyring.set_password("system", "username", "password")
keyring.get_password("system", "username")
How about using pywin32 module ?
Or is there any way to achieve this through subprocess module ?
I can't imagine how this is possible.
Windows services do not expose generic messaging API; each service (should it choose to) exposes its own specific API via its own choice of IPC channel (eg. WCF).
Regardless though, nothing would allow you to do this synchronously; any kind of IPC will be an async call to the service endpoint.
You kind of need to be more specific in your question.
The available generic APIs for interacting with a windows service are basically limited to; stop, start, install, uninstall. Have a look here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms685942(v=vs.85).aspx
(If you are writing a new windows service, in python, ZeroMQ would be a very reasonable choice to interact with it from a command line python script; there are any number of alternative IPC channels for python which would be equally good)
--
To just start a service, try:
import win32service
import win32serviceutil
import time
win32serviceutil.StartService(serviceName)
status = win32serviceutil.QueryServiceStatus(serviceName)
while status == win32service.SERVICE_START_PENDING:
time.sleep(1)
status = win32serviceutil.QueryServiceStatus(serviceName)
Nb. You'll get an access denied error unless you spawn the python instance as administrator.
I'm currently writing a script where I need to gain access to another computer on my LAN while using administrative credentials that differ from the account I am logged in as. I attempted to use the requests module.
Here is my code so far:
import requests
with requests.Session() as c:
location = ('file://computer/c$/')
USERNAME = 'notrealusername'
PASSWORD = 'notrealpassword'
c.get(location)
logindata = dict(username=USERNAME, password=PASSWORD, next='/')
c.post(location, data=logindata, headers{"Referer":"file://computer/c$/"})
Can someone tell me how I can edit my code to make it work properly according to the criteria specified above?
Impacket
This 3rd party library is pretty useful for Windows related networking tasks. In this situation i would use their wmiexec.py script:
wmiexec.py
A semi-interactive shell, used through Windows Management Instrumentation. It does not require to install any service/agent at the target server. Runs as Administrator. Highly stealthy.
If your not wanting any 3rd party dependencies, you could write your own solution. A wmi shell is mentioned in the BlackHat Python book.
I am new to Python and would like to write a script to change Windows proxy settings based on the network I am connected to. Is there any existing python module I can use? Appreciate your help.
Thanks,
Sethu
I would use winreg and query the settings directly from the registry.
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet
Settings] "MigrateProxy"=dword:00000001
"ProxyEnable"=dword:00000001
"ProxyHttp1.1"=dword:00000000
"ProxyServer"="http://ProxyServername:80"
"ProxyOverride"="<local>"
For example, something like:
import _winreg
def getProxy():
proxy = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings")
server, type = _winreg.QueryValueEx(proxy, "ProxyServer")
enabled, type = _winreg.QueryValueEx(proxy, "ProxyEnable")
if enabled:
return server
return None
Cannot you set the HTTP_PROXY environment variable in Windows (either manually or within your program) for your application before sending the request? That should take care that any request you send it via urllib2 goes via Proxy.