Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
derv82
GitHub Repository: derv82/wifite2
Path: blob/master/wifite/util/color.py
412 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
import sys
5
6
class Color(object):
7
''' Helper object for easily printing colored text to the terminal. '''
8
9
# Basic console colors
10
colors = {
11
'W' : '\033[0m', # white (normal)
12
'R' : '\033[31m', # red
13
'G' : '\033[32m', # green
14
'O' : '\033[33m', # orange
15
'B' : '\033[34m', # blue
16
'P' : '\033[35m', # purple
17
'C' : '\033[36m', # cyan
18
'GR': '\033[37m', # gray
19
'D' : '\033[2m' # dims current color. {W} resets.
20
}
21
22
# Helper string replacements
23
replacements = {
24
'{+}': ' {W}{D}[{W}{G}+{W}{D}]{W}',
25
'{!}': ' {O}[{R}!{O}]{W}',
26
'{?}': ' {W}[{C}?{W}]'
27
}
28
29
last_sameline_length = 0
30
31
@staticmethod
32
def p(text):
33
'''
34
Prints text using colored format on same line.
35
Example:
36
Color.p('{R}This text is red. {W} This text is white')
37
'''
38
sys.stdout.write(Color.s(text))
39
sys.stdout.flush()
40
if '\r' in text:
41
text = text[text.rfind('\r')+1:]
42
Color.last_sameline_length = len(text)
43
else:
44
Color.last_sameline_length += len(text)
45
46
@staticmethod
47
def pl(text):
48
'''Prints text using colored format with trailing new line.'''
49
Color.p('%s\n' % text)
50
Color.last_sameline_length = 0
51
52
@staticmethod
53
def pe(text):
54
'''Prints text using colored format with leading and trailing new line to STDERR.'''
55
sys.stderr.write(Color.s('%s\n' % text))
56
Color.last_sameline_length = 0
57
58
@staticmethod
59
def s(text):
60
''' Returns colored string '''
61
output = text
62
for (key,value) in Color.replacements.items():
63
output = output.replace(key, value)
64
for (key,value) in Color.colors.items():
65
output = output.replace('{%s}' % key, value)
66
return output
67
68
@staticmethod
69
def clear_line():
70
spaces = ' ' * Color.last_sameline_length
71
sys.stdout.write('\r%s\r' % spaces)
72
sys.stdout.flush()
73
Color.last_sameline_length = 0
74
75
@staticmethod
76
def clear_entire_line():
77
import os
78
(rows, columns) = os.popen('stty size', 'r').read().split()
79
Color.p('\r' + (' ' * int(columns)) + '\r')
80
81
82
@staticmethod
83
def pattack(attack_type, target, attack_name, progress):
84
'''
85
Prints a one-liner for an attack.
86
Includes attack type (WEP/WPA), target ESSID & power, attack type, and progress.
87
ESSID (Pwr) Attack_Type: Progress
88
e.g.: Router2G (23db) WEP replay attack: 102 IVs
89
'''
90
essid = '{C}%s{W}' % target.essid if target.essid_known else '{O}unknown{W}'
91
Color.p('\r{+} {G}%s{W} ({C}%sdb{W}) {G}%s {C}%s{W}: %s ' % (
92
essid, target.power, attack_type, attack_name, progress))
93
94
95
@staticmethod
96
def pexception(exception):
97
'''Prints an exception. Includes stack trace if necessary.'''
98
Color.pl('\n{!} {R}Error: {O}%s' % str(exception))
99
100
# Don't dump trace for the "no targets found" case.
101
if 'No targets found' in str(exception):
102
return
103
104
from ..config import Configuration
105
if Configuration.verbose > 0 or Configuration.print_stack_traces:
106
Color.pl('\n{!} {O}Full stack trace below')
107
from traceback import format_exc
108
Color.p('\n{!} ')
109
err = format_exc().strip()
110
err = err.replace('\n', '\n{!} {C} ')
111
err = err.replace(' File', '{W}File')
112
err = err.replace(' Exception: ', '{R}Exception: {O}')
113
Color.pl(err)
114
115
116
if __name__ == '__main__':
117
Color.pl('{R}Testing{G}One{C}Two{P}Three{W}Done')
118
print(Color.s('{C}Testing{P}String{W}'))
119
Color.pl('{+} Good line')
120
Color.pl('{!} Danger')
121
122
123