Advertisement
oquidave

ssh and query db

May 14th, 2011
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #ssh module
  3. import paramiko
  4.  
  5. '''remote/ssh to a pc(smsinfo.zain.co.ug) login
  6. to a postgres db
  7. execute a db querry
  8. return the result'''
  9.  
  10. hostname = '192.1968.2.178'
  11. port = 22
  12. username = 'dokwii'
  13. password = 'okwi1478'
  14. #cmd = 'tail -f /tmp/mt_billing.log'
  15. sql = "select count(*) from mt_bill20110514 where status='t';"
  16. #create ssh object
  17. ssh = paramiko.SSHClient()
  18. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  19. #connect
  20. try:
  21. ssh.connect('smsinfo.zain.co.ug',username=username, password=password)
  22. except:
  23. print 'failed to connect to remote host'
  24. #now lets login to the db
  25. try:
  26. ssh.exec_command('psql kannel postgres')
  27. except:
  28. print 'failed to login to db'
  29. #now lets query the db
  30. try:
  31. stdin, stdout, stderr = ssh.exec_command(sql)
  32. db_count = stdout.read()
  33. print db_count
  34.  
  35. except:
  36. print 'failed to querry db'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement