Writing perl network software




















This is true of most IT jobs, and especially true for system or network administrator. Networking jobs often involve repetitive tasks. If these tasks can be automated, then the network engineer has more time to work on something else.

A company would rather hire one engineer who can automate the simple tasks than two engineers who are both wasting time doing repetitive tasks. There are a ton of programming languages out in the wild.

Many are esoteric or uncommon, but even if you only think about the most common languages you will easily be able to list two dozen languages. Luckily, only some of these are going to be applicable to the networking field.

There's a handful of scripting languages that are truly worth learning for a network engineer. There are not many networking specific languages. But there are a number of programming languages that are more useful and more common in the networking field. This is due to a number of reasons, including the difficulty of the language, the compatibility of the language with networking software and the popularity of the language.

Python is a general purpose language. This means it is not built for a specific field. Instead it is a good all around language used for many different things. This includes writing scripts and developing networking tools. It is also a great beginner language. The first thing to know about Python is that it is extremely readable. This means that reading Python code is relatively simple and intuitive.

This readability factor makes it one of the most popular beginner programming languages. Python is formatted in such a way that you will not have to spend hours trying to decrypt code just to understand what it does. When I say that Python is a beginner programming language, I do not mean it is less powerful than other languages. Python is very powerful and can do just about anything you could ask a computer program to do.

What I do mean is that Python is a common language chosen by coding beginners. Code is important for modern network engineers, but coding is not necessarily going to be the main focus of a network engineer's job. A network engineer does not have as much time to spend learning languages. This is why a language like Python is a great choice.

It is powerful, but simple to learn. If you want to start learning Python a great beginner book is Python Crash Course. It will get you up and coding quickly. If you already have some software skills, then a networking specific book like Mastering Python Networking is going to be super helpful.

Go was made to be intuitive enough that new engineers at Google could pick it up relatively quickly. Go is a strongly typed language, which makes it a lot different from Python. Go is especially powerful when it comes to creating network software.

It is a great language for developing distributed systems, APIs and other network related software. One reason is that Go is great at concurrency.

It is able to handle a bunch of things happening at once. Go also goes hand in hand with protobufs Protocol buffers. Protobufs are another Google invention. Holds a list of paths, where Perl library modules or scripts can be looked into while executing the current script.

This INC is used by use and require statements to look into those paths for library modules. This is the array into which the input lines are stored when auto split —a command line switch is used.

The file name will be the keys; values will be the path to those files. Used by do, use and require. Perl regular expression is strong enough in matching the string patterns within a statements or group of statements.

Regular expressions are mostly used in text parsing, pattern matching and much more based on the requirement. Before going further, We need to know few things about regular expression; there are certain things like Meta characters, wildcards in Perl Regex syntax. Consider a situation where user provides some input during script execution, and we want to check whether the user entered some name as input or not.

We have to write a regular expression syntax to extract your name and print the same. The pattern can be anywhere in the variable. This operator can be used for searching and replacing any character with either null or some other character.

Note: We can actually use any pattern matching string as we did earlier in this substitution operator as well. This is similar to Substitution, but it does not use any perl regular expressions, rather we can directly pass the value or a word which we want to replace. Perl main advantage is in file parsing and handling the files. There are many inbuilt functions and operators used during file handling in Perl.

Consider we have a perl file with name file. We need to open this file and print the same. This will write the input provided during run-time and creates a file test.

The above way will always try to create a file named test. Seek function is similar to the fseek system call. This method is used to position the file pointer to a specific location by specifying the bytes followed by either start of the file pointer or end of the file pointer.

Zero will set it from the beginning of the file. Subroutines are similar to functions in other programming languages. We have already used some built-in functions like print, chomp, chop, etc.

We can write our own subroutines in Perl. These subroutines can be written anywhere in the program; it is preferable to place the subroutines either at the beginning or at the end of the code. Subroutines or perl function are written to place the reusable code in it. Most of the reusable code requires parameters to be passed to the subroutine.

Here, we will learn how we can pass arguments to the subroutine. Subroutines are typically used in object-oriented programming and also in places where you may have more reusable code to be placed. Perl has a mechanism using which we can generate reports. Using this feature, we can make reports exactly the way we want while printing on the Output screen or in a file. A simple format can be written using printf or sprintf functions available in Perl.

This will include leading zeros in front of number 30 making a total count of digits to 5. The same can be used for sprintf.

Using printf and sprintf, we can obtain most of the Perl formats. In the case of reports, it will be difficult to implement. This can be achieved by using the perl printf and perl sprintf. It can effectively be implemented using format. A format can be declared in the below manner. Here, we will be using a particular method write to print the data onto output screen or into the file. We can change this to file handler which we are using to write data into the file.

This will output the format to the file which we are working on. Each programmer will have his own sense of writing the code using certain standards; these standards should be familiar enough that other programmer can understand and support the code properly.

Writing code is simple and easy. The problem arises when it needs to be maintained at later stages. Proper guidelines and coding standards need to be followed while writing the code. Perl also defines certain standards that will be useful for programmers to write code.

Each of these modules has its own importance. Strict will make us declare the variable before using and also will tell if any bare word present in your code. Warnings will print on the output screen. An exception is an event that occurs during the program execution which will suspend or terminate your program. Error Handling is one which every programmer has to take care during programming. Perl also provides error handling techniques with which we can trap the error and handle those accordingly.

There are many ways to check for error in the program. We need to examine the return codes of the function that we are using code. If we are able to handle those return codes properly, then most of the error handling can be achieved. Eval function can handle fatal errors, compile time errors, runtime errors and those errors which terminates your code at some point in time. Perl Eval function can have a block of code or an expression.

Evals considers everything placed in it as a string. Consider a situation of calling a subroutine which is not defined in the script.

There are many uses of evals block; one such use is when we want to load the module which is specific to the operating system during runtime. Perl does not support try, catch and finally code blocks as other programming languages. We can still use them by loading an external Perl module. The socket is a medium through which two computers can interact on a network by using network address and ports.

Suppose, A Server and B Client are two systems, which has to interact with each other using Sockets for running some programs. To implements this we need to create sockets in both A Server and B Client , A will be in the receiving state and B will be in the sending state. Here, the server wishes to receive a connection from B Client and execute some tasks and send the result back to B Client.

When we execute the code, the operating system in A tries to create a socket and binds one port to that socket. Then it will listen from the sender which is B. Here, the client wishes to send some program from his system to A Server for some processing.

When we execute the code, the operating system in B tries to create a socket for communicating with A Server , B has to specify the IP address and the port number of A to which B wishes to connect.

If this goes well, both systems will interact to exchange the information through one port. Perl also supports socket programming. Perl has a native API through which sockets can be implemented. To make it easy, there are many CPAN modules using which we write socket programs. This is one module for socket programming, which is based on object oriented programming. This module does not support the INET network type used in networks. This hash is predefined, and we just need to provide the values to the keys which we want to use.

There is a list of keys used by this hash. In socket programming, we will have to execute Server. Modules and Packages are closely related to each other and are independent. Package: A Perl package is also known as namespace and which have all unique variables used like hashes, arrays, scalars, and subroutines.

Module: Module is a collection of reusable code, where we write subroutines in it. These modules can be loaded in Perl programs to make use of the subroutines written in those modules. Standard modules will get installed during installation of Perl on any system. Our own customized Perl Modules which can be written by us.

Basically, A module when loaded in any script will export all its global variables and subroutines. These subroutines can directly call as if they were declared in the script itself. Perl Modules can be written with. We can load a Perl module using require or use anywhere in the code. The major difference between require and use is, require loaded module during runtime and use loads during compile time. EXPORT array can be used to pass a list of variables and subroutines which by default will be exported to the caller of the Module.

By default, add subroutine will be exported. In this section, we will learn how to create Perl Object oriented Modules. An object is an instance using which we can access, modify and locate some data in any Perl module.

This is nothing but making your existing Perl package, variables and subroutines to act like class, objects, and methods in reference to other Programming Languages. We already know how to create modules from the previous topic. The purpose of the class is to store methods and variables. A Perl Module will have subroutines which are methods. We need to access those variables and subroutines objects.

A constructor in Perl is a method which will execute and return us a reference with the module name tagged to the reference. This is called as blessing the class. We use a particular variable for blessing a perl class, which is bless. The new method used as a constructor for a class, This constructor will create an object for us and will return to the script which is calling this constructor.

Here, we need to understand how the object created. Whenever we try to create an object for the class, we need to use the full name of the class.

And, if we want to access this perl class from the lib directory then we have to provide the entire path of to the class while calling in the script. How does Perl script know where library module exists? Perl only knows about current directory of the script and the Perl inbuilt library path.

Whenever we use, and Perl module, which is not located in the current directory or Perl library Path, the script will always fail. About INC, this is an array, which holds all directory paths where it has to look for the Perl modules. Try to execute this command and see what will be the output. This will give some output, and that is the path where the lib Modules will be available. Whenever we use any new library module, we need to tell Perl, interpreter, to look into that particular location where Perl module is available.

Make this as your first line of code. This will tell your interpreter to look into that Path. The destructor of an object is by default called at the end and before your script exits. This is used to destroy your objects from memory. Perl is widely used in automation.

It may not be the best programming languages in the world but its best suited for certain types of tasks. This can be done manually, but can a manual tester perform the same times? We have written content into the abc.

This file contains the data. We have overwritten the data of the abc. Opening file abc. We have used the abc. Below is the data present in the abc.

Source File name as abc. In the below example, we have created a file name as xyz. We have taken input from the user while writing data into the file.

Open and create file name as xyz. Perl write to file is used to write content into a file, we can copy content from one file and write into another text file. This is a guide to Perl Write to File.



0コメント

  • 1000 / 1000