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


Messages In This Thread
New to MySQL, can't work out why code doesn't work - by kintarowonders - Feb-18-2019, 03:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  equalto validator doesnt work robertkwild 1 216 Jun-02-2024, 06:16 AM
Last Post: Pedroski55
  I can't for the life of me get this basic If statement code to work CandleType1a 8 447 May-21-2024, 03:58 PM
Last Post: CandleType1a
  Extending list doesn't work as expected mmhmjanssen 2 348 May-09-2024, 05:39 PM
Last Post: Pedroski55
  Using xml.parsers.expat I can't get parser.ParseFile(f) to work Pedroski55 3 389 Apr-24-2024, 07:36 AM
Last Post: Pedroski55
  Making the tab key work like a tab key jackg 3 372 Apr-16-2024, 09:02 PM
Last Post: deanhystad
  print doesnt work in a function ony 2 434 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  Need some help trying to get my Python mortgage amortization calculator to work prope IamSirAskAlot 4 15,933 Feb-12-2024, 10:53 PM
Last Post: BerniceBerger
  Multiprocessing: Threads work well. Processes don't work. viyubu 11 2,088 Dec-03-2023, 08:50 PM
Last Post: snippsat
  Pydoc documentation doesnt work Cosmosso 5 4,596 Nov-25-2023, 11:17 PM
Last Post: vidito
  hi need help to make this code work correctly atulkul1985 5 981 Nov-20-2023, 04:38 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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