Programming networks in Python is one of the most important topics that many programmers are looking for due to the great capacity provided by the Python language in this field.
There are many, many libraries for dealing with servers and network devices using the Python programming language. In this article, we can say that the library we are talking about helps in dealing with devices remotely, and it may not be 100% specialized in the topic of network programming in Python, but it can be relied upon strongly during communication and remote communication with devices that do not connect to ssh.
Using ssh in a Pythonian way
At first, I assume dear reader that you have a prior knowledge of the basics of Python, especially how to install its libraries and how to call them. Fabric is one of the most popular libraries for automating application and server management tasks over the ssh connection, and its main goal is to provide the ability to send Shell commands to devices over the network using an ssh connection.
Programming networks in Python, for example
Suppose we have two devices across the network, and we want to track the storage space using the df command, for example (the df command is one of the commands used in the Linux system and is used to know the status of the storage capacity).
We have two ways to know the storage capacity of each device, the first way is to connect to each device separately via ssh and execute the df command and follow the output of each device separately. The second method is to use a system or tool that speeds up and shortens the previous process, and here comes the role of Fabric for Python fans.
Using the Fabric library is very easy, all we have to do is install the library using the pip command and then build a file called fabfile.py that contains the commands that we want to execute on the servers and target devices (in our example the df command) and then in the command screen use the fab tool to execute the file.
What is the fab tool?
There are two ways to use the Fabric library, the first is to import the library into the Python file that we want, and then call the library's functions in the appropriate way, and the second way is to use the fab tool.
The fab tool is a tool of the Fabric library that is used from the command screen, and when working with it in this example, the fabfile.py file should be located in the same run.
Let the devices' data be as follows:
Server IP | Server User Name | Password |
---|---|---|
192.168.1.100 | Ali | 777777 |
192.168.1.101 | Huda | A212121 |
We write the following code downfabric into a file and save it as fabfile.py.
Then, in the same path of the fabfile.py file, we execute the command from the command screen:
The result of the previous command will be the result of the df command for both target devices, knowing that we can deal with this result, analyze and save it in a database or publish it on a page or within a specific application.
read also :Why should you learn the Python programming language now?
Comments
Post a Comment