darrenm
February 7th, 2007, 10:08 AM
Hi. Venturing into Python a little bit and I'm trying to write a script that pulls some info from a MySQL database and runs an SSH command using these results.
The code at the moment is:
import MySQLdb
def sqlMeat(Company, Column, Table):
Con = MySQLdb.Connect(host="server", port=3306, user="darrenm", passwd="password", db="adb")
Cursor = Con.cursor()
sql = "select " + Column + " from " + Table + " where CompanyRef = '" + Company + "'"
Cursor.execute(sql)
Results = Cursor.fetchall()
Con.close()
return Results
def getCompanyNames():
Con = MySQLdb.Connect(host="server", port=3306, user="darrenm", passwd="password", db="adb")
Cursor = Con.cursor()
sql = "select CompanyRef from adb"
Cursor.execute(sql)
Results = Cursor.fetchall()
Con.close()
return Results
def Connect(IP, Rpw, TsIP, Port):
Command = string.join("ssh -lroot " + IP + " -L*:" + Port + ":" + TsIP + ":3389")
print Command
CompanyList = getCompanyNames()
i=0
while i < len(CompanyList):
Company = CompanyList[i]
Port = 3390
Connect(sqlMeat(Company, "RouterExternalIP", "locations"), sqlMeat(Company, "RootPassword", "table"), sqlMeat(Company, "TermSrvIP", "table"), Port)
Port = Port + 1
i=i+1
Its meant to execute and SSH to lots of servers, one after the other with a port forward across SSH, incrementing one every time.
When I execute it I get this:
Traceback (most recent call last):
File "pyssh.py", line 33, in <module>
Connect(sqlMeat(Company, "RouterExternalIP", "locations"), sqlMeat(Company, "RootPassword", "table"), sqlMeat(Company, "TermSrvIP", "table"), Port)
File "pyssh.py", line 7, in sqlMeat
sql = "select " + Column + " from " + Table + " where CompanyRef = '" + Company + "'"
TypeError: cannot concatenate 'str' and 'tuple' objects
So it seems I can't construct the SQL statement using the parameters passed to the function? Do I need to convert them to a string before I use them? How can I convert a tuple to a string?
Thanks!
The code at the moment is:
import MySQLdb
def sqlMeat(Company, Column, Table):
Con = MySQLdb.Connect(host="server", port=3306, user="darrenm", passwd="password", db="adb")
Cursor = Con.cursor()
sql = "select " + Column + " from " + Table + " where CompanyRef = '" + Company + "'"
Cursor.execute(sql)
Results = Cursor.fetchall()
Con.close()
return Results
def getCompanyNames():
Con = MySQLdb.Connect(host="server", port=3306, user="darrenm", passwd="password", db="adb")
Cursor = Con.cursor()
sql = "select CompanyRef from adb"
Cursor.execute(sql)
Results = Cursor.fetchall()
Con.close()
return Results
def Connect(IP, Rpw, TsIP, Port):
Command = string.join("ssh -lroot " + IP + " -L*:" + Port + ":" + TsIP + ":3389")
print Command
CompanyList = getCompanyNames()
i=0
while i < len(CompanyList):
Company = CompanyList[i]
Port = 3390
Connect(sqlMeat(Company, "RouterExternalIP", "locations"), sqlMeat(Company, "RootPassword", "table"), sqlMeat(Company, "TermSrvIP", "table"), Port)
Port = Port + 1
i=i+1
Its meant to execute and SSH to lots of servers, one after the other with a port forward across SSH, incrementing one every time.
When I execute it I get this:
Traceback (most recent call last):
File "pyssh.py", line 33, in <module>
Connect(sqlMeat(Company, "RouterExternalIP", "locations"), sqlMeat(Company, "RootPassword", "table"), sqlMeat(Company, "TermSrvIP", "table"), Port)
File "pyssh.py", line 7, in sqlMeat
sql = "select " + Column + " from " + Table + " where CompanyRef = '" + Company + "'"
TypeError: cannot concatenate 'str' and 'tuple' objects
So it seems I can't construct the SQL statement using the parameters passed to the function? Do I need to convert them to a string before I use them? How can I convert a tuple to a string?
Thanks!