0

I decided to try to do a code to automatically convert a ducky code into a python code (at least at a simple level), and the first thing I wanted to do was changing the STRING in the ducky code to python using pynput. The equivalent of STRING is keyboard.type(), defining keyboard as pynput.keyboard.Controller().

So, for example, the code should turn this:

STRING hello
STRING how are you?

Into this:

keyboard.type("hello")
keyboard.type("how are you?")

What I've tried was this:

ducky=input("Introduce your ducky code... \n")
py=ducky.replace("STRING", '''keyboard.type("''')

That code works, but it doesn't put the quotation marks and the parenthesis at the end of each line, and I don't know how to do that. Can anyone help me? Also, I'm a beginner, and I tried to use .replace for this because I know how it works, but if there is something better to use in this case, please, tell me. Thanks.

MKing
  • 11
  • 1

1 Answers1

0

You have to split your string into lines, then add the 'keyboard.type("' and the '")' to it, and finally rejoin the lines.

py=[]
for line in ducky.splitlines():
    print(line)
    if line.startswith("STRING "):
        py.append('keyboard.type("'+line[7:]+'")')
    else:
        py.append(line)

py="\n".join(py)
    
The_spider
  • 1,202
  • 1
  • 8
  • 18