Detecting what apps are open in python [duplicate] - python-3.x

I amm writing a little python script that will grab information from VMs of Windows that I am running.
At the moment I can list the processes on a 32bit XP machine with the following method:
http://code.activestate.com/recipes/305279/
Is it possible to somehow detect the version of windows running and excute a different method for getting the processes on a 64bit machine, I am trying to get the processes from a 64Bit Vista and 64bit Windows 7.
Any ideas?

If you don't want to rely on any extra installed modules then you can parse the output of wmic, e.g.:
c:\> wmic process get description,executablepath
...
explorer.exe C:\Windows\explorer.exe
cmd.exe C:\Windows\SysWOW64\cmd.exe
conhost.exe C:\Windows\system32\conhost.exe
...
Reference: http://geekpedia.wordpress.com/2008/08/18/use-command-line-to-track-windows-processes/

There is another recipe on activestate that does a similar thing, but uses the Performance Data Helper library (PDH) instead.
I have tested this on my Windows 7 64bit machine and it works there - so presumably the same function will work on both 32bit and 64 bit windows.
You can find the recipe here: http://code.activestate.com/recipes/303339/
Another method is using WMI, there is an example here in Python using the wmi module:
http://timgolden.me.uk/python/wmi/cookbook.html
import wmi
c = wmi.WMI ()
for process in c.Win32_Process ():
print process.ProcessId, process.Name

The cleanest way I found to solve this was to use the psutil library as recommended by Robert Lujo:
psutil.process_iter()
Note that it returns a generator object, issuing a process object at a time. For example if you need the list of process names, you can do something like:
[p.name() for p in psutil.process_iter()]

For similar purposes I have used psutil library. Some hints:
list processes with psutil.pids() (reference)
inspect process information with process = psutil.Process(pid) (reference)
do process.kill or process.terminate()
Installation on windows - pip will do installation from the source (which means compiling), so you probably want to download binary installation from https://pypi.python.org/pypi/psutil/#downloads.

You should be able to do this by exposing Windows Management Instrumentation within each VM. This tool gives you access to a bunch of system data, including processes, see http://technet.microsoft.com/en-us/library/cc757287%28WS.10%29.aspx
You should be able to popen one of the commands in the preceding link to get the info you're looking for.

Related

XSCT executes command in interactive shell but not within script

First, take note that I am using the Xilinx SDK 2018.2 on Kubuntu 22.04 because of my companies policy. I know from research, that the command I'm using is deprecated in newer versions, but in the version I am using, it works flawlessly - kind of... But read for yourself:
My task is to automate all steps in the FPGA build to create a pipeline which automatically builds and tests the FPGAs. To achieve this, I need to build the code - this works flawlessly in XSDK. For automation, this also has to work in the command line, so what I did is following the manual to find out how this is achieved. Everything works as expected if I write it in the interactive prompt like shown here:
user#ubuntuvm:~$ xsct
****** Xilinx Software Commandline Tool (XSCT) v2018.2
**** Build date : Jun 14 2018-20:18:43
** Copyright 1986-2018 Xilinx, Inc. All Rights Reserved.
xsct%
Then I can enter the commands I need to import all needed files and projects (hw, bsp, main project). With this toolset, everything works as expected.
Because I want to automate it via a pipeline, I decided to pack this into a script for easier access. The script contains exactly the commands I entered in the interactive shell and therefore looks like this:
user#ubuntuvm:~/gitrepos/repository$ cat ../autoBuildScript.tcl
setws /home/user/gitrepos/repository
openhw ./hps_packages/system.hdf
openbsp ./bsp_packages/system.mss
importprojects ./sources/mainApp
importprojects ./bsp_packages
importprojects ./hps_packages
regenbsp -bsp ./bsp_packages/system.mss
projects –clean
projects -build
The commands are identical to the ones entered via the interactive CLI tool, the only difference is that this is now packed into a script. The difference is, that this now does not build completely anymore. I get the following error:
user#ubuntuvm:~/gitrepos/repository$ xsct ../autoBuildScript.tcl
INFO: [Hsi 55-1698] elapsed time for repository loading 1 seconds
Starting xsdk. This could take few seconds... done
'mainApp' will not be imported... [ALREADY EXIST]
'bsp_packages' will not be imported... [ALREADY EXIST]
'hps_packages' will not be imported... [ALREADY EXIST]
/opt/Xilinx/SDK/2018.2/gnu/microblaze/lin
unexpected arguments: –clean
while executing
"error "unexpected arguments: $arglist""
(procedure "::xsdb::get_options" line 69)
invoked from within
"::xsdb::get_options args $options"
(procedure "projects" line 12)
invoked from within
"projects –clean"
(file "../autoBuildScript.tcl" line 8)
I've inserted projects -clean only, because I got the error before with projects -build and wanted to check, if this also happens with another argument.
In the internet I didn't really find anything according to my specific problem. Also I strictly held on to the official manual, in which the command is also used just as I use it - but with the result of it being working.
Also, I've checked the line endings (set to UNIX) because I suspected xsct to read maybe a newline character or something similar, with no result. This error also occurs, when I create the bsp and hardware from sketch. Also, to me the error looks like an internal one from Xilinx, but let me know what you think.
So, it appears that I just fixed the problem on my own. Thanks on everyone reading for being my rubber ducky.
Apparently, the version 2018.2 of XSDK has a few bugs, including inconsistency with their command interpretation. For some reason the command works in the interactive shell, but not in the script - because the command is in its short form. I just learned from a Xilinx tutorial, that projects -build is - even though it works - apparently not the full command. You usually need to clarify, that this command should come from the SDK like this: sdk projects -build. The interactive shell seems to ignore this fact for a reason - and so does the script for any command except projects. Therefore, I added the "sdk" prefix to all commands which I used from the SDK, just to be safe.
I cannot believe, that I just debugged 2 days for an error whose fix only contains 3 (+1 whitespace) letters.
Thanks everybody for reading and have a nice day

How to get "AWS Transcribe" To Work within Tomcat (Windows Service based) Using System.exec()?

I'm trying to run the amazon-provided python AWS Example for transcribing an audio file in a Java program using "System.exec()". I've substituted the various parts of the program to use parameters passed in. This code works as expected on a Mac, but in Windows (under Tomcat Service), the exact same System.exec() always returns null.
Basic Python code is here: https://docs.aws.amazon.com/code-samples/latest/catalog/python-transcribe-getting_started.py.html
I thought it might be an authentication problem, but I've authenticated trying the config file in the expected location, Environment Variables, and even System.setProperty() statements. Still always null.
Here's the Java code:
process = Runtime.getRuntime().exec(cmdArray);
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
The command array has:
python
path to python 'py' file
parameters
The code crashes on the subsequent line (null):
reader.readLine()
I have the Tomcat service set up to use 8MB RAM and have re-installed Tomcat to the latest 9 release.
If I take the individual parameters at the command line and execute them, it's all fine.
Would appreciate any suggestions.
Thanks in advance.

How to request NASMT Q700 QNAP linux hard disk smart states using the ssh interface?

I use a NASMT Q700 QNAP NAS. For remote monitoring purposes i want to read some values and save them into a database.
Since the web-interface is very complex and full of javascript, i can not scrape it. So I tried to connect to the NAS with SSH.
Which is great, because SSH is one of the methods, that i can connect with automatically with c# and I get back text that I can parse.
The installed Linux system on the box is a :
Linux NASMT 2.6.33.2 #1 Fri Mar 7 11:55:22 CST 2014 armv5tel unknown
I tried to reach my goal:
man is not installed.
smartctl is not installed. (Google told me to try this out)
I went into the /bin and /usr/bin directories and tried everything suspecious. There seems to be a program called nasutil installed. Only that it is not very self documenting. Various calls with different parameters did not work, i always get the same answer:
nasutil multi-call binary
[function] [arguments]...
Current defined functions:
init_nas_cache, init_admin_group, set_file_owner, chk_flash, reset_all, chk10198, get_trusted_domain, update_krb5_ticket
rescan_hd, check_e2key, burn_e2key, cnt_phy_nic, http_link, ip_filter, hdusb_copy, ims, qpkg, gen_upnp_desc, scanafpdb
eset_system, umount_all_vdd, sss_convert, httpd_init, get_hwsn, get_suid, setsum, getsum, rsyslog_util, radius_util, send_alert_mail, rsync_util
acl_cmd check_ldap clean_reset_pwd network_boot_rescan
I used google on this one but could not find anything useful.
I am looking for a command on this linux system without smartctl to give me a list of the installed hard drives with their SMART status.
Has anyone an idea?
Thank you very much in advance!
actually, I was able to find the answer using email and contacts at Fujitsu.
The answer was simple as can be:
# get_hd_smartinfo -d 1
1 is disk 1. Replace with 2 if want to check disk 2.
I did not test it yet, as soon as I have, i'll accept the answer for everyone to see.

Node.js profiling with Dtrace not showing functions names

I'm trying to profile CPU utilization for my project using DTrace. I just followed the script
The problem is that it doesn't show the functions names. The stacks.out file looks like that:
CPU ID FUNCTION:NAME
0 73700 :tick-60s
node`_ZN2v86Object3GetENS_6HandleINS_5ValueEEE+0x1
node`_ZN4node7TCPWrap12OnConnectionEP11uv_stream_si+0x14b
node`uv__server_io+0xbf
node`uv__io_poll+0x259
node`uv_run+0xda
node`_ZN4node5StartEiPPc+0x16d
node`main+0x1b
node`_start+0x83
1
node`_ZN4node6Buffer4FillERKN2v89ArgumentsE+0x22b
0x82474d1b
0x8241ee9c
0x8241e624
0x8241e30f
0x82426abd
0x82419e4e
0x82419948
0x82413fc3
0x8d20e501
0x82416f65
0x82413f98
0x8243e7d3
0x824186c0
0x8246e8cf
0x8d221899
0x8d21308a
node`_ZN2v88internalL6InvokeEbNS0_6HandleINS0_10JSFunctionEEENS1_INS0_6ObjectEEEiPS5_Pb+0x101
node`_ZN2v88internal9Execution4CallENS0_6HandleINS0_6ObjectEEES4_iPS4_Pbb+0xc9
node`_ZN2v88Function4CallENS_6HandleINS_6ObjectEEEiPNS1_INS_5ValueEEE+0x10b
node`_ZN4node12MakeCallbackEN2v86HandleINS0_6ObjectEEENS1_INS0_8FunctionEEEiPNS1_INS0_5ValueEEE+0x4c
node`_ZN4node12MakeCallbackEN2v86HandleINS0_6ObjectEEENS1_INS0_6StringEEEiPNS1_INS0_5ValueEEE+0x66
node`_ZN4node10StreamWrap12OnReadCommonEP11uv_stream_si8uv_buf_t14uv_handle_type+0x14f
node`_ZN4node10StreamWrap6OnReadEP11uv_stream_si8uv_buf_t+0x2e
node`uv__read+0x281
node`uv__stream_io+0x131
node`uv__io_poll+0x259
node`uv_run+0xda
node`_ZN4node5StartEiPPc+0x16d
node`main+0x1b
node`_start+0x83
1
The flame graph shows hex (ie. "0x82474d1b") as functions calls and not the actual function name.
I'm using OmniOS (illumos based OS) vagrant box r151002 and did this (https://gist.github.com/dalssoft/5595688) to build node.js 32bits on OmniOS.
Any thoughts?
I had similar issues when writing my blog post about using openindiana and node.js together.
http://blog.nearform.com/blog/node-openindiana.html
I found being explicit with the cpu arch was the way to resolve it.
./configure --with-dtrace --dest-cpu=x64
Don't forget to demangle the c++ symbols too
c++filt < stacks.out > demangled.out

Match a pid to an application desktop schema on Linux

All standards compliant applications in Linux store a desktop schema in /usr/share/applications/. In my particular use case, I have a WnckWindow data structure and I can get a pid from that. Using this pid, I can extract the command line from the proc.
Unfortunately, it seems that the proc command line entry does not match the desktop schema launch parameters. For example, the 'thunderbird' application is launched via /usr/bin/thunderbird but this is just a shell script which activates the real executable: /usr/lib/thunderbird-8.0/thunderbird-bin.
The real executable cannot be launched directly as it is dependent on the library paths configured in the /usr/bin/thunderbird script. Does anyone have any advice on how to match process id numbers to the appropriate desktop schema without getting caught by the issue I've described?, Thanks.
Ok, well, it appears that there's no nice way of solving this using the pid, however, it is relatively easy to match the Wnck windows class to application desktop schemas. The Wnck windows class needs to be preprocessed a little first to ensure that the filter works but it's pretty trivial stuff. Once you've got a good set of target strings, eg 'Thunderbird' or 'Google' + 'Chrome', you can use the system application menu API to zero in on a likely candidate, for example, by using garcon on Xfce.

Resources