简体   繁体   中英

In python, how do you input data from a list into a linux command?

I'm trying to write a simple script that takes a list of words I've created in a text file on linux and runs it through a program that checks the word against a steganography extractor.

The program (steghide) uses the following syntax in the command:

steghide --extract -p {password here} -sf {filename here}

I've been able to call the file and set up a for loop for the words in the list, but can not find a way to input the word from that iteration into each command.

Here's how I've been trying to work it.

import os
import sys

script, filename = argv
filename2 = sys.open(filename)

for word in filename2:
    os.system('steghide --extract -p [want to put something here] -sf stegfilename')

I'm on a controlled box and can't download anything beyond what I already have. Any help is appreciated.

Update:

I got it to work. But now I'm trying to get it to exit out if it finds the correct answer. I am just having a hard time getting Python to read the output. Here's what I have so far.

`import subprocess from sys import argv

script, filename = argv passes = filename

with open(passes) as f: for line in f: proc = subprocess.popen(['steghide', '--extract', '-p' line.strip(), '-sf', 'stegged file name'],stdout = subprocess.PIPE) stdout = proc.communicate()[0] output = proc.stdout.readline()

    if 'wrote' in output:
        print 'YOU FOUND IT!'
        break
    else:
        print line`

This is a good time to learn string formating options in Python. They let you insert values dynamically into a string. An example:

"This {0} is an example".format([1,2,3])
>>>> "This [1,2,3] is an example"

In this particular case, you want to do

value = 'foo' # the item you want to insert - replace with list, number, etc.
...
for word in filename2:
    os.system('steghide --extract -p {0} -sf stegfilename'.format(value))

This will insert the value into your string, and then call steghide on that string.

Use the subprocess module instead; it gives you more options/control and os.system() is deprecated.

import subproces
with open(filename, "r") as f:
    # assumes that you have one word per line
    for line in f:
        subprocess.call(['steghide', '--extract', '-p', line.strip(), '-sf', stegfilename])
        # or if you want the output of running Niels Provos' cool, old tool :)
        cmd_output = subprocess.check_output(['steghide', '--extract', '-p', line.strip(), '-sf', stegfilename])

Use subprocess.check_call passing a list of args:

from subprocess import check_call


for word in map(str.rstrip, filename2):    
    check_call(['steghide', "--extract", "-p", word, "-sf", "stegfilename'"])

String formatting is the subject here.

import os
import sys

script, filename = argv
filename2 = sys.open(filename)

for word in filename2:
    os.system('steghide --extract -p ' +[want to put something here]+ '-sf stegfilename')

To join all elements of a list in a string, try python join :

'  '.join(your_variable)

Example:

var = ['t', 't', 't', 't', 't', 't'] 
joined = ' '.join(var)
print(joined)
print(type(joined))

tttttt

class 'str'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM