Python Forum
New to MySQL, can't work out why code doesn't work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to MySQL, can't work out why code doesn't work
#1
I'm creating a script called rootrun, it runs things as root that were put in a MySQL database but only if they match regular expressions in the database. It should be telling me "hello word" but it doesn't.

The database is pretty simple:
MariaDB [rootrun]> SHOW TABLES;
+-------------------+
| Tables_in_rootrun |
+-------------------+
| allowed           |
| operations        |
+-------------------+
2 rows in set (0.001 sec)

MariaDB [rootrun]> SELECT * FROM allowed;
+----+------+
| id | rule |
+----+------+
|  1 | (.*) |
+----+------+
1 row in set (0.000 sec)

MariaDB [rootrun]> SELECT * FROM operations;
+----+------------------+
| id | oper             |
+----+------------------+
|  1 | echo hello world |
+----+------------------+
1 row in set (0.000 sec)
Keep in mind the regexp there is meant to just let everything through for now.

Here is the script which uses that database, but doesn't seem to be working. It doesn't return any output like it should.

#!/usr/bin/python3
import mysql.connector
import re
import os

global mydb

mydb = mysql.connector.connect(
        host="localhost",
        user="rootrun",
        passwd="4tFZYUfey8Lv5Vg3Gc7q8XE4",
        database="rootrun")

def allowExec(rootcmd):
    global mydb;
    cursor = mydb.cursor()
    cursor.execute("SELECT rule FROM allowed")
    result = cursor.fetchall()

    itran = False

    for rule in result:
        if (re.match(str(rule), str(rootcmd))):
            if (itran == False):
                os.system(rootcmd)
            itran = True


def runOperations():
    cursor = mydb.cursor()
    cursor.execute("SELECT id FROM operations")
    result = cursor.fetchall()

    for operid in result:
        cursor.execute("SELECT oper FROM operations WHERE id=\'" + str(operid) +"'")
        operation = cursor.fetchall()
        allowExec(operation)
        cursor.execute("DELETE FROM operations WHERE id=\'" + str(operid) +"'")

runOperations()
Reply
#2
1. You are not doing anything with the result of the cursor.execute() on line 35, e.g. fetch, print/return. etc.
2. even if you did, the result will not be what you expect. result (line 32) is tuple of tuples. So operid is tuple and str(operid) will have extra brackets, so your SELECT will return empty tuple
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
The 'result' variable on line 32 is used in the foreach loop for operid. Can you demonstrate your point with code?
Reply
#4
(Feb-18-2019, 04:32 PM)kintarowonders Wrote: The 'result' variable on line 32 is used in the foreach loop for operid. Can you demonstrate your point with code?
yes it is used in the for loop and operid (which is tuple) is used in second SELECT query, but you do nothing with the result of this second query
def runOperations():
    cursor = mydb.cursor()
    cursor.execute("SELECT id FROM operations")
    result = cursor.fetchall()
 
    for operid in result:
        print(operid)
        cmd = "SELECT oper FROM operations WHERE id=\'" + str(operid) +"'"
        print(cmd)
        cursor.execute(cmd)
        print(cursor.fetchall())
by the way the way you construct cmd is potentially dangerous. Use like this
cursor.execute("SELECT oper FROM operations WHERE id=%s", operid)
not tested, you may need '
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Alright, here is how I fixed it up, but for some reason the row isn't deleted by the database on line 48, and now I'm stuck there.

#!/usr/bin/python3
import mysql.connector
import re
import os
from itertools import chain

global mydb

mydb = mysql.connector.connect(
        host="localhost",
        user="rootrun",
        passwd="4tFZYUfey8Lv5Vg3Gc7q8XE4",
        database="rootrun")

def allowExec(rootcmd):
    global mydb;
    cursor = mydb.cursor()
    cursor.execute("SELECT rule FROM allowed")
    result = cursor.fetchall()

    itran = False

    lresult = list(result)

    for rule in lresult:
        frule = rule[0]
        if (re.match(str(frule), str(rootcmd[0]))):
            if (itran == False):
                os.system(rootcmd[0])
            itran = True

    print(itran)


def runOperations():
    global mydb
    cursor = mydb.cursor()
    cursor.execute("SELECT id FROM operations")
    result = cursor.fetchall()

    lresult = list(result)

    for operid in lresult:
        idstr = operid[0]
        cursor.execute("SELECT oper FROM operations WHERE id=\'" + str(idstr) +"'")
        operation = cursor.fetchall()
        allowExec(list(operation)[0])
        cursor.execute("DELETE FROM operations WHERE id=\'" + str(idstr) +"'")

runOperations()
Reply
#6
You need to commit the changes
add mydb.commit()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Works now.. Thanks so much!

#!/usr/bin/python3
import mysql.connector
import re
import os
from itertools import chain

global mydb

mydb = mysql.connector.connect(
        host="localhost",
        user="rootrun",
        passwd="4tFZYUfey8Lv5Vg3Gc7q8XE4",
        database="rootrun")

def allowExec(rootcmd):
    global mydb;
    cursor = mydb.cursor()
    cursor.execute("SELECT rule FROM allowed")
    result = cursor.fetchall()

    itran = False

    lresult = list(result)

    for rule in lresult:
        frule = rule[0]
        if (re.match(str(frule), str(rootcmd[0]))):
            if (itran == False):
                os.system(rootcmd[0])
            itran = True

    print(itran)


def runOperations():
    global mydb
    cursor = mydb.cursor()
    cursor.execute("SELECT id FROM operations")
    result = cursor.fetchall()

    lresult = list(result)

    for operid in lresult:
        idstr = operid[0]
        cursor.execute("SELECT oper FROM operations WHERE id=\'" + str(idstr) + "'")
        operation = cursor.fetchall()
        allowExec(list(operation)[0])
        cursor.execute("DELETE FROM operations WHERE id=\'" + str(idstr) + "'")
        mydb.commit()

runOperations()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why does datetime.strptime() not work in this situation Monomial 2 692 Apr-08-2025, 06:36 PM
Last Post: snippsat
  PIP doesn't work YKR 1 711 Mar-28-2025, 02:10 PM
Last Post: snippsat
  Excel password recovery tool for work OTH 6 4,883 Mar-06-2025, 03:49 PM
Last Post: Pedroski55
  I'm trying to install python 3.11.11 on windows 10 - it doesn't work Petonique 2 2,047 Feb-04-2025, 05:42 PM
Last Post: snippsat
  Generators don't work svalencic 7 1,031 Jan-16-2025, 04:16 PM
Last Post: DeaD_EyE
  Trying to Make Steganography Program Work For All Payload Types Stegosaurus 0 1,199 Sep-26-2024, 12:43 PM
Last Post: Stegosaurus
  Can't get graph code to work properly. KDDDC2DS 1 685 Sep-16-2024, 09:17 PM
Last Post: deanhystad
  Questions about while loop - why does one work, and the other variant does not Swgeek 2 889 Aug-29-2024, 07:57 AM
Last Post: Swgeek
  How to make my Telegram bot stop working at 16:15 and not work on Fridays? hus73 2 1,403 Aug-10-2024, 12:06 PM
Last Post: hus73
  Executable file compiled by PyInstaller does not work on Windows 7 amusaber 1 1,911 Jul-11-2024, 02:59 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020