Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python Automation to get logs with one click

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to use Python automation to get logs with one click". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Preface

For most people, solving Bug depends on keywords to log to locate the problem!

In the case of debugging, we can view the logs in the console in real time, but for applications deployed to the server, the logs are stored in a directory on the server and cannot be seen locally.

In this case, we need to log in to the server, then go to the log directory folder, and finally use the log file to locate the problem. If the K8s container is involved, we may also need to use the kubectl command to enter the corresponding container of the service and log directory before we can begin to locate the problem, which is very tedious and inefficient.

2. Pexpect introduction

Pexpect is an implementation of the class Expect implemented by the Python language. By generating and controlling the sub-applications, the output of the sub-applications is processed by the expected pattern.

Project address:

Https://github.com/pexpect/pexpect/tree/5eed1a31a2853a09b7367c59fbd1a4a0b53341df

It mainly simulates man-machine conversation to realize some automated scenarios.

For example, it is used to automatically interact with command line programs such as SSH, FTP, PASSWD, Telnet, etc.

Before using it, let's install the dependency package

# install dependent library pip3 install pexpect

3. Let's have a real fight.

Let's take getting logs from the server K8S container as an example.

3-1 SSH login

First, initialize using pexpect to simulate the SSH remote login server

Import pexpect PROMPT = ['#','>','>' '\ $'] def login_with_pexpect (): "" Login-pexpect: return: "# Port number port = * * # username user = * * # password pwd = * * # ip address ip = * * ssh_cmd =" ssh-p {} @ {} ".format (port,user Ip) # specify login command Get the program operation handle child = pexpect.spawn (ssh_cmd, timeout=60, encoding='utf-8') # prompt for password characters appear, otherwise timeout ret = child.expect ([pexpect.TIMEOUT,'[P | p] assword:'], timeout=10) # match is successful, enter the password Execute login operation if ret = = 1: # send password child.sendline (pwd) child.expect (PROMPT) return child else: print ('login failed!')

Among them

Pexpect.spawn (): used to execute a program and return an operation handle

The three common parameters of this method are as follows:

The first parameter is the command to be executed

The second parameter specifies the timeout, and the output check after the program is executed will throw an exception if the result does not match within the specified time.

The third parameter is used to set the encoding format

Child.expect (): keyword matching using regular expressions for output results

The meaning in the code is to wait for the keyword'[P | p] assword:'to appear within 10s

Finally, if the matching result is 1, use the sendline () method to send a string with a carriage return, simulate the input of the password, and complete the login operation.

3-2 encapsulated send command

Next, we will encapsulate the operation handle by sending the command and get the returned result.

Def send_command (child, cmd, expected_content=None, timeout=10): "" send a command And print the result: param expected_content:: param child:: param cmd:: return: "" # send a command if expected_content is None: expected_content = ["#"] child.sendline (cmd) # expect a command line prompt character to appear child.expect (expected_content Timeout=timeout) # output all the previous content result = child.before return result

It should be pointed out that child.before is used to get all the data already in the cache until the keyword is matched.

3-3 enter the container pod log directory

Using the above method, enter the corresponding container log directory through kubectl

# Log in to pod container exec_enter_pod = 'kubectl-n% s exec-it% s bash'% (env, pod_name) # enter container send_command (child, exec_enter_pod) # and log directory send_command (child, "cd logs", expected_content='tomcat/logs#')

3-4 get log content

Finally, we just need to form the command to get the log.

For example, use the grep/tail command to form a command to extract log files

Then use the handle object to send this command

Finally, clean the returned content.

This is the end of the content of "how to use Python Automation to get logs with one click". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report