Archive for the ‘ Linux ’ Category

Python modify user-agent

How to generate user-agent header for web requests in Python


In previous post, we saw how to modify user-agent header in wget, curl and httpie programs. In this post, I will show you how to modify user-agent header in Python’s popular requests module. There are several reasons for modify user agent, one of which is to trigger a different response from a website. Many website offer different content based on user-agent header. You can find user-agent header details here.

In Python, one of the most popular libraries to query web servers is the requests module. The requests module allows you to pass header information using the headers option –

1. Simplest use case, without a header

import requests
requests.get('http://linuxfreelancer.com/status')

And this is how it is logged on the web server side, Apache in this case –

76.1.2.3 [20/May/2018:01:08:37 -0400] "GET /status HTTP/1.1" 200 359 "-" "python-requests/2.18.4" 1798

The user-agent is simply showing as “python-requests/2.18.4”, and some website might even block this to prevent web crawlers. So the next step is to modify this.

 

2. Modify user-agent header

headers = {'User-Agent': 'Mozilla/5.0 (Android 5.1; Tablet; rv:50.0) Gecko/50.0 Firefox/50.0'}
requests.get('http://linuxfreelancer.com/status', headers=headers)

And this is what the access log entry looks like on the web server side –

76.1.2.3  [20/May/2018:01:11:29 -0400] "GET /status HTTP/1.1" 200 359 "-" "Mozilla/5.0 (Android 5.1; Tablet; rv:50.0) Gecko/50.0 Firefox/50.0" 1289

As you can see above, the user-agent entry has several identifiers which is not easy to remember. The best way would be to programatically generate valid user-agents for different platforms.

 

3. Generate valid user-agents

The user_agent module is used for generating random and yet valid web user agents. You can install it with ‘pip install user_agent’.

This module generate user-agent strings for differnt devices types such as desktop, smartphone and table, as well as OS types (Windows, Linux, Mac, Android …). Let us try it in a virtual environment –

virtualenv /tmp/venv
source /tmp/venv/bin/activate
pip install user_agent

Now run Python in an interactive mode –

import requests
from user_agent import generate_user_agent 

In [8]: generate_user_agent()
Out[8]: 'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2803.5 Safari/537.36'

In [9]: generate_user_agent()
Out[9]: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:49.0) Gecko/20100101 Firefox/49.0'

In [11]: generate_user_agent(os='linux')
Out[11]: 'Mozilla/5.0 (X11; Ubuntu; Linux i686 on x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2942.3 Safari/537.36'

In [13]: generate_user_agent(device_type='tablet')
Out[13]: 'Mozilla/5.0 (Linux; Android 4.4; HTC Desire 616 dual sim Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2938.21 Safari/537.36'

In [14]: generate_user_agent(device_type='desktop', os='win')
Out[14]: 'Mozilla/5.0 (Windows NT 6.1; rv:45.0) Gecko/20100101 Firefox/45.0'

In [20]: generate_user_agent(navigator='chrome', os='linux', device_type='desktop')
Out[20]: 'Mozilla/5.0 (X11; Ubuntu; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2894.26 Safari/537.36'

This allows us to generate from random to specific valid user-agent header information. We can then pass this randomly generated user-agent text to the requests module header option, and we will view our web server logs to validate –

from user_agent import generate_user_agent
import requests
requests.get('http://linuxfreelancer.com/status', headers={'User-Agent': generate_user_agent(navigator='firefox', os='linux')})

Log entry –

76.1.2.3 – – [20/May/2018:01:28:24 -0400] “GET /status HTTP/1.1” 200 359 “-” “Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0” 1486

 

References –


http://docs.python-requests.org/en/master/

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent

Configure IP Aliases in Red Hat / CentOS


IP aliasing is a term for assigning multiple IP addresses to a single network interface. It is quite useful in a shared web hosting for instance, particularly if the domains have SSL certificates. You can setup each domain to resolve to different IP address, even if they are all sharing the same network interface.

You have to be root to perform this tasks.

1. Disable Network Manager


# service NetworkManager stop

# chkconfig NetworkManager off

2. Add IP alias from cli


# ip addr addr add 192.168.0.11/24 dev eth0 label eth0:1

# ip addr show eth0

3. Persistently add lias

Create the file /etc/sysconfig/network-scripts/ifcfg-eth0:1

# cat /etc/sysconfig/network-scripts/ifcfg-eth0:1
DEVICE=eth0:1
IPADDR=192.168.0.11
PREFIX=24
ONPARENT=yes

4. Restart network service

# service network restart
# ip addr show eth0

 

How to install  Ubuntu 18.04 LTS (Bionic Beaver)

Installation steps for Ubuntu 18.04, code named Bionic Beaver. This steps are for the desktop version.

One notable change is Unity is no longer the default desktop environment for Ubuntu anymore. It will feature the latest GNOME (i.e., version 3.28).  The Linux kernel version on Ubuntu 18.04 is 4.15.

1. Download iso  from the sitehttp://releases.ubuntu.com/18.04/
Desktop image (AMD64) – http://releases.ubuntu.com/18.04/ubuntu-18.04-desktop-amd64.iso
Server image (AMD64) – http://releases.ubuntu.com/18.04/ubuntu-18.04-live-server-amd64.iso

2. Select disc image file (iso) during installation

In my case, I am using VMware player, I select disc image file (iso) during the creation of the VM.

createvm

3. Personalize Linux

In this step, it will ask you for your full name, username, password and to confirm your password.

4. For VMware player, make sure to  customize hardware, select the memory size to allocate (2GB in my case, minimum requirement is 1G), disk size, number of vCPUs etc.

Right after finishing the hardware customization and click start, it will immediately start creating the file system and installing software.

Once completed, the system will reboot.

5. Login screen – enter the username and password you submitted when installing the iso.

6. What is new screen

7. Start testing some of the applications

a. The command terminal

 

b. Firefox browser

 

By default Ubuntu 18.04 comes with Gnome 3, you can install KDE plasma and use that as your windows manager. Here are the steps –

1. Run the following commands from the terminal to install kde plasma

sudo apt-get update
apt-get install tasksel -y
sudo tasksel install kubuntu-desktop
reboot

2. After rebooting, select Plasma

 

Links – 


Release notes – Bionic Beaver release notes

What is new – https://itsfoss.com/ubuntu-18-04-release-features/

Some of the most popular Linux youtube videos by view counts –

1. Introduction to Linux
Linux history, what an OS is, how to interact with Linux, Linux distros etc.

2. 3D Desktop! TouchScreen and XGL on Linux!

3. How Linux is Built
The code, how many lines of code the Linux kernel has, how release are made …

4. Linux Baby Rocker
Baby rocket shell script, which ejects and pulls back a CDROM drive acting as a baby rocker.

5. Introduction to Linux and Basic Linux Commands for Beginners
Tutorial on basic Linux commands, the Linux in the demo is installed in Windows running a virtual box.

You can search full list in youtube.

How to troubleshoot dns issues by directly querying name servers


This tip will help you troubleshooting DNS issues by directly querying DNS using only the IP address of name servers. When you run dns resolution client tools such as dig or nslookup, they will query the name server configured on your host. If the DNS with unexpired ttl is in cache, they will return it from cache. The results will return from cache by any of the intermediate name servers except for the authoritative name servers. That is why ‘dig +trace’ is useful in troubleshooting dns issues, as it starts from the root name servers and moves down all the way to the authoritative name servers to get you the dns records.

Here is a similar tool to “dig +trace“, which queries root name servers, their IPs is hard coded in the script, and follows the authoritative name servers for the subdomains by directly querying the registered IP addresses of name servers. For instance, if you use this tool to query “www.example.com”, it will get a randomly picked root name server’s IP and query it for NS records of “.com” domain. Once it gets the IP address of the name servers for “.com”, it goes on to query them for authoritative name servers of “example.com.” and does the same for “www.example.com.” as well. Throughout the query, it doesn’t use any cache or FQDN, it get the IP address of authoritative name servers and queries the IP directly.

You will need to install dnspython module first –

cd /tmp
pip install dnspython
https://github.com/danasmera/Python_scripts.git
cd Python_scripts/

Start DNS tracing now –

1. google.com

$ python dig-trace.py google.com
Splitting domain into sub-domains ...
['.', 'com.', 'google.com.']

Selected root . name server: 199.9.14.201
Selecting name server for com. domain ...

picked name server: 192.48.79.30
Selecting name server for google.com. domain ...

picked name server: 216.239.36.10
Querying name server: 216.239.36.10
google.com. 300 IN A 173.194.219.102
google.com. 300 IN A 173.194.219.101
google.com. 300 IN A 173.194.219.113
google.com. 300 IN A 173.194.219.100
google.com. 300 IN A 173.194.219.138
google.com. 300 IN A 173.194.219.139


2. www.whitegov.com txt

 python dig-trace.py www.whitegov.com txt
Splitting domain into sub-domains ...
['.', 'com.', 'whitegov.com.', 'www.whitegov.com.']

Selected root . name server: 192.33.4.12
Selecting name server for com. domain ...

picked name server: 192.54.112.30
Selecting name server for whitegov.com. domain ...

picked name server: 204.11.57.26
Selecting name server for www.whitegov.com. domain ...

picked name server: 204.11.56.26
Querying name server: 204.11.56.26
www.whitegov.com. 3600 IN TXT "~"



3. cnn.com mx

$ python dig-trace.py cnn.com mx
Splitting domain into sub-domains ...
['.', 'com.', 'cnn.com.']

Selected root . name server: 193.0.14.129
Selecting name server for com. domain ...

picked name server: 192.31.80.30
Selecting name server for cnn.com. domain ...

picked name server: 205.251.192.47
Querying name server: 205.251.192.47
cnn.com. 300 IN MX 10 mxa-000c6b02.gslb.pphosted.com.
cnn.com. 300 IN MX 10 mxb-000c6b02.gslb.pphosted.com.

4. linuxfreelancer.com [ANY | NS ]

$ python dig-trace.py www.linuxfreelancer.com ANY
Splitting domain into sub-domains ...
['.', 'com.', 'linuxfreelancer.com.', 'www.linuxfreelancer.com.']

Selected root . name server: 192.112.36.4
Selecting name server for com. domain ...

picked name server: 192.35.51.30
Selecting name server for linuxfreelancer.com. domain ...

picked name server: 208.109.255.48
Selecting name server for www.linuxfreelancer.com. domain ...
Querying name server: 208.109.255.48


$ python dig-trace.py www.linuxfreelancer.com NS
Splitting domain into sub-domains ...
['.', 'com.', 'linuxfreelancer.com.', 'www.linuxfreelancer.com.']

Selected root . name server: 202.12.27.33
Selecting name server for com. domain ...

picked name server: 192.55.83.30
Selecting name server for linuxfreelancer.com. domain ...

picked name server: 216.69.185.48
Selecting name server for www.linuxfreelancer.com. domain ...
Querying name server: 216.69.185.48
www.linuxfreelancer.com. 1800 IN CNAME linuxfreelancer.com.
linuxfreelancer.com. 3600 IN NS ns75.domaincontrol.com.
linuxfreelancer.com. 3600 IN NS ns76.domaincontrol.com.


Links –


https://github.com/danasmera/Python_scripts

https://github.com/rthalley/dnspython

https://linux.die.net/man/1/dig

How to update or change author name or email in git or github

Under rare circumstances, your commits in github might appear in github as if they were made by some one else. I was once the sole contributor in a personal repo and when I pushed a commit to github, I noticed that the in github the commit appeared to come from some other github member and that member was added as a contributor in my repo. The reason for this issue was in my local git config, I had my email address as “user@localhost.localdomain”. Apparently github links your commit to whoever setup that email address to their GitHub account.

Here is the steps and a script that you can run to fix this –

1. Clone your github repo to your local file system.
2. change to the cloned directory
3. Add below shell script to your repo directory and run it –
Make sure to update the first three variables OLD_EMAIL, CORRECT_NAME, and CORRECT_EMAIL to appropriate values.

#!/bin/sh

git filter-branch --env-filter '
OLD_EMAIL="ADD_EMAIL_TO_BE_REMOVED@HERE.COM"
CORRECT_NAME="NEW_CORRECT_NAME"
CORRECT_EMAIL="NEW_CORRECT_EMAIL@HERE.COM"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

4. Run ‘git log’ to review the author information and if everything looks good –

git push --force --tags origin 'refs/heads/*'

5. delete the cloned directory from your local file system.

References –

https://help.github.com/articles/why-are-my-commits-linked-to-the-wrong-user/

https://help.github.com/articles/changing-author-info/