Home  >  Article  >  Backend Development  >  How to use Python scripts for Linux command line operations

How to use Python scripts for Linux command line operations

WBOY
WBOYOriginal
2023-10-05 10:24:301111browse

How to use Python scripts for Linux command line operations

How to use Python scripts for Linux command line operations requires specific code examples

[Introduction]
In daily work, we often need to use the Linux command line To complete various tasks, such as file operations, system management, software installation, etc. As a powerful programming language, Python can also simplify these command line operations by writing scripts.

This article will introduce how to use Python scripts to perform Linux command line operations, and provide specific code examples to help readers better understand and apply.

[1. Use the os module to execute commands]
In Python, you can use the built-in os module to execute Linux commands. Below is a simple example showing how to create a new directory using Python.

import os

dirname = "new_directory"
os.system("mkdir {}".format(dirname))

In the above example, we use the os.system() function to execute the mkdir command and pass the format() method Pass the directory name to the command dynamically.

Similarly, we can use os.system() to execute other Linux commands, such as deleting directories, copying files, etc. Just pass the corresponding command as a string to os.system().

[2. Use subprocess module to obtain command output]
In addition to executing commands, sometimes we also need to obtain the output results of commands. At this time, you can use Python's subprocess module. Below is an example showing how to use the subprocess module to get the output of a Linux command.

import subprocess

command = "ls -l"
output = subprocess.check_output(command, shell=True)
print(output.decode())

In the above example, we use the subprocess.check_output() function to execute the ls -l command and assign the output of the command to the variableoutput. Convert the byte type output result to a string through the decode() method, and use the print() function to output the result.

[3. Use sh module to simplify command line operations]
In addition to the os module and subprocess module, you can also use the third-party module sh to simplify command line operations. The sh module provides simpler syntax and more functions.

The following is an example that demonstrates how to use the sh module to execute Linux commands.

from sh import mkdir, ls

# 创建新目录
dirname = "new_directory"
mkdir(dirname)

# 列出当前目录下的文件
files = ls()
print(files)

In the above example, we use the mkdir() function in the sh module to create a new directory and the ls() function to list the current directory document. As you can see, using the sh module simplifies the code for command line operations.

[4. Use the paramiko module to execute commands remotely]
If you need to execute commands on a remote server, you can use Python's paramiko module. paramiko provides the functionality of an SSH client to interact with remote servers.

The following is an example that demonstrates how to use the paramiko module to remotely execute Linux commands.

import paramiko

# 远程服务器信息
hostname = "remote_server"
username = "username"
password = "password"

# 创建SSH客户端
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, username=username, password=password)

# 执行命令
stdin, stdout, stderr = client.exec_command("ls -l")
output = stdout.read().decode()
print(output)

# 关闭SSH连接
client.close()

In the above example, we first created an SSH client, and then used the exec_command() method to execute the remote ls -l command, and Get the output of the command through the read() method.

[Conclusion]
Python scripts can help us simplify and automate Linux command line operations. This article briefly introduces the method of using the os module, subprocess module, sh module and paramiko module to execute and manage Linux commands, and provides corresponding code examples. Readers can choose a method that suits them according to their specific needs to perform command line operations.

The above is the detailed content of How to use Python scripts for Linux command line operations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn