python os.system from input nor working - python

#!/usr/bin/env python
import os
import subprocess
mail=raw_input("")
os.system("mail" + mail + "<<<test")
When I run this program I`ve got error :sh: 1: Syntax error: redirection unexpected.
The script must send mail using mailutilis

Don't use os.system. Replace it with subprocess.Popen:
address = raw_input()
with open('test') as text:
subprocess.Popen(["mail", address], stdin=text).wait()

Related

Running cmd command and printing its output with in python

im using an email lookup module, called holehe (more can be found on it here - https://github.com/megadose/holehe) and i want to make it so when you enter an email it will automatically with in your python console output what came out from the new CMD window, makes it easier for my and colleges to use. How can i go about this? My code it bellow
import holehe
import os
from os import system
import subprocess
email = input("Email:")
p = subprocess.Popen(["start", "cmd", "/k", "holehe", email], shell = True)
p.wait()
input()
Thank you for answers

How to capture bash command output using python script

How to capture bash command output using python script.
For eg:
running below in linux :
[root#xxxxxx oracle]# echo sumit
sumit
[root#xxxxxx oracle]#
How can i re print only the above output using python script ? like running python test.py shoud give 'sumit' as output. i tried below:
test.py :
import sys
sys.stdout.flush()
out = sys.stdin.readline()
print(out)
Above prints only the input i type but not the already displayed output
With subprocess, you can run commands and check their return code, stdout and stderr outputs. Would that help?
For example:
import subprocess as proc
byte_output = proc.check_output(["ls", "-1", "."])
str_output = str(byte_output, "utf-8")
print(str_output)
# prints my local folders dev\ngit

How to access the output of a program via pipe in python

I'm trying to pipe the output of a python script using os.popen() . Here my python script :
sample.py
while(True):
print("hello")
python version : 3.6.7
os : ubuntu 18.04
My script to do the process :
import os
import types
def sample_function():
pipe = os.popen('python3 /home/gomathi/sample.py')
while(True):
a = pipe.readline()
yield a
s=sample_function()
for i in s:
print(i)
It works well for the above code.Now the problem is , i have changed the sample.py as follows :
sample.py
print("hello")
It just print blank for the entire screen and continues printing blank characters . What went wrong with my code ? What changes to be made in my code to work for the above sample.py ?
Your new sample.py ends, but you keep reading from the pipe. So you're getting empty strings.
Use the subprocess module, with the same function Popen and redirect the output and the error to the print function of your main .py file.
from subprocess import Popen, PIPE
command = ['python3', '/home/gomathi/sample.py']
process = Popen(command, shell=False, stdout=PIPE, stdin=PIPE)
while True:
line = process.stdout.readline() #variable_name_changed
print(line)

how to use netstat -nb in python

I want to use netstat -nb in python but every code that i write i get the same msg: "The requested operation requires elevation."
The last code that i try is
import os
output_command = os.popen("netstat -nb").readlines()
and i try also
import subprocess
program_list = subprocess.run(["netstat", "-nb"], stdout=subprocess.PIPE).stdout.decode("utf-8")
program_list = program_list.split("\r\n")
import os
a=os.popen('netstat -nb').read()
print("\n Connections ",a )
try this, it's working!

How to execute python script from another python script and send it's output in text file

I have a script test.py which is used for server automation task and have other script server.py which list out all server name.
server.py list all sevrer name in text.log file and this log file is used by test.py as a input.
I want one single script test.py to execute server.py from inside it and also redirect server.py script output to text.log file as well.
So far i have tried with execfile("server.py") >text.log in test.py which didn't worked.
Use subprocess.call with stdout argument:
import subprocess
import sys
with open('text.log', 'w') as f:
subprocess.call([sys.executable, 'server.py'], stdout=f)
# ADD stderr=subprocess.STDOUT if you want also catch standard error output

Categories

Resources