Link to home
Start Free TrialLog in
Avatar of EEBbrown
EEBbrown

asked on

ssh using public key with Python

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
Avatar of clockwatcher
clockwatcher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EEBbrown
EEBbrown

ASKER

Thanks clockwatcher, I will try this out early in the week and let you know if i'm successful