Create a python script and use grep command? - python

I'm creating a script wherein I want to grep all a specific address based on the list?
before what I usually do run a grep 1 by 1 using this command ex. grep "192.168.1.1" *
Now I'm creating a script.
Example of the output.
print(i) output.
192.168.1.0
192.168.1.1
192.168.1.2
192.168.1.3
but how to call the list and put into loop under os.system so I can grep all the list?
Thanks
import ipaddress
import os
#Ask the ipaddress in CIDR format
ip = input("Enter the IP/CIDR: ")
os.chdir("/rs/configs")
print("pwd=%s" % os.getcwd())
for i in ipaddress.IPv4Network(ip):
print (i)
os.system("grep $i '*') #<--Grep from list and run to all directory *

The basic answer is "grep {} '*'".format(ip) but there are a number of problems with your script.
To improve usability, I would suggest you change the script so it accepts a list of IP addresses as command-line arguments instead.
You want to avoid os.system() in favor of subprocess.run()
There is no need to cd to the directory which contains the files you want to examine.
Finally, there is no need really to run grep, as Python itself is quite capable of searching a set of files.
import ipaddress
import glob
ips = set([ipaddress.IPv4Network(ip) for ip in sys.argv[1:]])
for file in glob.glob('/rs/configs/*'):
with open(file) as lines:
for line in lines:
if any(x in line for x in ips):
print("{0}:{1}".format(file, line))
This should be significantly more efficient by way of examining the files only once.
It's not entirely clear what you hope to gain by using ipaddress here if you are grepping for individual IP addresses anyway.

Related

find name of nfs server with python [duplicate]

How do I iterate through the mount points of a Linux system using Python? I know I can do it using df command, but is there an in-built Python function to do this?
Also, I'm just writing a Python script to monitor the mount points usage and send email notifications. Would it be better / faster to do this as a normal shell script as compared to a Python script?
Thanks.
The Python and cross-platform way:
pip install psutil # or add it to your setup.py's install_requires
And then:
import psutil
partitions = psutil.disk_partitions()
for p in partitions:
print p.mountpoint, psutil.disk_usage(p.mountpoint).percent
Running the mount command from within Python is not the most efficient way to solve the problem. You can apply Khalid's answer and implement it in pure Python:
with open('/proc/mounts','r') as f:
mounts = [line.split()[1] for line in f.readlines()]
import smtplib
import email.mime.text
msg = email.mime.text.MIMEText('\n'.join(mounts))
msg['Subject'] = <subject>
msg['From'] = <sender>
msg['To'] = <recipient>
s = smtplib.SMTP('localhost') # replace 'localhost' will mail exchange host if necessary
s.sendmail(<sender>, <recipient>, msg.as_string())
s.quit()
where <subject>, <sender> and <recipient> should be replaced by appropriate strings.
The bash way to do it, just for fun:
awk '{print $2}' /proc/mounts | df -h | mail -s `date +%Y-%m-%d` "you#me.com"
I don't know of any library that does it but you could simply launch mount and return all the mount points in a list with something like:
import commands
mount = commands.getoutput('mount -v')
mntlines = mount.split('\n')
mntpoints = map(lambda line: line.split()[2], mntlines)
The code retrieves all the text from the mount -v command, splits the output into a list of lines and then parses each line for the third field which represents the mount point path.
If you wanted to use df then you can do that too but you need to remove the first line which contains the column names:
import commands
mount = commands.getoutput('df')
mntlines = mount.split('\n')[1::] # [1::] trims the first line (column names)
mntpoints = map(lambda line: line.split()[5], mntlines)
Once you have the mount points (mntpoints list) you can use for in to process each one with code like this:
for mount in mntpoints:
# Process each mount here. For an example we just print each
print(mount)
Python has a mail processing module called smtplib, and one can find information in the Python docs

Using console commands in python

I am using console commands in python, however, it is not outputting the value I want.
The path is:
#ifconfig -a | grep "HWaddr"
From this command I get:
eth0 Link encap:Ethernet HWaddr 30:9E:D5:C7:1z:EF
eth1 Link encap:Ethernet HWaddr 30:0E:95:97:0A:F0
I need to use console commands to retrieve that value, so this is what I have so far for code:
def getmac():
mac=subprocess.check_output('ifconfig -a | grep "HWaddr"')
print "%s" %(mac)
I basically only want to retrieve the hardware address which is 30:0E:D5:C7:1A:F0. My code above doesn't retrieve that. My question is how do I use console commands to get the value I want.
Thanks in advance.
The most robust and easy way in Linux to get the MAC address is to get it from sysfs, mounted on /sys.
For interface etho, the location would be /sys/class/net/eth0/address; Similarly for eth1, it would be /sys/class/net/eth1/address.
% cat /sys/class/net/eth0/address
74:d4:35:XX:XX:XX
So, you could just read the file in python too:
with open('/sys/class/net/eth0/address') as f:
mac_eth0 = f.read().rstrip()
Quoting from here.
Python 2.5 includes an uuid implementation which (in at least one version) needs the mac address. You can import the mac finding function into your own code easily:
from uuid import getnode as get_mac
mac = get_mac()
The return value is the mac address as 48 bit integer.
import subprocess
def getmac(command):
return subprocess.check_output(command, shell=True)
command = "ifconfig -a | grep HWaddr"
print "%s" %(getmac(command).split()[9])
# or print out the entire list to see which index your HWAddr corresponds to
# print "%s" %(getmac(command).split())
Or as per user heemayl,
command = "cat /sys/class/net/eth1/address"
print "%s" %(getmac(command))
Note:
1. Using shell=True isn't recommended as per Python docs
2. This isn't as efficient compared to the normal ways of reading a file in Python.
You could have also returned
subprocess.check_output(command)
However, in the above case, you might get an OSError or a CalledProcessError(retcode, cmd, output=output) depending on whether you passed your commands as a list, which can be solved if you explicitly mention your python path as per this

Using python socket.gethostbyname to accept multiple arguments in the command Prompt

I have a text file with a list of about 50 hostnames and I am looking to script a way to run through them to get each associated IP address in the Command Prompt.
I thought pasting the hostname list in to the following code might be the easiest way but socket.gethostbyname will take no more than 1 argument at a time.
import socket
socket.gethostbyname("***hostnames***")
Is there a way to work around this argument issue, or is there a way to have the hostnames read from the textfile?
The easiest work around is to pass a filename and iterate through it:
#!/usr/bin/python
import sys
import socket
file_nm = sys.argv[1]
with open(file_nm, 'r') as f:
for host in f:
print socket.gethostbyname(host.strip())

How to use python to run a list from one file through another script

Alright, so I am being tasked with a large task of auditing the use of our IPs in my office because we are running low. So I am trying to write a basic script that takes a list of the IPs I need to verify , and tun them against another script that verifies the IP's use. I have it working in Bash, but I want to use Python so that I can make some adjustments the way I like.
So I have a file called "iprange" and I have a perl file a former coworker made that no longer works here, and it takes an IP and checks it, verifies what is using it, and then outputs it back. Here is example of what "works" for me in Bash.
cat iprange | xargs -I % checkIP.pl -s % > ipresults.txt 2>&1
The problem is, This gives me an output that looks like this.
"This IP is free"
"This IP is free"
"This IP is used by XPNSE43525"
There are two problems. 1) There are thousands of IPs, so I need to find a way to have it so it looks more like this.
10.4.8.5
"This IP is free"
OR
checkIP.pl -s 10.4.8.5
"This IP is free"
Since this does not seem possible with bash, I am hoping to get this hammered out with Python. But have no idea how to get python to launch the script and output it the same way I do with bash.
You could always write a small wrapper script that is invoked by xargs and which in turn displays the IP address and then invokes checkIP.pl.
checkIP.sh
#!/usr/bin/env sh
echo -n "$1 : "
checkIP.pl -s $*
Then you can call it like this:
cat iprange | xargs -I % checkIP.sh % > ipresults.txt 2>&1
and expect to see output like this:
10.4.8.5 : This IP is free
10.1.1.1 : This IP is free
192.168.1.1 : This IP is used by XPNSE43525
8.8.8.8 : This IP is used by Google DNS
What #squiguy said is correct. You probably need to modify the PERL script.
grep "This IP is free" checkIP.pl
grep "This IP is used by" checkIP.pl
Find the lines in the file where the lines are on in the script by running a couple of greps
Then assuming the script used 'Getopt::Std qw(getopts);' the variable that the -s' parameter should be attached to is "$opt_s"
Change the lines to
"$opt_s: This IP is free"
"$opt_s: This IP is used by"

Using python nmap module to scan hosts generated from a previous scan

I've been playing with the module straight from the python command line to try and figure out how it all works, and start to piece together how the script I want to write is going to need to work. What I'd like to do, is do a simple host discovery scan first, such as -n -sP -PE, then use the all_hosts() function to generate the host list for the actual port scan. So if I do...
import nmap
nm = nmap.PortScanner()
nm.scan(hosts='XXX.XXX.XXX.X/24', arguments='-n -sP -PE')
Then nm.all_hosts() gives me exactly what I'm looking for, a shortened list of all the active hosts that the scan found. Now, the problem I'm having is passing that into the next scan. If you just do something like
hostlist = nm.all_hosts()
nm.scan(hosts=hostlist etc)
Then it complains about not being able to use a list for the hosts argument. Ok, makes sense. So I tried to make it comma separted, so they'd show up as aaa.aaa.aaa.aaa, bbb.bbb.bbb.bbb etc, by doing...
hostlist = ""
for item in nm.all_hosts():
hostlist = item + ", " + hostlist
Then, just dumping hostlist, it looks just how I'd like it to, but if you try to plug that into the hosts argument, it says "Failed to resolve "alltheipslisted" WARNING: No targets were specified, so 0 hosts scanned.
Does anyone have any good ideas for how to go about this? Maybe dumping the IPs to then pulling them from a file? Seems like I'd run into the same problem if a string isn't working...
If you remove the comma it will work. Multiple hosts are listed with only a space between them.
Example of use:
import nmap
nm = nmap.PortScanner()
hostlist = ' '.join(nm.all_hosts())
nm.scan(hosts=hostlist, arguments='-n -sP -PE')

Categories