Python – remove newline from readlines

 

readlines() is a function in python used to read text file and convert it into list where each line in at file is a element in list with new line(\n) at the end of each element.

Example:

>>> filelist = open("file.txt").readlines()
['a\n', 'b\n', 'c\n', 'd\n']

If you want to remove new line(\n) at the end of all elements in list use read() and splitlines() functions as shown in below example.

read().splitlines() will do same funcationality as readlines() & only difference is read().splitlines() won’t have have new line(‘\n’) at the end of all elements in list.

Example:

>>>open("file.txt").read().splitlines()
['a', 'b', 'c', 'd']

Python – check if string starts with number

With the help of isdigit() function we can check if a string start with number or not.

Lets check with following examples:

>>> input = '1viewsby'
>>> input[0].isdigit()
True

Above example is returned true since input string is started with with number.

>>> input = 'viewsby'
>>> input[0].isdigit()
False

Since input is is not started with number above example returned false.

iostat – installation and usage

 

The iostat command is used for monitoring system input/output device loading by observing the time the devices are active in relation to their average transfer rates.

The iostat command generates reports that can be used to change system configuration to better balance the input/output load between physicaldisks.

How to install iostat:

iostat is available in sysstat module, so to install iostat we need to install sysstat module.

Use following command to sysstat:

$ apt-get install sysstat

Examples:

To print all disks IO stat:

$ iostat

To print a specific disk IO stat:

$ iostat /dev/sdb

To contiuesly print a specific disk IO stats every 10 seconds:

$ iostat -d 10 /dev/sdb

To display device utilization and extended statistics in Kilobytes:

$ iostat -dkx

Ubuntu install gnome classic from command line

Recent versions of Ubuntu is coming with Unity desktop which are bit heavy.

We can also install gnome classic from command line.

To install gnome classic or gnome session fallback use following command:

$ sudo apt-get install  gnome-session-fallback

After installing just logout from this session and relogin.

Before logging in click the icon(Ubuntu Icon) in the top right of the Login box and select Gnome classic and login to you account.

This will take to gnome classic desktop.

Lighttpd – block user agent

 

In lighttpd we can block or deny access to a specific user agent.

Syntax to block user agent:

$HTTP["useragent"] =~ "User agent name" {url.access-deny = ( "" )}

where “User agent name” is any user agent.

Example:

$HTTP["useragent"] =~ "Xenu Link Sleuth" {url.access-deny = ( "" )}

Above example will block all requests with user agent name “Xenu Link Sleuth”.

Nmap – Scan for open ports

 

nmap is a network exploration tool and security/port scanner.

We can list all open ports using nmap for a given IP/Host.

Use following command to list all open ports:

$ sudo nmap -v -sS 192.168.1.1

Use following command to scan specific ports:

$ nmap -sS -O -p80,8080 192.168.1.1

To can specific range of ports(scan from port 80 to 100):

$ sudo nmap -sS -O -p80-100 192.168.1.1

Ping all IP’s in a subnet

 

We can use nmap command to ping all IP’s in subnet.

If you don’t have nmap installed in your machine install nmap with following command:

$ apt-get install nmap

Use following command to ping all IP’s in a subnet:

$ nmap -n -sP 192.168.2.0/26

Output:

Starting Nmap 5.21 ( http://nmap.org ) at 2014-06-30 17:04 PDT
Nmap scan report for 192.168.2.1
Host is up (0.00073s latency).
Nmap scan report for 192.168.2.2
Host is up (0.0011s latency).
Nmap scan report for 192.168.2.4
Host is up (0.0010s latency).
Nmap done: 64 IP addresses (3 hosts up) scanned in 0.62 seconds

In above output we can see all reachable IP’s.

 

mutt – send mails with attachments

 

By using mutt we can send mails with attachments from command line.

Install mutt:

$ apt-get install mutt

Send mail with attachment:

$ mutt -s "PFA" -a attachment.txt user@example.com < body.txt

or

$ echo "body text here" | mutt -s "PFA" -a t.txt user@example.com

Send mail without attachment:

$ mutt -s "PFA" user@example.com < body.txt

or

$ echo "body text here"  | mutt -s "PFA" user@example.com

Python convert list values to int values

 

I have a list with int values as strings as following:

>>> mylist = ['1', '2', '3', '4']

>> mylist

['1', '2', '3', '4']

I would like to convert all values in mylist to int’s.

I used following code to convert values of lists to int’s:

>>> output = map(int, mylist)

>>> output

[1, 2, 3, 4]

Now just observer both list output values, all values in mylist are strings and all values in output list are int’s.