|
| 1 | +''' |
| 2 | +this file won't execute, you will have to type this all into the python prompt, which is the appropriate way of |
| 3 | +doing programming, as it allows the programmer to have an interactive output |
| 4 | +
|
| 5 | +http://standards.ieee.org/develop/regauth/ethertype/eth.txt |
| 6 | +this is the etherType text file we are going to use |
| 7 | +there are 43 lines, each one has a hex number, I have to convert |
| 8 | +all those hex numbers into decimal, and then print them back to the text file |
| 9 | +
|
| 10 | +NO way I am going to do that by hand. simple, use python! |
| 11 | +
|
| 12 | +we have to convert the EtherType file to this |
| 13 | +0x0800 2048 Internet Protocol version 4 (IPv4) |
| 14 | +0x0806 2054 Address Resolution Protocol (ARP) |
| 15 | +0x0842 2114 Wake-on-LAN[3] |
| 16 | +
|
| 17 | +''' |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +>>> f = open('/root/Desktop/project/EtherType','r') |
| 22 | +>>> lines = f.readlines() |
| 23 | +>>> lines |
| 24 | +['0x0800 \t2048 Internet Protocol version 4 (IPv4)\n', '0x0806 \t2054 Address Resolution Protocol (ARP)\n', ...... |
| 25 | + |
| 26 | +'''now we get the content of the file in python's tuple, the next thing we want to do is extract only the hex numbers''' |
| 27 | + |
| 28 | +>>> for i in lines: |
| 29 | +... print i[:7] |
| 30 | +... |
| 31 | +0x0800 |
| 32 | +0x0806 |
| 33 | +0x0842 |
| 34 | +0x22F3 |
| 35 | +0x6003 |
| 36 | +0x8035 |
| 37 | +0x809B |
| 38 | +0x80F3 |
| 39 | +0x8100 |
| 40 | +0x8137 |
| 41 | +0x8138 |
| 42 | +0x8204 |
| 43 | +0x86DD |
| 44 | +0x8808 |
| 45 | +0x8809 |
| 46 | +0x8819 |
| 47 | +0x8847 |
| 48 | +0x8848 |
| 49 | +0x8863 |
| 50 | +0x8864 |
| 51 | +0x8870 |
| 52 | +0x887B |
| 53 | +0x888E |
| 54 | +0x8892 |
| 55 | +0x889A |
| 56 | +0x88A2 |
| 57 | +0x88A4 |
| 58 | +0x88A8 |
| 59 | +0x88AB |
| 60 | +0x88CC |
| 61 | +0x88CD |
| 62 | +0x88E1 |
| 63 | +0x88E3 |
| 64 | +0x88E5 |
| 65 | +0x88F7 |
| 66 | +0x8902 |
| 67 | +0x8906 |
| 68 | +0x8914 |
| 69 | +0x8915 |
| 70 | +0x892F |
| 71 | +0x9000 |
| 72 | +0x9100 |
| 73 | +0xCAFE |
| 74 | +#the next thing we need to do is convert them into integer |
| 75 | + |
| 76 | +for i in lines: |
| 77 | +... print int(str(i[:7]),16) |
| 78 | +... |
| 79 | +2048 |
| 80 | +2054 |
| 81 | +2114 |
| 82 | +8947 |
| 83 | +24579 |
| 84 | +32821 |
| 85 | +32923 |
| 86 | +33011 |
| 87 | +33024 |
| 88 | +33079 |
| 89 | +33080 |
| 90 | +33284 |
| 91 | +34525 |
| 92 | +34824 |
| 93 | +34825 |
| 94 | +34841 |
| 95 | +34887 |
| 96 | +34888 |
| 97 | +34915 |
| 98 | +34916 |
| 99 | +34928 |
| 100 | +34939 |
| 101 | +34958 |
| 102 | +34962 |
| 103 | +34970 |
| 104 | +34978 |
| 105 | +34980 |
| 106 | +34984 |
| 107 | +34987 |
| 108 | +35020 |
| 109 | +35021 |
| 110 | +35041 |
| 111 | +35043 |
| 112 | +35045 |
| 113 | +35063 |
| 114 | +35074 |
| 115 | +35078 |
| 116 | +35092 |
| 117 | +35093 |
| 118 | +35119 |
| 119 | +36864 |
| 120 | +37120 |
| 121 | +51966 |
| 122 | + |
| 123 | +the next thing we need to do is to write this to our file, remmember in the actual code we have 'printed' the results, |
| 124 | +in reality we have to save them to python variables to work with them, but it is a good practice to see them first |
| 125 | +and then do the actual programming |
| 126 | + |
| 127 | +you will now notice that we already have the file read in python, so the pointer is now in the last position. We have to |
| 128 | +in a way create a new file with one extra column after the hex number |
| 129 | + |
| 130 | +>>> a=[] #create a new tuple |
| 131 | + |
| 132 | +>>> for i in lines: |
| 133 | +... a.append( int(str(i[:7]),16)) |
| 134 | +... |
| 135 | +>>> a |
| 136 | +[2048, 2054, 2114, 8947, 24579, 32821, 32923, 33011, 33024, 33079, 33080, 33284, 34525, 34824, 34825, 34841, |
| 137 | +34887, 34888, 34915, 34916, 34928, 34939, 34958, 34962, 34970, 34978, 34980, 34984, 34987, 35020, 35021, 35041, |
| 138 | +35043, 35045, 35063, 35074, 35078, 35092, 35093, 35119, 36864, 37120, 51966] |
| 139 | + |
| 140 | + |
| 141 | +>>> lines[0] |
| 142 | +'0x0800 \tInternet Protocol version 4 (IPv4)\n' |
| 143 | + |
| 144 | +>>> lines[:7]+str(2048)+lines[7:] |
| 145 | +Traceback (most recent call last): |
| 146 | + File "<console>", line 1, in <module> |
| 147 | +TypeError: can only concatenate list (not "str") to list |
| 148 | + |
| 149 | +>>> lines[0] |
| 150 | +'0x0800 \tInternet Protocol version 4 (IPv4)\n' |
| 151 | + |
| 152 | +>>> type(lines[0]) |
| 153 | +<type 'str'> |
| 154 | + |
| 155 | +>>> lines[:7] |
| 156 | +['0x0800 \tInternet Protocol version 4 (IPv4)\n', ..... |
| 157 | + |
| 158 | +>>> lines[0][:7]+str(2048)+lines[0][7:] |
| 159 | +'0x0800 2048\tInternet Protocol version 4 (IPv4)\n' |
| 160 | + |
| 161 | +# now we got the answer for the first line, we need to do this for a;; the other lines as well |
| 162 | + |
| 163 | +>>> file = open('/root/Desktop/project/EtherType2','a') |
| 164 | + |
| 165 | +>>> dir(file) |
| 166 | +['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', |
| 167 | +'__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', |
| 168 | +'__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'closed', 'encoding', |
| 169 | +'errors', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', |
| 170 | +'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines'] |
| 171 | + |
| 172 | +>>> for i in range(43): |
| 173 | +... final_print = lines[i][:7]+str(a[i])+lines[i][7:] |
| 174 | +... file.write(final_print) |
| 175 | +... |
| 176 | + |
| 177 | +>>> file.close() |
| 178 | + |
| 179 | + |
| 180 | +[root@localhost ~]# cat /root/Desktop/project/EtherType2 |
| 181 | +0x0800 2048 Internet Protocol version 4 (IPv4) |
| 182 | +0x0806 2054 Address Resolution Protocol (ARP) |
| 183 | +0x0842 2114 Wake-on-LAN[3] |
0 commit comments