1 2 3 4 5 |
sudo apt-get install build-essential python-dev python-setuptools build-essential -y sudo easy_install pip sudo pip install virtualenv # 创建虚拟环境 virtualenv -p python3 ansible |
MySQL, MongoDB, Redis 安装
1 2 3 4 5 6 7 |
sudo apt-get install -y mysql-server redis-server mongodb #进入命令行 mysql -uroot -p mongo redis-cli #如需非本地连接,可将/etc/mongodb.conf bind_ip 和/etc/redis/redis.conf 中的 bind 值修改为0.0.0.0 |
/etc/mysql/my.cnf
1 2 3 4 5 6 |
[client] default-character-set=utf8 [mysqld] character-set-server=utf8 collation-server=utf8_general_ci |
日志配置
settings.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# logging LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'verbose': { 'format': '[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d] %(message)s', 'datefmt': '%Y%m%d %H:%M:%S', }, }, 'handlers': { 'file': { 'level': 'INFO', 'class': 'logging.FileHandler', 'formatter': 'verbose', 'filename': '%s/error.logs'%BASE_DIR, }, }, 'loggers': { 'django': { 'handlers': ['file'], 'level': 'INFO', 'propagate': True, }, }, } |
调用
1 2 |
import logging logger = logging.getLogger("django") |
存活探测
ICMP(ping, traceroute),TCP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# CentOS,Ubuntu yum/apt-get install nmap # 使用 nmap 更推荐使用 Ubuntu # 发送一个包 ping -c 1 xx.xx.xx.xx sudo nmap -n -sP xx.xx.xx.xx # 以上两者区别:前者会接收到两个包,后者只接收一个包,效率更高,ping 接收示例: 09:23:48.648271 IP 192.168.0.11 > 192.168.0.5: ICMP echo request, id 20166, seq 1, length 64 09:23:53.661619 ARP, Reply 192.168.0.11 is-at 08:00:27:83:d0:0d, length 46 # 探测端口 sudo nmap -n -PE xx.xx.xx.xx # 抓包 yum/apt-get install tcpdump # 以enp0s3网卡为例(常见的还有 eth0) tcpdump -np -i enp0s3 src host xx.xx.xx.xx # 对整个网段探测主机存活状况,以192.168.0.x 为例 sudo nmap -n -sP -PE 192.168.0.0/24 |
Python 代码实现
1 2 3 4 5 6 7 8 9 10 11 12 |
# 安装 Python 的 Nmap pip install python-nmap # 使用 import nmap nm = nmap.PortScanner() nm.scan(hosts="192.168.0.0/24", arguments="-n -sP -PE") # 所有主机的 IP nm.all_hosts() # 检测端口 import telnetlib tm = telnetlib.Telnet(host='192.168.0.11', port='22', timeout=4) tm.read_until(b"\n", timeout=5) |
小技巧:
使用多个 settings.py,如本地和线上使用不同 配置文件可在运行时执行 python manage.py runserver –settings=mydevops.settings的形式(修 settings为对应的文件)
pexpect, paramiko
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
pip install pexpect import pexpect # 指令,状态(0表示成功) pexpect.run("ls /tmp", withexitstatus=1) # 缓存区内容匹配:正则表达式,pexpect.EOF,pexpect.TIMEOUT ssh_k = pexpect.spawn('ssh alan@192.168.0.14 -p22') ssh_k.expect('password:') # 向子程序发送指令:send(), sendline(), sendcontrol(char) pip install paramiko import paramiko jssh = paramiko.SSHClient() # paramiko.Transport() jssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 密码验证方式 jssh.connect('192.168.1.110','22','test','123456') # 密钥验证方式 key = paramiko.RSAKey.from_private_key_file('/tmp/id_rsa') jssh.connect('192.168.1.110','22', 'test', pkey=key) # 执行命令stdout.read()读取内容 stdin,stdout,stderr = jssh.exec_command('ls /tmp') |
常见问题
ubuntu warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
1 2 3 |
sudo rm -f /var/lib/mongodb/mongod.lock # 默认数据库目录为/data/db sudo mongod --smallfiles |
Complete output from command python setup.py egg_info:
sh: 1: mysql_config: not found
1 |
sudo apt-get install -y libmysqlclient-dev |
No module named django.core.management
1 |
sudo pip install "django<2" --upgrade -i https://pypi.douban.com/simple |
mysql.service: Control process exited, code=exited status=1
Failed to start MySQL Community Server.
mysql.service: Unit entered failed state.
mysql.service: Failed with result ‘exit-code’.
mysql.service: Service hold-off time over, scheduling restart
Stopped MySQL Community Server.
mysql.service: Start request repeated too quickly.
Failed to start MySQL Community Server.
1 2 3 |
sudo chown -R mysql:mysql /var/lib/mysql sudo mysqld --initialize /etc/init.d/mysql start |