0

Using Python/Psycopg2/PopstgreSQL and Cron. I'd like to take remote server information(see below) and add it into a PostGreSQL database on the host computer.

    Using #!/usr/bin/python 
import socket 
import commands 
import string 
import os 
hostname = socket.gethostname() 
print hostname 
ip = commands.getoutput("ifconfig").split("\n")[1].split()[1][5:] 
print ip 
os = commands.getoutput("lsb_release -d") 
print os[13:34] 
kernel = commands.getoutput("uname -p") 
print kernel 
reboot = commands.getoutput("who -b") 
print reboot[22:38] 

This is the 'connect to database' script:

#!/usr/bin/python

import psycopg2
import sys

try:
        conn = psycopg2.connect('host=*** dbname=*** user=*** password=***')
        print "Connected to Database"
except:
        print "No Connection"

cur = conn.cursor()#cursor_factory=psycopg2.extras.DictCursor)
try:
        cur.execute('SELECT * FROM new')
        rows = cur.fetchall()
        print "\n Show: \n"
        for row in rows:
                print "   ", row
except:
        print "Not Working"

I'm able to connect, I'm able to pull the data. I need to combine the two scripts and insert the returned information into the database.

1 Answer 1

2

Your local python script would have these lines:

import psycopg2 as db
remote_connection = db.connect('host=that_host dbname=that_db user=user password=pwd')
local_connection = db.connect('host=localhost dbname=local_db user=user password=pwd')
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. After connecting to the database from a remote machine... how would I go about taking information(commands.getouput("uptime")) and take the result and place that into a column?
Your questions are too broad. The format of this site is for very specific questions. You need to research first and only then make your questions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.