1

I have a SQLite3 table named DEVICES that has a list of ip addresses from my home network. I want to run an arp-scan against those ip addresses. Notice, this is the data in my DEVICES table exported to a csv file:

ipAddr,macAddr,Description,OS,Notes

192.168.0.1,,,,

192.168.0.134,,,,

Now when I run the following code I feel like the data I'm getting back from the SQLite3 is not correct. Notice the PRINT stmts:

    cursorObj.execute("SELECT ipAddr FROM Devices")
    rows = cursorObj.fetchall()
    print("rows="+str(rows))
    for ipAddr in rows:
        print("working on ipAddr="+str(ipAddr))
        macAddr = os.popen('sudo arp-scan '+str(ipAddr)+' | grep MAC: | cut -f 6- -d \' \' | cut -c1-17').read()
        print("MAC: "+macAddr+" found for IP: "+str(ipAddr))

MY LOGS:

rows=[('192.168.0.1',), ('192.168.0.134',)]

working on ipAddr=**('**192.168.0.1**',)**

/bin/sh: 1: Syntax error: "(" unexpected
MAC:  found for IP: **('192.168.0.1',)**

Why is my "ipAddr=('192.168.0.1',)" and not simply "ipAddr=192.168.0.1" just as it is in the DB?

buran
  • 13,682
  • 10
  • 36
  • 61
qbert85
  • 23
  • 2
  • 2
    `fetchall` returns a list of tuples. – Shawn Feb 20 '21 at 21:01
  • Thanks @Shawn. In light of this (in case anyone wants to know) i modified my code as follows and it seems to work fine now:\n\n ``` for row in rows: #LOOP THROUGH LIST for ipAddr in row: #LOOP THROUGH TUPLE print("working on ipAddr="+str(ipAddr)) ``` – qbert85 Feb 20 '21 at 21:24

2 Answers2

0

Thanks @Shawn. In light of this (in case anyone wants to know) i modified my code as follows and it seems to work fine now:\n\n

for row in rows: #LOOP THROUGH LIST
    for ipAddr in row: #LOOP THROUGH TUPLE
        print("working on ipAddr="+str(ipAddr))
qbert85
  • 23
  • 2
0

Instead of iterating over each individual tuple returned from cursorObj.fetchall, you can use unpacking in your first for loop:

for ipAddr, *_ in rows:
   pass

*_ is pointing to any other values in the tuple, after ipAddr, in _, a throwaway variable.

Since your database has multiple columns, unpacking helps you avoid iterating over any non-ip values in the tuples.

Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • Good to know! Thanks! However, since I'm grabbing just one column: cursorObj.execute("SELECT ipAddr FROM Devices") I don't think there's any concern of iterating over my non-ip columns. – qbert85 Feb 22 '21 at 15:01