場(chǎng)景是這樣的:一個(gè)生產(chǎn)機(jī)房,會(huì)有很多的測(cè)試機(jī)器和生產(chǎn)機(jī)器(也就是30臺(tái)左右吧),由于管理較為混亂導(dǎo)致了哪臺(tái)機(jī)器有人用、哪臺(tái)機(jī)器沒(méi)人用都不清楚,從而產(chǎn)生了一個(gè)想法--利用一臺(tái)機(jī)器來(lái)管理所有的機(jī)器,記錄設(shè)備責(zé)任人、設(shè)備使用狀態(tài)等等信息....那么,為什么選擇python,python足夠簡(jiǎn)單并且擁有豐富的第三方庫(kù)的支持。
最初的想法
由于剛參加工作不久,對(duì)這些東西也都沒(méi)有接觸過(guò),輪崗到某個(gè)部門(mén)需要做出點(diǎn)東西來(lái)(項(xiàng)目是什么還沒(méi)情況,就要做出東西來(lái),沒(méi)辦法硬著頭皮想點(diǎn)子吧)。。。
本想做一個(gè)簡(jiǎn)單點(diǎn)的自動(dòng)化測(cè)試的工具,但這項(xiàng)目的測(cè)試方法和測(cè)試用例暫時(shí)不能使用這種通用的測(cè)試手段(輸入和輸出都確定不了),從而作罷...
那么做點(diǎn)什么東西,經(jīng)常發(fā)現(xiàn)同事們問(wèn)208誰(shuí)用的?201誰(shuí)用的?那IP是我的!!!你是不是把我得網(wǎng)線給拔掉了?242那機(jī)器到底是哪臺(tái)?
突然間,春天來(lái)了,是不是可以做一個(gè)系統(tǒng)用來(lái)檢測(cè)IP和記錄設(shè)備的使用人,甚至可以按需要在某臺(tái)設(shè)備上運(yùn)行一個(gè)腳本或命令?把這個(gè)矮矬窮的想法和leader溝通過(guò)后,確認(rèn)可以做,那么就開(kāi)始吧!!!
設(shè)計(jì)思想
該系統(tǒng)的大概思想:
1. 要獲得所有服務(wù)器的各種信息,需要在任意一臺(tái)服務(wù)器上部署一個(gè)agent作為信息獲取的節(jié)點(diǎn),定時(shí)向管理服務(wù)器節(jié)點(diǎn)發(fā)送服務(wù)器信息數(shù)據(jù)。
2. server作為綜合管理節(jié)點(diǎn),接收并儲(chǔ)存agent提交的信息。
3. 為了方便使用,采用web頁(yè)面的形式做展示。
開(kāi)發(fā)工具選擇
1. 開(kāi)發(fā)語(yǔ)言:python
之所以選擇python,簡(jiǎn)單,第三方庫(kù)豐富,不用造輪子
2. 數(shù)據(jù)庫(kù):mysql
簡(jiǎn)單、易用
3. webpy:web框架
入門(mén)簡(jiǎn)單、部署方便
4. bootstrap:前端框架
不要關(guān)心太多前端問(wèn)題
5. paramiko:python庫(kù),遵循SSH2協(xié)議,支持以加密和認(rèn)證的方式,進(jìn)行遠(yuǎn)程服務(wù)器的連接
通過(guò)SSH方式連接agent服務(wù)器:遠(yuǎn)程運(yùn)行命令、傳輸文件
6. scapy: python庫(kù),可用來(lái)發(fā)送、嗅探、解析和偽造網(wǎng)絡(luò)數(shù)據(jù)包,這里用來(lái)掃描IP
7. MySQLdb: 連接mysql
8. shell 和 python腳本接口: 為其他人提供shell腳本的接口
經(jīng)驗(yàn)分享
1. 前端對(duì)我來(lái)說(shuō)是新東西,從來(lái)沒(méi)弄過(guò),頁(yè)面的動(dòng)畫(huà)效果,腳本運(yùn)行時(shí)的過(guò)渡都是需要考慮的,開(kāi)始考慮利用倒計(jì)時(shí),但是這個(gè)時(shí)間是不可控的,后來(lái)采用ajax來(lái)處理這個(gè)問(wèn)題
2. agent要自動(dòng)部署到每臺(tái)機(jī)器,并可以通過(guò)server來(lái)控制刷新時(shí)間
3. 建立一個(gè)可擴(kuò)展的表是非常重要的,而且一些重要的信息需要寫(xiě)入磁盤(pán),在數(shù)據(jù)庫(kù)失效的情況下,可以從磁盤(pán)獲取數(shù)據(jù)
4. 數(shù)據(jù)庫(kù)的連接,如果長(zhǎng)時(shí)間沒(méi)有操作的話會(huì)超時(shí),要考慮到
... ...
項(xiàng)目結(jié)構(gòu)--webpy
1. website.py為webpy的主程序,設(shè)置了url映射
2. model.py為webpy的url映射類(lèi),處理請(qǐng)求和返回
3. static中存放靜態(tài)資源
4. scripts用來(lái)存放處理的腳本,這里起的名字有些問(wèn)題
連接數(shù)據(jù)庫(kù)
使用MyQSLdb連接mysql,在這里我沒(méi)有使用webpy提供的數(shù)據(jù)庫(kù)接口,而是自己封裝了一套
ssh遠(yuǎn)程連接服務(wù)器
paramiko實(shí)現(xiàn)ssh連接、與數(shù)據(jù)傳輸、執(zhí)行命令和腳本
def executecmd(cmd, host, port=22, user='root', passwd='root'):
??? try:
??????? s = paramiko.SSHClient()
??????? s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
??? s.connect(host, port, user, passwd, timeout = 10)
??? except Exception as e:
??????? s.close()
??????? print e
??????? print 'connet error...'
??????? return
??? try:
??????? stdin,stdout,stderr=s.exec_command(cmd)
??????? #print 'Host: %s......' %host
??????? res = stdout.readlines()
??? except Exception as e:
??????? print 'exec_commmand error...'
??? s.close()
??? return res
def executefile(file, host, port=22, user='root', passwd='root'):
??? try:
??????? s = paramiko.SSHClient()
??????? s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
??? s.connect(host, port, user, passwd,timeout=5)
??????? t = paramiko.Transport((host, port))
??????? t.connect(username=user, password=passwd)
??????? sftp =paramiko.SFTPClient.from_transport(t)
??? except Exception as e:
??????? s.close()
??????? print e
??????? print 'connet error...'
??????? return ''
??? try:
??????? filename = os.path.basename(file)
??????? if filename.find('.sh') >= 0:
??????????? sftp.put(path+'/'+file, '/tmp/tmp_test.sh')
??????????? stdin,stdout,stderr=s.exec_command('sh /tmp/tmp_test.sh 2>/dev/null', timeout=5)
??????? else:
??????????? sftp.put(path+'/'+file, '/tmp/tmp_test.py')
??????????? stdin,stdout,stderr=s.exec_command('python /tmp/tmp_test.py', timeout=5)
??????? #stdin,stdout,stderr=s.exec_command('rm -rf /tmp/tmp_test* 2>/dev/null')
??????? res = stdout.readlines()
??????? s.exec_command('rm -rf /tmp/tmp_test* 2>/dev/null')
??? except Exception as e:
??????? s.exec_command('rm -rf /tmp/tmp_test* 2>/dev/null')
??????? print 'timeout error...'
??????? print e
??????? return ''
??? return res
IP掃描
使用scapy進(jìn)行IP掃描
def pro(ip, cc, handle):
??? global dict
??? dst = ip + str(cc)
??? packet = IP(dst=dst, ttl=20)/ICMP()
??? reply = sr1(packet, timeout=TIMEOUT)
??? if reply:
??????? print reply.src,' is online'
??????? tmp = [1, reply.src]
??????? handle.write(reply.src + '\n')
??????? #handle.write(reply.src+" is online"+"\n")
?
def main():
??? threads=[]
??? ip = '192.168.1.1'
??? s = 2
??? e = 254
??? f=open('ip.log','w')
??? for i in range(s, e):
??????? t=threading.Thread(target=pro,args=(ip,i,f))
??????? threads.append(t)
??? print "main Thread begins at ",ctime()
??? for t in threads :
??????? t.start()
??? for t in threads :
??????? t.join()
??? print "main Thread ends at ",ctime()
批量添加ssh-key
home_dir = '/home/xx'
id_rsa_pub = '%s/.ssh/id_rsa.pub' %home_dir
if not? id_rsa_pub:
??? print 'id_rsa.pub Does not exist!'
??? sys.exit(0)
file_object = open('%s/.ssh/config' %home_dir ,'w')
file_object.write('StrictHostKeyChecking no\n')
file_object.write('UserKnownHostsFile /dev/null')
file_object.close()
def up_key(host,port,user,passwd):
??? try:
??????? s = paramiko.SSHClient()
??? s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
??? s.connect(host, port, user, passwd)
??????? t = paramiko.Transport((host, port))
??????? t.connect(username=user, password=passwd, timeout=3)
??????? sftp =paramiko.SFTPClient.from_transport(t)
??????? print 'create Host:%s .ssh dir......' %host
??????? stdin,stdout,stderr=s.exec_command('mkdir ~/.ssh/')
??????? print 'upload id_rsa.pub to Host:%s......' %host
??????? sftp.put(id_rsa_pub, "/tmp/temp_key")
??????? stdin,stdout,stderr=s.exec_command('cat /tmp/temp_key >> ~/.ssh/authorized_keys && rm -rf /tmp/temp_key')
??????? print 'host:%s@%s auth success!\n' %(user, host)
??????? s.close()
??????? t.close()
??? except Exception, e:
??????? #import traceback
??????? #traceback.print_exc()
??????? print 'connect error...'
??????? print 'delete ' + host? + ' from database...'
??????? delip(host)
??????? #delete from mysql****
??????? try:
??????????? s.close()
??????????? t.close()
??????? except:
??????????? pass
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
