SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 296 | Next

Shon Harris, Allen Harper, Chris Eagle, and Jonathan Ness

"Gray Hat Hacking, Second Edition"

Dictionaries are a great way to
store any values that you can associate with a key where the key is a more useful way to
fetch the value than a list??™s index.
Files with Python
File access is as easy as the rest of Python??™s language. Files can be opened (for reading or
for writing), written to, read from, and closed. Let??™s put together an example using several
different data types discussed here, including files. This example will assume we
start with a file named targets and transfer the file contents into individual vulnerability
target files. (We can hear you saying, ???Finally, an end to the Dilbert examples!???)
Gray Hat Hacking: The Ethical Hacker??™s Handbook
144
Chapter 6: Programming Survival Skills
145
PART III
% cat targets
RPC-DCOM 10.10.20.1,10.10.20.4
SQL-SA-blank-pw 10.10.20.27,10.10.20.28
# We want to move the contents of targets into two separate files
% python
# First, open the file for reading
>>> targets_file = open('targets','r')
# Read the contents into a list of strings
>>> lines = targets_file.readlines()
>>> lines
['RPC-DCOM\t10.10.20.1,10.10.20.4\n', 'SQL-SA-blank-pw\
t10.10.20.27,10.10.20.28\n']
# Let's organize this into a dictionary
>>> lines_dictionary = {}
>>> for line in lines: # Notice the trailing : to start a loop
.


Pages:
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308