Hello all,
I'm currently learning Python while having while having to make use of it for a course. Would someone be able to help with explaining some parts of this Ping Sweep that I don't understand? I have some beginner level java programming experience from 6 years but I'm definitely rusty.
import subprocess
#I don't understand this command.
for ping in range(1,254):
#this defines a range of numbers for "ping". I think I have an understanding of this.
address = "10.11.1." + str(ping)
#creates the address variable with "10.11.1" in the first 3 octets
res = subprocess.call(['ping', '-c', '3', address])
#I only understand what's happening in the brackets and it's creating the res variable.
if res == 0:
print "ping to", address, "OK"
#The output if res = 0
elif res == 2:
print "no response from", address
#The output if res = 2, I take it you use "elif" if there are more than 2 possible outcomes?
else:
print "ping to", address, "failed!"
#The output if nothing matches the previous 2 outcomes.
Hi,
My thoughts on the questions you asked.
Some brief answers, not sure how much detail you need, so please feel free to ask...
import subprocess
The import command "imports" a module, in this case called subprocess. This will contain standard pieces of code that the program you are writing will use.
for ping in range(1,254):
This will loop from 1( as in the 1 before the comma) unitl 254.
if res == 0:
This if statement will process the code below the colon ":" that is indented by 4 spaces, if the "res" variables equals zero "0"
Once the your code below stops being indented then it will no longer be executed as part of this "if" statement.
#The output if res = 0
elif res == 2:
print "no response from", address
#The output if res = 2, I take it you use "elif" if there are more than 2 possible outcomes?
Correct - code executed as above
else:
print "ping to", address, "failed!"
#The output if nothing matches the previous 2 outcomes.
Correct - code executed as above
Hope this helps
Bass
AWESOME, Thanks! I that helps a lot now when I try to run it in Linux I get a "syntax error near unexpected token ('" do you know what could be causing that?
Hi,
I don't use Linux as such, so not fully qualified to answer, but...
Doing a quick search on Google, it looks like your Python script is being run outside of Python and to fix this you need to add this line to the top of your script:
#!/usr/bin/python
This should ensure that the script is executed in Python. I may be wrong - but it should be worth a try. Add the line above and see what happens...
Good Luck
Bass
Since Python 3.3 there is a nice module in the stdlib for ipaddress.
import ipaddress
start_ip = ipaddress.ip_address('192.168.0.1')
for i in range(254):
print(start_ip + i)
Output:
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
192.168.0.6
192.168.0.7
192.168.0.8
192.168.0.9
192.168.0.10
192.168.0.11
192.168.0.12
192.168.0.13
192.168.0.14
192.168.0.15
192.168.0.16
192.168.0.17
192.168.0.18
192.168.0.19
192.168.0.20
192.168.0.21
192.168.0.22
192.168.0.23
192.168.0.24
192.168.0.25
192.168.0.26
192.168.0.27
192.168.0.28
192.168.0.29
192.168.0.30
192.168.0.31
192.168.0.32
192.168.0.33
192.168.0.34
192.168.0.35
192.168.0.36
192.168.0.37
192.168.0.38
192.168.0.39
192.168.0.40
192.168.0.41
192.168.0.42
192.168.0.43
192.168.0.44
192.168.0.45
192.168.0.46
192.168.0.47
192.168.0.48
192.168.0.49
192.168.0.50
192.168.0.51
192.168.0.52
192.168.0.53
192.168.0.54
192.168.0.55
192.168.0.56
192.168.0.57
192.168.0.58
192.168.0.59
192.168.0.60
192.168.0.61
192.168.0.62
192.168.0.63
192.168.0.64
192.168.0.65
192.168.0.66
192.168.0.67
192.168.0.68
192.168.0.69
192.168.0.70
192.168.0.71
192.168.0.72
192.168.0.73
192.168.0.74
192.168.0.75
192.168.0.76
192.168.0.77
192.168.0.78
192.168.0.79
192.168.0.80
192.168.0.81
192.168.0.82
192.168.0.83
192.168.0.84
192.168.0.85
192.168.0.86
192.168.0.87
192.168.0.88
192.168.0.89
192.168.0.90
192.168.0.91
192.168.0.92
192.168.0.93
192.168.0.94
192.168.0.95
192.168.0.96
192.168.0.97
192.168.0.98
192.168.0.99
192.168.0.100
192.168.0.101
192.168.0.102
192.168.0.103
192.168.0.104
192.168.0.105
192.168.0.106
192.168.0.107
192.168.0.108
192.168.0.109
192.168.0.110
192.168.0.111
192.168.0.112
192.168.0.113
192.168.0.114
192.168.0.115
192.168.0.116
192.168.0.117
192.168.0.118
192.168.0.119
192.168.0.120
192.168.0.121
192.168.0.122
192.168.0.123
192.168.0.124
192.168.0.125
192.168.0.126
192.168.0.127
192.168.0.128
192.168.0.129
192.168.0.130
192.168.0.131
192.168.0.132
192.168.0.133
192.168.0.134
192.168.0.135
192.168.0.136
192.168.0.137
192.168.0.138
192.168.0.139
192.168.0.140
192.168.0.141
192.168.0.142
192.168.0.143
192.168.0.144
192.168.0.145
192.168.0.146
192.168.0.147
192.168.0.148
192.168.0.149
192.168.0.150
192.168.0.151
192.168.0.152
192.168.0.153
192.168.0.154
192.168.0.155
192.168.0.156
192.168.0.157
192.168.0.158
192.168.0.159
192.168.0.160
192.168.0.161
192.168.0.162
192.168.0.163
192.168.0.164
192.168.0.165
192.168.0.166
192.168.0.167
192.168.0.168
192.168.0.169
192.168.0.170
192.168.0.171
192.168.0.172
192.168.0.173
192.168.0.174
192.168.0.175
192.168.0.176
192.168.0.177
192.168.0.178
192.168.0.179
192.168.0.180
192.168.0.181
192.168.0.182
192.168.0.183
192.168.0.184
192.168.0.185
192.168.0.186
192.168.0.187
192.168.0.188
192.168.0.189
192.168.0.190
192.168.0.191
192.168.0.192
192.168.0.193
192.168.0.194
192.168.0.195
192.168.0.196
192.168.0.197
192.168.0.198
192.168.0.199
192.168.0.200
192.168.0.201
192.168.0.202
192.168.0.203
192.168.0.204
192.168.0.205
192.168.0.206
192.168.0.207
192.168.0.208
192.168.0.209
192.168.0.210
192.168.0.211
192.168.0.212
192.168.0.213
192.168.0.214
192.168.0.215
192.168.0.216
192.168.0.217
192.168.0.218
192.168.0.219
192.168.0.220
192.168.0.221
192.168.0.222
192.168.0.223
192.168.0.224
192.168.0.225
192.168.0.226
192.168.0.227
192.168.0.228
192.168.0.229
192.168.0.230
192.168.0.231
192.168.0.232
192.168.0.233
192.168.0.234
192.168.0.235
192.168.0.236
192.168.0.237
192.168.0.238
192.168.0.239
192.168.0.240
192.168.0.241
192.168.0.242
192.168.0.243
192.168.0.244
192.168.0.245
192.168.0.246
192.168.0.247
192.168.0.248
192.168.0.249
192.168.0.250
192.168.0.251
192.168.0.252
192.168.0.253
192.168.0.254
21.28. ipaddress — IPv4/IPv6 manipulation library
If you want to get a range for a whole network, you should use following:
ip_network_24 = ipaddress.ip_network('192.168.0.0/24') # Netmask = 255.255.255.0
ip_network_16 = ipaddress.ip_network('192.168.0.0/16') # Netmask = 255.255.0.0
print('Whole 192.168.0.0/24 network:')
for ip in ip_network_24.hosts():
print(ip)
# Network Address and Braodcast Address are excluded.
# 254 hosts
print('Whole 192.168.0.0/16 network:')
for ip in ip_network_16.hosts():
print(ip)
# 65534 hosts
This module saves you against string manipulation. All important logic is inside the module and has a good abstraction. This saves brain-time.