加小编Python学习群:813542856即可自动获取大量python视频教程以及各类PDF!
公司服务器上的ip最少的也有100多个,有时候查到一个站的Ip, 不想通过OA去查,自己就用自己最近学的python知识,结合数据库,编写了一python小程序。实现只要输入主ip就能查到这台服务器的子ip,输入子ip能查到此ip所在的主服务器。
功能示例:
我用python给公司写一个ip批量查询工具,老板五千块现金甩我脸上使用 -m 参数 指定主服务器地址,即查询此服务器上所有的子ip
我用python给公司写一个ip批量查询工具,老板五千块现金甩我脸上使用 -s 参数 指定子ip, 即可查询此子ip所在的服务器主ip地址
我用python给公司写一个ip批量查询工具,老板五千块现金甩我脸上使用 -h 或 -help 参数可打印help
我用python给公司写一个ip批量查询工具,老板五千块现金甩我脸上使用 -v 或-version参数可打印版本
我用python给公司写一个ip批量查询工具,老板五千块现金甩我脸上如果程序参数不合法,则提示帮助
好了,功能就这么多,我们来看看怎么用python 实现的。
一,我们先看一下数据库,看一下他的结构,其实数据库里很简单,只记录了ip的对应关系。如下图
我用python给公司写一个ip批量查询工具,老板五千块现金甩我脸上二,我们来看一下程序是怎么写的, 先贴一下程序。
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#! /usr/bin/python
Filename select.py
import MySQLdb,os,sys
try:
conn = MySQLdb.connect("localhost","root","密码","ips",charset="utf8")
except MySQLdb.OperationalError, message:
print "link error"
def masterip(ip):
sql="select secip from ip_master where masterip='%s'" %ip
cursor=conn.cursor()
n=cursor.execute(sql)
cds=cursor.fetchall()
for cd in cds:
for col in cd:
print "%s" % (col)
cursor.close()
conn.close()
def secip(ip):
sql="select masterip from ip_master where secip='%s'" %ip
cursor=conn.cursor()
n=cursor.execute(sql)
cds=cursor.fetchall()
for cd in cds:
for col in cd:
print "%s" % (col)
cursor.close()
conn.close()
if len(sys.argv)<2:
print "You have an error in you syntax,please you -help,-h for help"
sys.exit()
if "-h"==sys.argv[1] or "-help"==sys.argv[1]:
print '''
This program select master ips and slave ips.
Options include:
-s slave ip :use slave ip to select msterip
-m masterip :use master ip to select slaveip
-h;-help :help
-v;-version :prints version '''
sys.exit()
elif "-v"==sys.argv[1] or "-version"==sys.argv[1]:
print "Version is 0.1"
sys.exit()
elif "-s"==sys.argv[1]:
if len(sys.argv)<3:
print "You have an error in you syntax,please you -help,-h for help"
sys.exit()
ip=sys.argv[2]
secip(ip)
elif "-m"==sys.argv[1]:
if len(sys.argv)<3:
print "You have an error in you syntax,please you -help,-h for help"
sys.exit()
ip=sys.argv[2]
masterip(ip)
else:
print "You have an error in you syntax,please you -help,-h for help"
</pre>
三.对程序进行解释
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#! /usr/bin/python
import MySQLdb,os,sys #加载 mysqldb os sys
try:
conn = MySQLdb.connect("localhost","root","密码","ips",charset="utf8")
except MySQLdb.OperationalError, message:
print "link error"
</pre>
尝试利用括号里的信息去连接数据库,如果连接数据库不成功刚打印link error!
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def masterip(ip):
sql="select secip from ip_master where masterip='%s'" %ip
cursor=conn.cursor()
n=cursor.execute(sql)
cds=cursor.fetchall()
for cd in cds:
for col in cd:
print "%s" % (col)
cursor.close()
conn.close()
</pre>
定义一个masterip函数, 括号里的ip 为参数,在下面的sql语句里用到。sql后面是查询语句。 利用上面括号里定义的ip 去查询 子ip。再利用for 循环逐个打印出ip!
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def secip(ip):
sql="select masterip from ip_master where secip='%s'" %ip
cursor=conn.cursor()
n=cursor.execute(sql)
cds=cursor.fetchall()
for cd in cds:
for col in cd:
print "%s" % (col)
cursor.close()
conn.close()
</pre>
定义一个secip函数, 括号里的ip 为参数,在下面的sql语句里用到。sql后面是查询语句。 利用上面括号里定义的ip 去查询 主ip。再利用for 循环逐个打印出ip!
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">if len(sys.argv)<2:
print "You have an error in you syntax,please you -help,-h for help"
sys.exit()
</pre>
判断命令行参数,如果命令行参数小于2,(命令本身就属于一个参数)就打印提示信息,并退出,此行的意思就是半数命令后面有没有跟参数,如果没有跟参数,就直接退出。
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">if "-h"==sys.argv[1] or "-help"==sys.argv[1]:
print '''
This program select master ips and slave ips.
Options include:
-s slave ip :use slave ip to select msterip
-m masterip :use master ip to select slaveip
-h;-help :help
-v;-version :prints version '''
sys.exit()
</pre>
判断命令行第一个数据是不是 -h (注,命令行参数是从0开始,0 也就是命令本身),如果是 -h的话,就打印帮助信息,并退出。
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">elif "-v"==sys.argv[1] or "-version"==sys.argv[1]:
print "Version is 0.1"
sys.exit()
</pre>
判断命令行第一个数据是不是 -v (注,命令行参数是从0开始,0 也就是命令本身),如果是 -v的话,就打印版本信息,并退出。
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">elif "-s"==sys.argv[1]:
if len(sys.argv)<3:
print "You have an error in you syntax,please you -help,-h for help"
sys.exit()
ip=sys.argv[2]
secip(ip)
</pre>
判断命令行第一个数据是不是 -s (注,命令行参数是从0开始,0 也就是命令本身),如果是 -s的话,判断命令行参数是否小于3,也就是 -s 后面有没有跟参数(ip), 如果没跟的话,就打印提示信息,并退出 。如果跟了的话,就把-s 后面的参数给 ip这个变量,并执行 secip() 这个函数。
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">elif "-m"==sys.argv[1]:
if len(sys.argv)<3:
print "You have an error in you syntax,please you -help,-h for help"
sys.exit()
ip=sys.argv[2]
masterip(ip)
</pre>
判断命令行第一个数据是不是 -m (注,命令行参数是从0开始,0 也就是命令本身),如果是 -m的话,判断命令行参数是否小于3,也就是 -m 后面有没有跟参数(ip), 如果没跟的话,就打印提示信息,并退出 。如果跟了的话,就把-m 后面的参数给 ip这个变量,并执行 masterip() 这个函数。
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">else:
print "You have an error in you syntax,please you -help,-h for help"
</pre>
如果跟上面所有的参数都不符合,就直接打印帮助信息。
Ok!程序完了。很简单,但好像也很实用。
我用python给公司写一个ip批量查询工具,老板五千块现金甩我脸上
网友评论