I am trying to write a Python script which will use openssl command to output contents of few digital certificates. The problem is that I am not able to loop through the files with the subprocess.check_output([]) function. Here's what I have got so far:-
#!/usr/bin/env python3
import subprocess
import os
import glob
for f in glob.glob("*.cer"):
OUT_PUT = subprocess.check_output(['openssl','x509','-in','f','-noout','-text'])
print(type(OUT_PUT))
print(OUT_PUT.decode('utf-8'))
I get a feeling that something's wrong with the way I am placing the "f" variable in the function. The above code does not work.
Please advise.
This may or may not be your problem, but as it stands, you have f in quotes, so you're just passing through a string ("f") rather than the name of the file you wish to pass to openssl.
Related
I am trying to pipe the output of xz to a custom python script:
xz -cd file.pcap.xz | myscripy.py
However, I get an error when the script attempts to run this line:
#!/usr/bin/env python2.7
from __future__ import print_function
import pcap
import io
STDIN_ALIAS = '/proc/self/fd/0'
pcap.pcap(io.open(STDIN_ALIAS, 'r'))
and received an error
pcap.pcap(io.open(STDIN_ALIAS, 'r'))
File "pcap.pyx", line 196, in pcap.pcap.__init__
TypeError: expected string or Unicode object, _io.TextIOWrapper found
I am on Ubuntu 18.04 and running under python 2.7.
You can't use Python to pass in packets from a file to pcap.pcap(). The pypcap library you are using is a thin wrapper around the pcap_open_offline() and pcap_create() C functions, and offers no facilities for passing in a Python file object. This wrapper only accepts a filename or a network interface name, nothing else.
The pcap_open_offline() function does accept - as an alias for stdin, so just pass that in directly:
import pcap
sniffer = pcap.pcap('-')
The error message already tell you what happened. You need a string to the pcap() function, not a file object. To fix this, try
pcap.pcap(io.open(STDIN_ALIAS, 'r').read())
But I am not sure this will work as your file might be binary instead of text. In such case, you may want to open with 'rb' instead of 'r' flag, and do some conversion afterwards (especially if you use Python 3 instead of Python 2.7).
I see another issue: your code is not portable as it depends on this:
STDIN_ALIAS = '/proc/self/fd/0'
A pythonic way to read the stdin is the follows (see Reading binary data from stdin)
import sys
string = sys.stdin.read()
Have you tried upgrading PyPcap to work on Python 3? This could help, since Unicode handling is a lot cleaner and less prone to surprises on Python 3. The appropriate package is available, at least on Debian (and probably derived distros as well). Look for: python3-pypcap.
I am trying to run a batch file through python; however, it is not recognizing the path. It stops reading the path after the space between 'Practice' and 'Folder'. How can I fix this? I've tried the r and using forward and backward slashes. Any help would be awesome. Thank you!
import os
Practice = r"C:\Users\Username\Desktop\Practice Folder\Practice.bat"
os.system(Practice)
'C:\Users\Username\Desktop\Practice' is not recognized as an internal
or external command, operable program or batch file.
Change working directory to the script directory as you are using some relative redirection paths. Pushd changes current directory to any drive and can map network drives. The && chains commands and only runs the right hand command if the left hand command succeeds. %UserProfile% is a standard environmental variable which is usually better then using a fixed path of C:\Users\Username.
import os
Practice = r'pushd "%UserProfile%\Desktop\Practice Folder" && Practice.bat'
os.system(Practice)
Try using call from subprocess module.
You need to enclose the command only in double quotes.
from subprocess import call
call(r'"C:\Users\Username\Desktop\Practice Folder\Practice.bat"')
(Notice the order of placing quotes...)
This would even work with os.system() provided you take care the order of quotation marks.
from os import system
system(r'"C:\Users\Username\Desktop\Practice Folder\Practice.bat"')
This should help fix your problem.
You probably need to use two types of quotation marks e.g.
import os
Practice = r"'C:\Users\Username\Desktop\Practice Folder\Practice.bat'"
os.system(Practice)
As it is, your string does not contain quotation marks - you need to include quotation marks within your string or else Windows will think that Folder\Practice.bat is an argument to the command rather than a continuation of the file path
Try this
import os
Practice = os.path.abspath(r"C:\Users\Username\Desktop\Practice Folder\Practice.bat")
Edit:
Something like this worked for me
os.system(r'"C:\Users\Username\Desktop\Practice Folder\Practice.bat"')
I need you help with nmap script and printing the output to csv file.
When I run the script and finish it with print(nm.csv()) I got the following results displayed which is what I want first place:
host;hostname;hostname_type;protocol;port;name;state;product;extrainfo;reason;version;conf;cpe
82.214.228.176;176.228.214.82.in-addr.arpa;PTR;tcp;21;ftp;open;;;syn-ack;;3;
82.214.228.176;176.228.214.82.in-addr.arpa;PTR;tcp;22;ssh;open;;;syn-ack;;3;
82.214.228.176;176.228.214.82.in-addr.arpa;PTR;tcp;53;domain;open;;;syn-ack;;3;
82.214.228.176;176.228.214.82.in-addr.arpa;PTR;tcp;80;http;open;;;syn-ack;;3;
But my question is how to get this redirected or to create a csv file on the same Directory from where I run the script or maybe in a path.
Thanks in advance!
You could declare a function like this one
def save_csv_data(nm_csv, path='.'):
with open(path + '/output.csv', 'w') as output:
output.write(nm_csv)
then by passing an argument you could specify another path to save the file, or otherwise, use the same directory as the script:
if (len(sys.argv) > 1 and sys.argv[1]):
save_csv_data(nm.csv(), path=sys.argv[1])
else:
save_csv_data(nm.csv())
Edit: also remember to import sys
I also would suggest you, if you decide to use arguments, use a module like argparse.
print (nm.csv(),file=open('a.csv','w'))
Edited:
You can use the print_function to get the print() behaviour from python3 in python2:
from __future__ import print_function
I made this code and it is working but only in Linux.
import subprocess as sub
sub.Popen([r"Rscript","diccionari.R"])
Where "diccionari.R" is the name of my script in R.
Error text message: System can't found the specific file.
Can somebody help me and do that it works on windows please?
Thank you.
You should probably try the slashes the other way around as how I said it earlier.
Using full path to the .r script (e.g. "C:/myfolder/diccionari.R") instead of just the script file, and using OS independent slashes.
You should specify where Rscriptis located i.e
import subprocess as sub
cmd_line = [r"C:\\Program Files\\R\\R-3.6.0\\bin\\Rscript", "diccionari.R"]
sub.Popen(cmd_line)
watch for the \\ characters
I'm writing a small script, that should be able to handle multiple files. So I've added that files can be passed comma seperated, and do a arg.split(',') and then handle each one.
Now I've wanted to add asterisk as input possibility like
python myPythonScript.py -i folder/*
If I print the the argument to option -i right when I access it the first time I get
folder/firstFileInFolder.txt
But if I call my script with
python myPythonScript.py -i someFolder/someFile,folder/*
it works just fine. Does anyone have an idea, why python might behave that way?
Try to run this script
import sys
for arg in sys.argv:
print arg
python script.py *
your shell expands the asterisk before python sees it.
As mentioned in the comments, your shell is expanding the asterisk for the non-comma separated case. If you know that the user may specify an asterisk as part of a file name as in your second example, you can have Python do the path expansion by using the glob module.
from glob import glob
glob('*')
code which would allow either the shell or Python to do asterisk expansion may look something like this:
import glob
file_list = []
for pattern in sys.argv[1:]:
file_list.extend(glob.glob(pattern))
In your case, using a comma as a separator would then prevent you from using a comma as part of a filename.