python 执行shell 命令,自动化添加攻击IP地址到iptables
2017-06-30 15:28
471 查看
通过python执行shell命令的方法有4种,在这里介绍一种常用的。
os.system、 os.popen、 commands、 subprocess
接下来介绍subprocess的使用
通过python 日志分析,获取到攻击源IP地址,收集写入到mysql数据库中
mysql如下:
iptables.py 脚本
python iptables.py
result:
os.system、 os.popen、 commands、 subprocess
接下来介绍subprocess的使用
通过python 日志分析,获取到攻击源IP地址,收集写入到mysql数据库中
mysql如下:
iptables.py 脚本
#!/usr/bin/env python # -*- coding:utf-8 -*- import os import MySQLdb import subprocess try: connection = MySQLdb.connect(user='soul',passwd='soul',host='127.0.0.1',db='iptable',port=3306,use_unicode=1,charset="utf8") cursor = connection.cursor() cursor.execute('select * from iptables') data = cursor.fetchall() cursor.close() connection.close() except MySQLdb.Error,err_msg: print 'MySQLdb error msg:' ,err_msg def insert(): for info in data: ipaddress = info[1] print '成功插入IP黑名单:%s' %ipaddress subprocess.call(['/sbin/iptables -I INPUT -s %s -p tcp --dport 8000 -j ACCEPT' % ipaddress], shell=True) def save(): subprocess.call(['service iptables save'], shell=True) if __name__ == '__main__': insert() save()
python iptables.py
result:
相关文章推荐
- Python执行Shell命令
- python中执行shell命令
- python中执行shell命令
- [Python/自动化运维] 利用Paramiko模块执行批量上传/下载/执行命令/查看服务器列表
- python 执行shell命令的类
- python执行shell命令
- 使用Python生成Shell命令,批量执行程序代码解析
- python执行shell命令
- python脚本执行shell命令或脚本
- python执行shell命令四法
- python执行shell命令的几种方法
- python中执行shell命令的几个方法小结
- python之执行shell命令
- python中执行shell命令的几个方法小结
- python执行shell命令
- Python 执行Shell 外部命令
- paip.执行shell cmd 命令uapi java php python总结
- python基础-tcp粘包、解决方案、subprocess执行shell命令
- python 执行shell命令并将结果保存的实例
- 在python中执行shell命令,并以字符串形式返回命令执行结果