1. 程式人生 > >Python腳本之利用django---mysql將磁盤文件系統寫入數據庫保存

Python腳本之利用django---mysql將磁盤文件系統寫入數據庫保存

filesystem

/usr/lib/python2.6/site-packages/django/contrib/admin/templates/admin/base_site.html#修改登錄名稱

############################################

[[email protected] myweb]# python

Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48)

[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import os

>>> import tab,sys

>>> sys.path.append(‘/tmp/python/Django-1.5.1/django/bin/myweb‘)

>>> os.environ[‘DJANGO_SETTINGS_MODULE‘] = ‘myweb.settings‘

>>> from pyweb.models import Publisher

>>> p1 = Publisher(name=‘xushanghai1‘, address=‘24242 chuansha road‘,city=‘ShangHai‘, state_province=‘CN‘, country=‘China‘,website=‘http://www.xxk.com/‘)

>>> p1.save()

#####################################################寫入數據庫

#!/bin/usr/bin python

import os,datetime,paramiko

import tab,sys

sys.path.append(‘/tmp/python/Django-1.5.1/django/bin/myweb‘)

os.environ[‘DJANGO_SETTINGS_MODULE‘] = ‘myweb.settings‘

from pyweb.models import Filesystem

hosts=[‘192.168.1.10‘,‘192.168.1.11‘,‘192.168.1.13‘]

username=‘root‘

password=‘123456‘

port=22

for ip in hosts:

if os.system(‘ping %s -c 1‘%ip)==0:

paramiko.util.log_to_file(‘paramiko.log‘)

s = paramiko.SSHClient()

s.set_missing_host_key_policy(paramiko.AutoAddPolicy())

s.connect(ip,port,username,password)

stdin,stdout,stderr=s.exec_command(‘df -kP‘)

df= stdout.read().split(‘\n‘)

s.close()

else:

df= ‘nul \n null null null null null null \n‘.split(‘\n‘)

time=datetime.datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘)

# df=os.popen(‘df -kP‘).read().split(‘\n‘)

df.pop(0)

df.pop(-1)

for line in df:


list=line.split()

p1 = Filesystem(ip=‘%s‘%ip,date_time=‘%s‘%time,Filesystem=‘%s‘%list[0], sum_kb=‘%s‘%list[1],Used=‘%s‘%list[2],

Available=‘%s‘%list[3], Capacity=‘%s‘%list[4],Mounted_on=‘%s‘%list[5])

p1.save()


#########################

import datetime

datetime.datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘);


Python腳本之利用django---mysql將磁盤文件系統寫入數據庫保存