Unable to get AppPackage and AppActivity - automation

I know that we use adb shell and then dumpsys window windows | grep -E ‘mCurrentFocus’ to find the appPackage and appActivity using adb commands. But when I open an app and run this command I'm not getting anything.

Using AndroidViewClient/culebra you can just run
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from com.dtmilano.android.viewclient import ViewClient
print(ViewClient.connectToDeviceOrExit()[0].getTopActivityName())
and will print something like
com.android.chrome/com.google.android.apps.chrome.Main

Related

How to run a terminal command using python script, which is held through wsgi process?

I have a Centos 7 server with cPanel and I'm working on a Telegram bot for my business needs. The bot should be able to run a terminal command with os.system or subprocess.Popen, however both options do not work when configured through a webhook + wsgi process.
I tested both with bot.polling method and they worked as a charm, however after I switched to webhook method served by flask and wsgi, both stopped working for me. I have tried the following:
mycommand = "python3.6 GoReport.py --id 31-33 --format word"
os.chdir('dir_to_run_command_from')
os.system(mycommand)
and the following one:
mycommand = "python3.6 GoReport.py --id 31-33 --format word"
subprocess.Popen(mycommand, cwd="dir_to_run_command_from", shell=True)
Both options simply do nothing right now. I tried to print them both and received 0 as a response. I wonder if the issue is caused by permissions or something.
I expect both options to work through webhook + wsgi as good as they work through bot.polling method.
I think I got it wrong. Your script writes a report to a specific directory. You do not need a result in your application route.
I wrote a small test application called tryout. It runs in a virtual environment.
$ mkdir tryout
$ cd tryout
$ python3 -m venv tryout
$ source tryout/bin/activate
$ export FLASK_APP=tryout/app
$ export FLASK_ENV=development
$ flask run
Directory structure:
/tryout
/app/*
/bin/*
/include/*
/lib/*
/subdir/*
Application:
# /tryout/app/__init__.py
import sys, os
from flask import Flask
def create_app(env=os.getenv('FLASK_ENV', 'development')):
app = Flask(__name__)
#app.route('/run-script')
def run_script():
import subprocess
cmd = 'python script.py'
cwd = 'subdir'
ret = subprocess.check_output(cmd, cwd=cwd, shell=True)
print(ret)
return ret, 200
return app
app = create_app()
Script:
# /subdir/script.py
import os, sys
def main():
with open('report.txt', 'w+') as fp:
fp.write('Info\n')
sys.stdout.write('It works!')
if __name__ == '__main__':
main()
It works!
A new file named "report.log" is written into the "subdir"-directory.
In Browser appears "It works!".
Hope I could help you or I have no real idea of what you want to do.
If you want to run an external script from inside flask, you could use subprocess to run the script from the command line. This is the right solution.
#app.route('/run-script')
def run_script():
cmd = '<your command here!>'
result = subprocess.check_output(cmd, cwd='<your wordir>', shell=True)
return render_template('results.html', **locals())
Have fun!
#Bogdan Kozlowskyi
Is it possible to pipe on the command line? Do you need to return a result to the user?
cmd = 'first_cmd | tee report.log'
result = subprocess.check_output(cmd, cwd='<your wordir>', shell=True)
Perhaps you should look for shell commands like '>>', '>' and 'tee'.
Seems to be a user-groups permission problem (execute and write).

scrapy shell url return SyntaxError in iPython notebook

In windows power shell ,I can run scrapy shell 'http://www.hao123.com',
scrapy shell 'http://www.hao123.com
I can run ipython
I can run ipython but not scrapy shell 'http://www.hao123.com'
ipython then scrapy shell 'http://www.hao123.com
In ipython notebook,I can't run scrapy shell 'http://www.hao123.com'also
scrapy shell 'http://www.hao123.com'
File "<ipython-input-3-be4048c8f90b>", line 1
scrapy shell 'http://www.hao123.com'
^
SyntaxError: invalid syntax
Ipython is installed by anaconda,scrapy is installed by pip,anaconda and pip is in different file.
Please help me!
That's not a feature you can have in ipython. scrapy shell is a command, it's own application completely separate from ipython.
However, there are two things you can do:
If you have a Spider and Response objects from somewhere you can simply use scrapy.shell.inspect_repsonse
from scrapy.shell import inspect_response
# You need a scrapy spider object that has crawler instance attached to it
some_spider = Spider()
# you need response object
some_response = Response()
inspect_response(some_spider, some_response)
# new ipython shell will be embedded, like one from scrapy shell command
Otherwise you can spawn a subprocess:
import subprocess
subprocess.call('scrapy shell http://stackoverflow.com', shell=True)
# new ipython shell will be embedded, like one from scrapy shell command
if you are using Anaconda on windows 7 .
Follow: environment --> root --> Open terminal. Then you can write:
scrapy shell 'http://www.hao123.com'

Write echo output in file when shell execute from os.system

I am running a shell command through jython os.system command. My code is given below:
import os
import sys
vCmdLine = "sh C:/scripts/WAQ.sh PARAM_1 PARAM_2 >> C:/logs/WAQ.log 2>&1"
vCmdRC = os.system(vCmdLine)
My shell has several echo commands which I want to get written in C:/logs/WAQ.log file.
The code is working fine if I execute the same on unix OS. However when I run the same on windows, it does not write anything in C:/logs/WAQ.log file. I am using unix util lib on windows.
What is the issue?
I have changed a code and replace os.system with subprocess.call and the logging is now perfect. Below is my code
import subprocess
vLogFileName = 'C:/logs/WAQ.log'
vCmdLine = "sh C:/scripts/WAQ.sh PARAM_1 PARAM_2"
f = open(vLogFileName, "a")
subprocess.call(vCmdLine, stdout=f)
f.close()
Why the logging is not working with os.system on windows?

Nacl And Pnacl from terminal on chrome device

I've built hello world program by nacl and pnacl tools from NACL_SDK.
And now I want to test it.
I've mad a html page and js script which are working with my nexe and pexe and in browser everything is working fine.
But how can I launch my programs from console?
And how can I write stdout to file?
To run a nexe-program my-app.nexe from console and redirect output to file output.log use the following command:
$NACL_SDK/tools/sel_ldr.py my-app.nexe > output.log
sel_ldr.py is just a helper script. If you pass a --verbose option to it, you'll see a real command used to run your program. It's something like this:
$NACL_SDK/tools/nacl_helper_bootstrap_x86_64 $NACL_SDK/tools/sel_ldr_x86_64 \
--r_debug=0xXXXXXXXXXXXXXXXX --reserved_at_zero=0xXXXXXXXXXXXXXXXX -a \
-B $NACL_SDK/tools/irt_core_x86_64.nexe my-app.nexe

Apache2, Python3, CGI Script

Environment:
Mac OS X 10.8.5
Apache2 (version that came with the OS)
Python2 (version that came with the OS)
Python3 (installed via Homebrew)
This code returns "testing" in the web browser:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from __future__ import print_function, division
print("Content-Type: text/html") # HTML is following.
print() # Blank line, end of headers.
print("testing")
But this code returns "Internal Server Error" in the web browser (using python3 this time):
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
print("Content-Type: text/html") # HTML is following.
print() # Blank line, end of headers.
print("testing")
...and in the Apache2 error log:
env: python3: No such file or directory
Premature end of script headers: test_cgi.py
In echo $PATH:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Looking deeper, ls -al /usr/bin/python*:
/usr/bin/python
/usr/bin/python-config
/usr/bin/python2.5 -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5
/usr/bin/python2.5-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5-config
/usr/bin/python2.6 -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6
/usr/bin/python2.6-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6-config
/usr/bin/python2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
/usr/bin/python2.7-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
/usr/bin/pythonw
/usr/bin/pythonw2.5 -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/pythonw2.5
/usr/bin/pythonw2.6 -> ../../System/Library/Frameworks/Python.framework/Versions/2.6/bin/pythonw2.6
/usr/bin/pythonw2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
And in ls -al /usr/local/bin/python*:
/usr/local/bin/python3 -> ../Cellar/python3/3.3.3/bin/python3
/usr/local/bin/python3.3 -> ../Cellar/python3/3.3.3/bin/python3.3
/usr/local/bin/python3.3-config -> ../Cellar/python3/3.3.3/bin/python3.3-config
/usr/local/bin/pythonw3.3 -> ../Cellar/python3/3.3.3/bin/pythonw3.3
Questions:
Since the first item in my PATH is /usr/local/bin, why can't Apache find Python3 ?
How can I tell Apache to use Python3 ?
Thank you for your help :)
I have the exact same problem; I cannot figure out how to get apache to recognize python3. Furthermore, if you specify the interpreter manually:
#!/usr/bin/python
Works, however:
#!/usr/local/bin/python3
Complains about a "malformed header from script" in the apache error log... I have no idea why this is an issue as it should just be running an arbitrary interpreter.
EDIT
Okay, so my problem was actually just me not outputting header information. Your issue is that no python3 environment variable has been set. Try changing the first line to:
#!/usr/local/bin/python3