Python script to print nslookup - python

import os
ipRange = []
for i in range(1, 254):
ipRange.append('192.168.5' + '.' + str(i))
for e in ipRange:
print os.system('nslookup ' + str(e))
This outputs the full output of nslookup for each ip - is there a way to discard the empty results and make the output look more like this?
192.168.5.5 testbox4
192.168.5.6 box3
192.168.5.8 hellobox
192.168.5.9 server2012
192.168.5.18 dnsbox
192.168.5.19 sallysbox
192.168.5.20 bobsbox
192.168.5.21 serverx

Do you need to use system? This would do without system calls:
import socket
for i in range(0, 255):
ipa = "130.233.192." + str(i)
try:
a = socket.gethostbyaddr(ipa)
print (ipa, a[0])
except socket.herror:
pass
EDIT: change 255 to 256 if you want to query .255 as well but in class C networks this is the broadcast address and not in DNS. If you are trawling through class A or B networks, then .255 could be valid as well

Related

Find next available IP address in python

Using python I need to find the next IP address given a range of IP addresses I've already used. So if I have a list of IP address like...
IPs = ['10.220.1.1','10.220.1.2','10.220.1.3','10.220.1.5']
When I ask for the next IP address I need it to return '10.220.1.4'. The next request would return '10.220.1.6' and so on.
If you're using Python 3.3 (or newer), you can use the ipaddress module. Example for all hosts in the subnet 10.220.1.0/24 except for those in reserved:
from ipaddress import IPv4Network
network = IPv4Network('10.220.1.0/24')
reserved = {'10.220.1.1', '10.220.1.2', '10.220.1.3', '10.220.1.5'}
hosts_iterator = (host for host in network.hosts() if str(host) not in reserved)
# Using hosts_iterator:
print(next(hosts_iterator)) # prints 10.220.1.4
print(next(hosts_iterator)) # prints 10.220.1.6
print(next(hosts_iterator)) # prints 10.220.1.7
# Or you can iterate over hosts_iterator:
for host in hosts_iterator:
print(host)
So basically this can be done in a single line (+ imports and definition of network and reserved addresses).
If you're using Python 3 you could use ipaddress with generator:
import ipaddress
def gen_ip(start, reserved):
start = int(ipaddress.IPv4Address(start))
for i in range(start + 1, int(2 ** 32) + 1):
ip = str(ipaddress.IPv4Address(i))
if ip not in reserved:
yield ip
IPs = ['10.220.1.1','10.220.1.2','10.220.1.3','10.220.1.5']
j = 0
for ip in gen_ip(min(IPs), set(IPs)):
print(ip)
j += 1
if j == 5:
break
Output:
10.220.1.4
10.220.1.6
10.220.1.7
10.220.1.8
10.220.1.9
You can use an generator using the ipaddress module which is a bultin from python >= 3.3 or you can install with pip for earlier versions:
In [20]: from ipaddress import ip_network
In [21]: IPs = {'10.220.1.1','10.220.1.2','10.220.1.3','10.220.1.5'}
In [22]: net = ip_network(u"10.220.1.0/24")
In [23]: avail =(str(ip) for ip in net.hosts() if str(ip) not in IPs
....: )
In [24]: next(avail)
Out[24]: '10.220.1.4'
In [25]: next(avail)
Out[25]: '10.220.1.6'
Convert your list to a set, for performance:
used_ips = set(IPs)
Now generate your IP#'s however you would like, and check if they are contained in the set:
for next_ip in generate_ip_numbers():
if next_ip in used_ips:
continue
print("Next ip address is:", next_ip)
Create a basic ip object to hold a record of your current ip and to get the next ip
class IPObj(object):
def __init__(self, list_of_ips):
self.position = 0
self.ips = list_of_ips
self.current_ip = self.ips[self.position]
def next_ip(self, stop_iteration=False):
'''
return the next ip in the list
'''
if self.position >= len(self.ips)-1:
self.position = 0 # By default next_ip will do nothing when it reaches the end but it will throw an exception if stop_iteration==True
if stop_iteration:
raise StopIteration
self.position += 1
self.current_ip = self.ips[self.position]
return self.current_ip
def __repr__(self):
return repr(self.current_ip)
#Testing
ips = IPObj(['10.220.1.1','10.220.1.2','10.220.1.3','10.220.1.5'])
print ips
while True:
print ips.next_ip(True),
Output:
10.220.1.1,
10.220.1.2,
10.220.1.3,
10.220.1.5,
Traceback (most recent call last):
File "C:\Users\J10ey\workspace\SO_Help\src\ip's.py", line 32, in
print ips.next_ip(True)
File "C:\Users\J10ey\workspace\SO_Help\src\ip's.py", line 21, in next_ip
raise StopIteration
StopIteration

How do i search in a telnet session output with re a string where x is a number between 1 and 99999?

import getpass
import sys
import telnetlib
import re
import smtplib
print "Pasul 1"
HOST = "route-views.routeviews.org"
user = "rviews"
password = ""
tn = telnetlib.Telnet(HOST)
tn.read_until("login: ", 5)
tn.write(user + "\r\n")
tn.read_until("Password: ", 5)
tn.write(password + "\r\n")
print tn.read_until(">", 10)
tn.write("show ip route 192.0.2.1"+"\r\n")
y = tn.read_until("free", 10)
print y
tn.write("exit"+ "\r\n")
tn.close()
print "Pasul 2"
for x in range(1,99999):
m = re.search(' Known via "bgp xxxxx"', y)
if m:
print (m.group(0))
break
else:
print False
break
x has to be a number between 1 and 99999
If i write ' Known via "bgp 6447"' it will find and print it, but if i write ' Known via "bgp xxxxx"', it returns false. Anybody knows why?
The output is this:
route-views>
show ip route 192.0.2.1
Routing entry for 192.0.2.1/32
Known via "bgp 6447", distance 20, metric 0
Tag 19214, type external
Last update from 208.74.64.40 4w0d ago
Routing Descriptor Blocks:
208.74.64.40, from 208.74.64.40, 4w0d ago
Route metric is 0, traffic share count is 1
AS Hops 1
Route tag 19214
MPLS label: none
route-views>
You're using regexp in a totally wrong way, try changing the whole this section:
for x in range(1,99999):
m = re.search(' Known via "bgp xxxxx"', y)
if m:
print (m.group(0))
break
else:
print False
break
with following:
m = re.search(r'Known via "bgp \d{0,5}"', y)
if m:
print m.group(0)
else:
print False
And notice r before expression, it's important here.
Probably you should read this docs for python re module: https://docs.python.org/2/library/re.html
Upd. By the way, your version does not works because x inside string is interpreted as literal "x", not the value of variable x. If you want to put a variable inside a string you should use formatting like in this example:
x = 12345
print ' Known via "bgp {}"'.format(x)
It gives me 'True' if I test
>>> y = ' Known via "bgp xxxxx"'
>>> re.search('Known via "bgp xxxxx"', y)
>>> if x:
... print "yes"
...
yes

Python: get name of a USB flash drive device [windows]

I'm trying to write little program which will be able to read some informations about REMOVEABLE_DEVICE (USB). I've tried pyusb but I was not able to pull data I need.
I would like to read from the system the name of the USB device.
In this format:
USB Flash Memory - this is the model information
Removable Disk (H:) - this is the name of device
Generic Flash Disk
USB DISK (F:)
Lexar USB Flash Drive
Lexar (I:)
I am able to get the model information with win32com.client library, inspired from here, but I am not able to get name of the device shown in Windows explorer.
Maybe I am using wrong library?
Here is my code:
import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("SELECT * FROM Win32_DiskDrive WHERE InterfaceType = \"USB\"")
for objItem in colItems:
if objItem.Caption != None:
print "Caption:" + ` objItem.Caption[:-11]`
Here is link for Windows Win32_DiskDrive Class: link
Thank you in advance for your help.
Disclaimer: I haven't really got any experience with the win32com.client library.
By starting with Win32_DiskDrive like you did, I went over Win32_DiskDriveToDiskPartition and Win32_LogicalDiskToPartition, and then to Win32_LogicalDisk to get the VolumeName which seems what you want.
import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
# 1. Win32_DiskDrive
colItems = objSWbemServices.ExecQuery("SELECT * FROM Win32_DiskDrive WHERE InterfaceType = \"USB\"")
DiskDrive_DeviceID = colItems[0].DeviceID.replace('\\', '').replace('.', '')
DiskDrive_Caption = colItems[0].Caption
print 'DiskDrive DeviceID:', DiskDrive_DeviceID
# 2. Win32_DiskDriveToDiskPartition
colItems = objSWbemServices.ExecQuery("SELECT * from Win32_DiskDriveToDiskPartition")
for objItem in colItems:
if DiskDrive_DeviceID in str(objItem.Antecedent):
DiskPartition_DeviceID = objItem.Dependent.split('=')[1].replace('"', '')
print 'DiskPartition DeviceID:', DiskPartition_DeviceID
# 3. Win32_LogicalDiskToPartition
colItems = objSWbemServices.ExecQuery("SELECT * from Win32_LogicalDiskToPartition")
for objItem in colItems:
if DiskPartition_DeviceID in str(objItem.Antecedent):
LogicalDisk_DeviceID = objItem.Dependent.split('=')[1].replace('"', '')
print 'LogicalDisk DeviceID:', LogicalDisk_DeviceID
# 4. Win32_LogicalDisk
colItems = objSWbemServices.ExecQuery("SELECT * from Win32_LogicalDisk WHERE DeviceID=\"" + LogicalDisk_DeviceID + "\"")
print 'LogicalDisk VolumeName:', colItems[0].VolumeName
print
# putting it together
print DiskDrive_Caption
print colItems[0].VolumeName, '(' + LogicalDisk_DeviceID + ')'
Works for me:
DiskDrive DeviceID: PHYSICALDRIVE1
DiskPartition DeviceID: Disk #1, Partition #0
LogicalDisk DeviceID: D:
LogicalDisk VolumeName: PENDRIVE
Sony Storage Media USB Device
PENDRIVE (D:)
This seems to provide a complicated, but possible way, maybe you can simplify it even more. My only simplification is leaving out Win32_DiskPartition already because we only need the connection.
Please note:
I'm not sure what's the "clean" way to unpack something like \\.\PHYSICALDRIVE1, but it should be possible to get rid of the .replace()-methods.
I'm not sure if it's possible to integrate the loops in steps 2 and 3 into the query? That would also simplify it a lot (maybe it's possible to JOIN them SQL-like?).
(The code above will only work for a single USB drive.)
I will look odd but it will work (get list of all removable volume's Label's)
import os
os.system('echo list volume > Ravi.txt')
path1 = os.path.join(os.getcwd(),"Ravi.txt")
os.system('diskpart /s '+path1+' > logfile.txt')
path2 = os.path.join(os.getcwd(),"logfile.txt")
Str = open(path2).read()
Str = Str.split('\n')
matching = [s for s in Str if "Removable" in s]
for i in matching:
i = ' '.join(i.split())
i = i.split(" ")
print i[3]+"("+i[2]+":)"
output
MYPENDRIVE(D:)
I used the approach of #adrianus and improved a bit on it to return multiple usb drives. For how it works check his answer. For quick and dirty code that hopefully works for you check below :)
def get_usb_volume_name(): # pragma: no cover
str_computer = "."
logical_disk_device_ids = []
volumes = []
try:
obj_wmi_service = win32com.client.Dispatch("WbemScripting.SWbemLocator")
obj_swbem_services = obj_wmi_service.ConnectServer(str_computer, "root\cimv2")
# 1. Win32_DiskDrive
col_items = obj_swbem_services.ExecQuery("SELECT * FROM Win32_DiskDrive WHERE InterfaceType = \"USB\"")
for item in col_items:
disk_drive_device_ids = item.DeviceID.replace('\\', '').replace('.', '')
# 2. Win32_DiskDriveToDiskPartition
col_items = obj_swbem_services.ExecQuery("SELECT * from Win32_DiskDriveToDiskPartition")
disk_partition_device_ids = []
for obj_item in col_items:
for disk_drive_device_id in disk_drive_device_ids:
if disk_drive_device_id in str(obj_item.Antecedent):
disk_partition_device_ids.append(obj_item.Dependent.split('=')[1].replace('"', ''))
break
# 3. Win32_LogicalDiskToPartition
col_items = obj_swbem_services.ExecQuery("SELECT * from Win32_LogicalDiskToPartition")
for objItem in col_items:
for disk_partition_device_id in disk_partition_device_ids:
if disk_partition_device_id in str(objItem.Antecedent):
logical_disk_device_ids.append(objItem.Dependent.split('=')[1].replace('"', ''))
break
# 4. Win32_LogicalDisk
col_items = []
for logical_disk_device_id in logical_disk_device_ids:
col_items.append(obj_swbem_services.ExecQuery("SELECT * from Win32_LogicalDisk WHERE DeviceID=\"" +
logical_disk_device_id + "\""))
for col_item in col_items:
volumes.append(col_item[0].VolumeName)
except IndexError:
pass
volumes_result = []
logical_disk_device_ids_result = []
for i in range(len(volumes)):
if volumes[i] != "":
volumes_result.append(volumes[i])
logical_disk_device_ids_result.append(logical_disk_device_ids[i])
return logical_disk_device_ids_result, volumes_result

Find / Slicing Method issue

I was trying to use the find and string slicing method to extract the number at the end of the line. but I get this mismatch error because I came back with position 18 but from what I have read and research this position is suppose to be 18 am I missing something here?
str = ('X-DSPAM-Confidence:0.8475')
atpos = str.find(':')
print atpos
sppos = str.find(' ',atpos)
print sppos
host = float(str[atpos + 1:sppos])
print host
str = ('X-DSPAM-Confidence:0.8475')
atpos = str.find(':')
sppos = str.find(' ',atpos)
host = float(str[atpos + 1:])
print (host)
You should rather use the string.split method than looking for the specific position of the : delimiter,
_str = 'X-DSPAM-Confidence:0.8475'
host = float(_str.split(':')[1])
print(host)
str = ('X-DSPAM-Confidence:0.8475')
atpos = str.find(':')
sppos = str.find(' ',atpos)
host = float(str[atpos + 1:])
print host
text = "X-DSPAM-Confidence: 0.8475";
spacePos = text.find(" ")
number = text[spacePos::1]
#not really necessary but since we are just learning and playing
strippedNumber = number.lstrip();
result = float(strippedNumber)
def reprint(printed):
print printed
reprint(result)

Python scp copy file with spaces in filename

I'm trying to copy files in local network with scp.
It's working well with filenames without spaces, but it crash with.
I've tried to replace " " with "\ " as this exemple, but it don't work.
Here is my code:
def connection(locals):
a = (int(re.search(br'(\d+)%$', locals['child'].after).group(1)))
print a
perc = (Decimal(a)/100)
print (type(perc)), perc
while gtk.events_pending():
gtk.main_iteration()
FileCopy.pbar.set_text("Copy of the file in the Pi... " + str(a) + "%")
while gtk.events_pending():
gtk.main_iteration()
FileCopy.pbar.set_fraction(perc)
file_pc = "/home/guillaume/folder/a very large name of file with space .smthg"
file_pi = "pi#192.168.X.X:/home/pi/folder/a very large name of file with space .smthg"
if " " in file_pc:
file_pc = fichier_pc.replace(" ", '\\\ ') # tried '\\ ' or '\ '
file_pi = fichier_pi.replace(" ", '\\\ ') # but no way
else:
pass
command = "scp %s %s" % tuple(map(pipes.quote, [file_pc, file_pi]))
pexpect.run(command, events={r'\d+%': connection}) # this command is using to get the %
How can I fix this problem ?
Thanks
Use subprocess module and/or shlex.split():
import subprocess
subprocess.call(['scp', file_pc, file_pi])
and you don't need to worry about escaping or quoting anything
You may keep local file file_pc as is (pipes.quote will escape the spaces). The remote file should be changed:
import pipes
file_pi = 'pi#192.168.X.X:/home/pi/folder/file with space.smth'
host, colon, path = file_pi.partition(':')
assert colon
file_pi = host + colon + pipes.quote(path)
i.e., user#host:/path/with space should be changed to user#host:'/path/with space'
You might want to look into fabric, a Python library that streamlines the use of SSH.
from fabric.state import env
from fabric.operations import get
env.user = 'username'
env.key_filename = '/path/to/ssh-key'
get('/remote_path/*', 'local_path/')

Categories

Resources