But first, a little background:
NIS stands for Network Information Service and it’s basically a way for your networked computers to share information about things like user accounts, printers, and other resources. It’s kind of like having one big phone book that everyone can access instead of each computer having its own separate list. And the best part? You don’t have to be a Linux wizard to use it!
So how do we interact with NIS from Python? Well, there are actually two ways: using the `nis` module or using the `pypure` package (which is essentially just a wrapper around the `nis` module). Let’s take a look at both options.
To kick things off, let’s use the built-in `nis` module. This requires that you have NIS installed on your system and that you know how to configure it properly. If you don’t want to deal with all of that hassle (and who can blame you?), then skip ahead to the next section where we talk about using `pypure`.
To use the `nis` module, simply import it at the top of your Python script:
# Import the nis module
import nis
# The nis module allows for interaction with the Network Information Service (NIS) on a system.
# To use the nis module, it must be installed on the system and properly configured.
# To avoid dealing with installation and configuration, we will use the pypure module instead.
# To use the pypure module, simply import it at the top of your Python script:
# Import the pypure module
import pypure
# The pypure module allows for easy interaction with the NIS without the hassle of installation and configuration.
# Now, we can use the pypure module to interact with the NIS without any additional setup.
# Note: It is important to properly import modules at the top of the script to ensure they are available for use throughout the code.
Then, create a NIS object and connect to the server:
# Create a NIS object and connect to the server
# Replace 'your.nis.domain' with your actual domain name
server = 'your.nis.domain'
# Import the nis module
import nis
# Create a client object using the NIS class and passing in the server variable
client = nis.NIS(server)
Now you can start querying for information! For example, let’s say we want to find out who has access to a certain printer:
# This script is used to query for information about a specific printer and its access permissions.
# First, we define the name of the printer we want to query for.
printer_name = 'my-awesome-printer' # replace with your actual printer name
# Next, we create a query using the NIS_MAP function from the nis module, specifying the 'print/printers' map and the printer name as the suffix.
query = nis.NIS_MAP('print/printers') + [(nis.NISSUFFIX, printer_name)]
# Then, we use the client's request function to send the query and store the results in a variable.
results = client.request(query)
# Finally, we iterate through the results and print out the user's name and the printer they have access to.
for result in results:
print("User:", result[0][1], "has access to", printer_name)
# Explanation:
# - The NIS_MAP function is used to specify the map we want to query from.
# - The NISSUFFIX parameter is used to specify the suffix of the map, in this case, the printer name.
# - The client's request function is used to send the query and retrieve the results.
# - The results are then iterated through and the user's name and printer name are printed out.
# - This script can be modified to query for different printers by changing the printer_name variable.
And that’s it! You can use the `nis` module to query for all sorts of information, from user accounts to network services. But if you don’t want to deal with configuring NIS or dealing with its quirks (like the fact that it doesn’t support IPv6 addresses), then there’s another option: using `pypure`.
`pypure` is a Python package that provides an easy-to-use interface for interacting with NIS. It handles all of the configuration and quirks for you, so you can focus on writing your code instead of worrying about low-level details. To install it, simply run:
# This script installs the pypure Python package using the pip command.
# First, we need to make sure that pip is installed on the system.
# We can do this by running the following command:
sudo apt-get install python-pip
# Once pip is installed, we can use it to install pypure.
# The following command will install the latest version of pypure:
pip install pypure
# Note: It is recommended to use the -U flag to ensure that pypure is updated to the latest version.
# The command would then be:
pip install -U pypure
# After the installation is complete, we can start using pypure in our Python code.
# We can import it in our code using the following statement:
import pypure
# Now we can use the functions and methods provided by pypure to interact with NIS.
# For example, we can use the pypure.get_config() function to retrieve the current configuration of NIS.
# Overall, this script simplifies the process of interacting with NIS by handling all the configuration and details for us.
Then, import `pypure` at the top of your Python script:
# Import the `pypure` library
import pypure
# Define a function named `calculate_sum` that takes in two parameters, `num1` and `num2`
def calculate_sum(num1, num2):
# Initialize a variable `sum` and assign it the sum of `num1` and `num2`
sum = num1 + num2
# Return the value of `sum`
return sum
# Call the `calculate_sum` function with the arguments 5 and 10 and assign the result to a variable `result`
result = calculate_sum(5, 10)
# Print the value of `result`
print(result)
# Output: 15
And create a NIS object and connect to the server using the same syntax as before:
# Create a NIS object and connect to the server using the same syntax as before:
# Import the necessary library for connecting to the server
import pure
# Define the server name as a string variable
server = 'your.nis.domain' # replace with your actual domain name
# Create a client object using the pure library and passing in the server name
client = pure.Client(server) # Connect to the server using the specified domain name
Now you can start querying for information! For example, let’s say we want to find out who has access to a certain printer:
# This script is used to query for information about a specific printer and the users who have access to it.
# First, we define the name of the printer we want to query for.
printer_name = 'my-awesome-printer' # replace with your actual printer name
# Next, we create a query object using the pure.Query() function and specify the endpoint we want to query for, in this case 'print/printers'.
query = pure.Query('print/printers')
# Then, we add an attribute to our query using the pure.Attr() function. This attribute specifies the printer name we want to search for, using the NISSUFFIX attribute.
query += pure.Attr(pure.NISSUFFIX, printer_name)
# After that, we make a request to the client using the request() function and passing in our query object.
results = client.request(query)
# Finally, we iterate through the results and print out the user's gidnumber and the printer name they have access to.
for result in results:
print("User:", result['entry']['gidnumber'][0], "has access to", printer_name)
And that’s it! You can use `pypure` to query for all sorts of information, from user accounts to network services. It even supports IPv6 addresses and other advanced features that the built-in `nis` module doesn’t support. So if you want an easier way to interact with NIS in Python, give `pypure` a try!