troubleshooting Question

ssh using public key with Python

Avatar of EEBbrown
EEBbrown asked on
Programming Languages-OtherPython
2 Comments1 Solution2453 ViewsLast Modified:
Hello all,
Been a long time since I was a member of this community! Glad to be back!

I am looking for a solution using python, my script currently connects to a mysql db (via IP/Usr/Pwd) and runs a query that outputs a file(.csv). Next I need to ssh into the box (with key) to retrieve (email) said file. These are remote servers "in the cloud". Any suggestions would be appricated, I have paramiko installed along with an ssh python module but I am unsuccessful:


import MySQLdb
import paramiko
import os
import ssh
# Connect to MySQL-Run Query-close connection
   
conn = MySQLdb.connect (host = "10.10.10.10",user = "username here",passwd = "passwd here",db = "dbname here")
cursor = conn.cursor ()
cursor.execute (""" select t.Creator, u.EmailAddress, t.Queue INTO OUTFILE '/mnt/tmp/filename.csv' FIELDS TERMINATED BY ',' OPTIONALLY   ENCLOSED BY '"' LINES TERMINATED BY '\n' from Tickets t, Users u where status != 'resolved' and t.Creator = u.id and t.Queue = 38; """)
cursor.close ()
conn.close ()
   
# ssh to root to get file # Email CSV file

#privatekeyfile = os.path.expanduser('/root/.ssh/Farms/key.pem file here')
# mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
x = ssh.Connection('10.10.10.10', username = 'root', private_key = '/root/.ssh/Farms/key.pem')
cursor =x.cursor()
cusor.execute("""gzip -m9 /mnt/tmp/filename.csv ; uuencode /mnt/tmp/filename.csv.gz filename.csv.gz | mail -s "Emails" recipient@here.com""")
ASKER CERTIFIED SOLUTION
clockwatcherProgrammer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros