Your First Admin Script
Your First Admin Script
In this section we write a simple script that prints a simple welcome banner such as you might see when logging into a server via SSH.
The print() function
Before we can produce a welcome banner, we need to learn how to print text to the terminal. In Python, this is done using the print() function. The print() function takes an argument, which can be a string, number, or other object, and print it to the terminal.
The following script prints the string "Hello World!" to the terminal:
print("Hello World!")The print() function can also take multiple arguments, which will be printed with a space between them, the parameters can be of different types, for example:
# The result of this script will be: Hello Class of 2028
print("Hello", "Class of", 2028)Concept: print()
The print() function is used to display output to the console. It can take multiple arguments, which will be printed with a space in between by default. You can also customize the separator and end character.
sep: Specifies the string that will be used to separate the arguments. Default is a space.end: Specifies the string that will be printed at the end of the output. Default is a newline character.
print("Connecting to server...", end=" ")
print("Success!")
# Output: Connecting to server... Success!
print("Server01", "192.168.1.10", "Online", sep=" | ")
# Output: Server01 | 192.168.1.10 | OnlineOutput can be further customized using escape characters:
| Escape Character | Description |
|---|---|
\n | New line |
\t | Tab |
\\ | Backslash |
The sep and _end parameters can even be used in the same function call, for example:
# Create a terminal prompt with date, time, and username
print("2026", "08", "28", sep="-", end=" ")
print("14:32", "root", end=" $ ", sep=" | ")
# Output: 2026-08-28 14:32 | root $Note that the example above does not specify a name for the first fex parameters, this is because those are positional, while the last two are optional. A positional parameter's function is derived from the position in the function call. In this case, Python knows that the first parameters are strings (text) to be printed. By specifying a name for the last two parameters, Python knows that this is not something to be printed and must instead be processed as a seperator or end character.
Note also that the order of the parameters does not matter, as long as the positional parameters are first.
We can use the function to print a simple welcome banner, for example:
server_name = "CORE-DATABASE-01"
environment = "PRODUCTION"
admin_contact = "admin@thomasmore.be"
warning_msg = "UNAUTHORIZED ACCESS IS STRICTLY PROHIBITED"
print("=" * 60)
print(f"{'SYSTEM ACCESS CONTROL':^60}")
print("=" * 60)
print(f"\nSERVER NAME:\t{server_name}")
print(f"ENVIRONMENT:\t{environment}")
print(f"ADMIN CONTACT:\t{admin_contact}")
print("\n" + "-" * 60)
print(f"{warning_msg:^60}")
print("-" * 60)
print("\nWelcome, Administrator. Please ensure all changes are logged.")
print("=" * 60)Note that we use the ^ operator to center the text in the banner, this is a feature of f-strings that allows us to align text within a specified width. In this case, we specify a width of 60 characters for the banner.
Reading input from the user
A professional script should not hard-code values, but instead read them from the system or the command used to execute the script. Since both of these options are more advanced topics that we'll discuss in a later chapter, we will start with the simplest option: reading input from standard input (stdin) using the input() function.
Concept: The input() function
The input() function is used to get user input in Python. When called, it displays a prompt (if provided) and waits for the user to type something and press Enter. The input is then returned as a string.
user_input = input("Please enter your name: ")
print("Hello, " + user_input + "!")
# Output: Please enter your name: John
# Hello, John!Using the input() function, we can prompt the user for their name and include it in the welcome banner[1]:
username = input("Please enter your username: ")
server_name = "CORE-DATABASE-01"
environment = "PRODUCTION"
admin_contact = "admin@thomasmore.be"
warning_msg = "UNAUTHORIZED ACCESS IS STRICTLY PROHIBITED"
print("=" * 60)
print(f"{'SYSTEM ACCESS CONTROL':^60}")
print("=" * 60)
print(f"\nSERVER NAME:\t{server_name}")
print(f"ENVIRONMENT:\t{environment}")
print(f"ADMIN CONTACT:\t{admin_contact}")
print("\n" + "-" * 60)
print(f"{warning_msg:^60}")
print("-" * 60)
print(f"\nWelcome, {username}. Please ensure all changes are logged.")
print("=" * 60)Comments
As the script grows in size and complexity, it might become harder to understand what the script is doing. To help with this, we can add comments to the code.
As explained above, it is not a good idea to write comments that simply describe what the code is doing, as this can be inferred from the code itself. Since the script above does not contain any complex code that can be misunderstood, the code is self-explanatory and does not require any comments. It is however still a good idea to add comments to separate the different sections of the code.
# --- Username Input ---
username = input("Please enter your username: ")
# --- Configuration ---
server_name = "CORE-DATABASE-01"
environment = "PRODUCTION"
admin_contact = "admin@thomasmore.be"
warning_msg = "UNAUTHORIZED ACCESS IS STRICTLY PROHIBITED"
# --- Banner Execution ---
print("=" * 60)
print(f"{'SYSTEM ACCESS CONTROL':^60}")
print("=" * 60)
print(f"\nSERVER NAME:\t{server_name}")
print(f"ENVIRONMENT:\t{environment}")
print(f"ADMIN CONTACT:\t{admin_contact}")
print("\n" + "-" * 60)
print(f"{warning_msg:^60}")
print("-" * 60)
print(f"\nWelcome, {username}. Please ensure all changes are logged.")
print("=" * 60)Executing a Python script in the terminal
To run a Python script, you can use the python command followed by the name of the script file. For example, if your script is saved in a file called welcome.py, you can run it in the terminal like this:
python welcome.pyIf you're using Linux, you should make sure that the script is executable and has the correct shebang line at the top of the file. The shebang line tells the operating system which command to use to execute the script. This allows the shell (bash, zsh, fish, etc.) to run the script without needing to explicitly call the python command, all you need to do is type the name of the script file in the terminal and hit enter.
The shebang consists of the characters #! followed by the path to the Python interpreter, which is usually /usr/bin/env python3 on Linux systems.
Following this, you need to make the script executable using the chmod command:
chmod +x welcome.pyThe script can now be executed directly from the terminal:
./welcome.pyIt is of course also possible to run the script using the python command, but this is not the most efficient way to do it for production scripts. It's better to treat the script as a standalone program and make it executable (like any other tool on the system).
#!/usr/bin/env python3
# -- Username Input ---
username = input("Please enter your username: ")
# --- Configuration ---
server_name = "CORE-DATABASE-01"
environment = "PRODUCTION"
admin_contact = "admin@thomasmore.be"
warning_msg = "UNAUTHORIZED ACCESS IS STRICTLY PROHIBITED"
# --- Banner Execution ---
print("=" * 60)
# The ^ operator can be used to center text in an f-string.
# In this case text will be centered within 60 characters.
print(f"{'SYSTEM ACCESS CONTROL':^60}")
print("=" * 60)
print(f"\nSERVER NAME:\t{server_name}")
print(f"ENVIRONMENT:\t{environment}")
print(f"ADMIN CONTACT:\t{admin_contact}")
print("\n" + "-" * 60)
print(f"{warning_msg:^60}")
print("-" * 60)
print(f"\nWelcome, {username}. Please ensure all changes are logged.")
print("=" * 60)In a production environment, you would typically use the system's user management and authentication mechanisms to identify the user, rather than asking for their name directly. We'll discuss this in a later chapter. ↩︎
Concept: Comments
While most scripts you write will be relatively short and straightforward, it is still a good idea to include comments in your code. Comments are lines of text that are ignored by the computer when the script is run, but they can be very helpful for anyone reading the code (including yourself) to understand what the code is doing.
It is important to write comments that are clear and concise. Comments that tell the reader what the code is doing are not very helpful as they can be inferred from the code itself. Instead, focus on explaining why the code is doing what it is doing, and only explain what the code is doing if it is not immediately obvious. And even then, it's often preferable to rewrite the code to make it more clear rather than adding a comment.
Additionally, comments can be used to separate sections of code such as configuration, input, and output, or to provide context for a specific block of code.