We help IT Professionals succeed at work.
Get Started

ssh using public key with Python

2,451 Views
Last Modified: 2012-06-21
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""")
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE