场景是这样的:一个生产机房,会有很多的测试机器和生产机器(也就是30台左右吧),由于管理较为混乱导致了哪台机器有人用、哪台机器没人用都不清楚,从而产生了一个想法--利用一台机器来管理所有的机器,记录设备责任人、设备使用状态等等信息....那么,为什么选择python,python足够简单并且拥有丰富的第三方库的支持。
最初的想法
由于刚参加工作不久,对这些东西也都没有接触过,轮岗到某个部门需要做出点东西来(项目是什么还没情况,就要做出东西来,没办法硬着头皮想点子吧)。。。
本想做一个简单点的自动化测试的工具,但这项目的测试方法和测试用例暂时不能使用这种通用的测试手段(输入和输出都确定不了),从而作罢...
那么做点什么东西,经常发现同事们问208谁用的?201谁用的?那IP是我的!!!你是不是把我得网线给拔掉了?242那机器到底是哪台?
突然间,春天来了,是不是可以做一个系统用来检测IP和记录设备的使用人,甚至可以按需要在某台设备上运行一个脚本或命令?把这个矮矬穷的想法和leader沟通过后,确认可以做,那么就开始吧!!!
设计思想
该系统的大概思想:
1. 要获得所有服务器的各种信息,需要在任意一台服务器上部署一个agent作为信息获取的节点,定时向管理服务器节点发送服务器信息数据。
2. server作为综合管理节点,接收并储存agent提交的信息。
3. 为了方便使用,采用web页面的形式做展示。
开发工具选择
1. 开发语言:python
之所以选择python,简单,第三方库丰富,不用造轮子
2. 数据库:mysql
简单、易用
3. webpy:web框架
入门简单、部署方便
4. bootstrap:前端框架
不要关心太多前端问题
5. paramiko:python库,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接
通过SSH方式连接agent服务器:远程运行命令、传输文件
6. scapy: python库,可用来发送、嗅探、解析和伪造网络数据包,这里用来扫描IP
7. MySQLdb: 连接mysql
8. shell 和 python脚本接口: 为其他人提供shell脚本的接口
经验分享
1. 前端对我来说是新东西,从来没弄过,页面的动画效果,脚本运行时的过渡都是需要考虑的,开始考虑利用倒计时,但是这个时间是不可控的,后来采用ajax来处理这个问题
2. agent要自动部署到每台机器,并可以通过server来控制刷新时间
3. 建立一个可扩展的表是非常重要的,而且一些重要的信息需要写入磁盘,在数据库失效的情况下,可以从磁盘获取数据
4. 数据库的连接,如果长时间没有操作的话会超时,要考虑到
... ...
项目结构--webpy
1. website.py为webpy的主程序,设置了url映射
2. model.py为webpy的url映射类,处理请求和返回
3. static中存放静态资源
4. scripts用来存放处理的脚本,这里起的名字有些问题
连接数据库
使用MyQSLdb连接mysql,在这里我没有使用webpy提供的数据库接口,而是自己封装了一套
ssh远程连接服务器
paramiko实现ssh连接、与数据传输、执行命令和脚本
代码如下:
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进行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

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.