Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
How to create a network scanner in python using socket?

socket library is used to work with network and here we use it to simulate a network scanner.

A network scanner iterates over all the possible IP addresses on the network and for every IP it tries to connect to one of the ports. If the connection was successful (ACK received) we know that this host is available at the given IP address. This is the philosophy of a network scanner.

For now we just try to connect to a specific port of the server and leave the other parts to you to iterate and check the status:

import socket

addr = '172.16.132.20'
port = 27017

socket_obj = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
result = socket_obj.connect_ex((addr,port))
print result
socket_obj.close()

The above code connects to the given address:port and returns a status code. If returned code is 0 then the
port is open otherwise the port is probably blocked/closed.

#python #socket #network #tcp #tcp_scanner
PostgreSQL: Up and Running: A Practical Guide to the Advanced Open Source Database #ebook #book #pdf
How to use regex in MongoDB?

I had a query that I had to find records that their numbers does not start with +98. For regex you need to use $regex in MongoDB find query:

db.my_collection.find({'destination_number': {'$regex': '^(?!\\+98)(\\+.*).*$'}})

The above regex says that the number should not start with +98 and then it says that it should start with + (to ignore non-standard numbers, strings, etc).

NOTE: ?! is used to negate the regex inside of parenthesis.

#mongodb #mongo #regex
What is CDN?

CDN is short for content delivery network. A content delivery network (CDN) is a system of distributed servers (network) that deliver pages and other Web content to a user, based on the geographic locations of the user, the origin of the webpage and the content delivery server.

This service is effective in speeding the delivery of content of websites with high traffic and websites that have global reach. The closer the CDN server is to the user geographically, the faster the content will be delivered to the user. CDNs also provide protection from large surges in traffic.


What are the benefits of using a CDN?

- Improving website load times
- Reducing bandwidth costs
- Increasing content availability and redundancy
- Improving website security


Telegram is an example of those services that use CDN in order to serve public channels media to end users with faster pace.

#cdn #content_delivery_network #telegram
504 Gateway timeout

It is a known issue to many people even those who are not in the programming field. 504 timeout happens when a response for a request has taken longer than expected. There are times that you know and are sure that you need to increase this time. For example if users export a huge excel file as a report. In nginx you can increase this time to what seems to be appropriate from programmer point of view.

In nginx.conf usually in /etc/nginx/ do as follow:

proxy_connect_timeout       600s;
proxy_send_timeout 600s;
proxy_read_timeout 600s;
send_timeout 600s;

More info: http://nginx.org/en/docs/http/ngx_http_proxy_module.html


#504 #gateway_timeout #timeout #nginx #proxy_read_timeout #proxy_send_timeout
If you run python using uwsgi you may get an error like below:

open("./python_plugin.so"): No such file or directory [core/utils.c line 3321]
!!! UNABLE to load uWSGI plugin: ./python_plugin.so: cannot open shared object file: No such file or directory !!!

This error says that the plugin is not loaded. This error happens when you install uwsgi using pip. Distros should package uWSGI in a modular way, with each feature as a plugin. But when you install using language specific ways (pip, gem...) the relevant language is embedded, so you do not need to load the plugin.

#python #uwsgi #pip
Docker can set different log drivers for its logging mechanism it can be json file, syslog, fluentd and so on. The default is set to json-file and these log files are located in /var/lib/docker/containers/. You can check type of your log in docker using:

$ docker inspect -f '{{.HostConfig.LogConfig.Type}}' <CONTAINER>
json-file

Instead of <CONTAINER> put your currently running container id.


To read more about this head on to: https://docs.docker.com/config/containers/logging/configure/#configure-the-logging-driver-for-a-container


#docker #log #log_driver
I usually use linux copy command cp to copy files from my project into a production environment project. I copied wrong files by accident from a different repo into production environment and messed up the repo. Some files got into modified state, many untracked
files added into the project.

To revert all the C**P and make a clean slate of your project again, you just need to do 2 things:

1- git checkout .

2- git clean -f


First command will revert all modified files into their previous state. Second one will remove all untracked files.

Happy copying :)

#git #revert #checkout #clean #git_clean
CoreOS (one of the most used docker base images) has been acquired by Red Hat with $250 million.

Read on:
- https://coreos.com/blog/coreos-agrees-to-join-red-hat/?utm_source=DevOps%27ish&utm_campaign=c766654b17- EMAIL_CAMPAIGN_2018_02_04&utm_medium=email&utm_term=0_eab566bc9f-c766654b17-46016105

#linux #coreos #redhat #docker
Configure Linux iptables Firewall for MongoDB

In order to harden your network infrastructure in linux, you need to monitor all incoming and outgoing traffic and only allow connections from servers that are trusted. For that reason we use iptables in linux. Each record in iptables is either an INPUT record or an OUTPUT record that controls all incoming traffic and all outgoing traffic.

With records below in iptables we explicitly allow traffic to the mongod instance from the application server. In the following examples, replace <ip-address> with the IP address of the application server:

iptables -A INPUT -s <ip-address> -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -d <ip-address> -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT

The first rule allows all incoming traffic from <ip-address> on port 27017, which allows the application server to connect to the mongod instance. The second rule, allows outgoing traffic from the mongod to reach the application server.

The default policy for iptables chains is to allow all traffic. After completing all iptables configuration changes, you must change the default policy to DROP so that all traffic that isn’t explicitly allowed as above will not be able to reach components of the MongoDB deployment. Issue the following commands to change this policy:

iptables -P INPUT DROP
iptables -P OUTPUT DROP

DANGER: do not issue the above commands if you are not fully aware of what you are doing!


#mongodb #linux #iptables #security