Python测试数据库连通性:
#!/usr/bin/python27 #encoding: utf-8 import ibm_db import os import sys def find_db(filename,db_name): f = open(filename) for line in f: file_db = line.strip().split(',')[0] if file_db == db_name: db_list = line.strip().split(',') break f.close() return db_list def test_conn(db_name,db_host,db_port,db_user,db_pwd): db2 = "DATABASE=%s;HOSTNAME=%s;PORT=%s;PROTOCOL=TCPIP;UID=%s;PWD=%s;" %(db_name,db_host,db_port,db_user,db_pwd) try: conn = ibm_db.connect(db2, "", "") except: print "0" else: print "1" ibm_db.close(conn) if __name__ == "__main__": file_path = os.path.join(os.path.split(os.path.realpath(__file__))[0],'db1.cfg') conn_db = find_db(file_path,sys.argv[1]) db_name,db_host,db_port,db_user,db_pwd = tuple(conn_db) test_conn(db_name,db_host,db_port,db_user,db_pwd)
db.cfg
PTEST1,192.168.2.180,50000,db2inst1,db2inst1 PTEST,192.168.2.180,50001,db2inst1,db2inst1 PTEST2,192.168.2.180,50000,db2inst1,db2inst1