Deleting Specific Lines from a Remote File using SSH in Python
Step 1: Understanding the Goal
Before we start coding, it's essential to understand our goal. We want to:
- Connect to a remote server using SSH.
- Read a file from the server.
- Delete lines containing a specific string.
- Save the modified file back to the server.
Full Code is available at the end of this article.
Step 2: Setting Up
To achieve our goal, we'll use the paramiko
library in Python. If you haven't installed it yet, you can do so using pip:
pip install paramiko
Step 3: Starting with the Basics - The SSH Client
First, let's learn how to establish an SSH connection using paramiko
import paramiko
# Create an SSH client instance
ssh = paramiko.SSHClient()
Here, we've imported the paramiko library and created an SSH client instance.
Step 4: Trusting the Server's Host Key
Before connecting, we need to decide how to handle the server's host key. For simplicity, we'll automatically trust it (though this is insecure for real-world applications).
# Automatically add untrusted hosts (make sure okay for security policy in your environment)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
Step 5: Establishing the Connection
Now, let's connect to the SSH server using our credentials.
# Connect to the server
host = "your_ubuntu_ip"
port = 22 # Default SSH port
username = "your_username"
password = "your_password"
ssh.connect(host, port, username, password)
Replace the placeholders with your server's details.
Step 6: Reading the File
Once connected, we'll use SFTP (Secure File Transfer Protocol) to read our target file.
sftp = ssh.open_sftp()
with sftp.file("/path/to/your/file.txt", 'r') as file:
lines = file.readlines()
Replace "/path/to/your/file.txt" with the path to your target file on the server.
Step 7: Modifying the File Content
Now, let's filter out lines containing a specific string. We'll use a list comprehension for this.
target_string = "CASB"
new_lines = [line for line in lines if target_string not in line]
another version of the same code:
# Remove lines containing the target string (case-insensitive) using a for loop
new_lines = []
for line in lines:
if target_string.lower() not in line.lower():
new_lines.append(line)
Step 8: Writing the Modified Content Back
With our modified content ready, let's write it back to the file on the server.
with sftp.file("/path/to/your/file.txt", 'w') as file:
file.writelines(new_lines)
Step 9: Cleaning Up After our operations, it's a good practice to close the connections.
sftp.close()
ssh.close()
Step 10: Handling Exceptions
In real-world scenarios, things might not always go as planned. It's crucial to handle exceptions to understand any issues that arise.
Wrap the entire code inside a try...except block to catch and print any errors:
try:
# [All the above code]
except Exception as e:
print(f"An error occurred: {e}")
Full Code
import paramiko
def ssh_delete_line_with_string(host, port, username, password, file_path, target_string):
try:
# Create an SSH client instance
ssh = paramiko.SSHClient()
# Automatically add the server's host key (this is insecure and used for demonstration; in a real-world scenario, you'd verify the host key)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect to the SSH server
ssh.connect(host, port, username, password)
# Fetch the file content
sftp = ssh.open_sftp()
with sftp.file(file_path, 'r') as file:
lines = file.readlines()
# Remove lines containing the target string (case-insensitive) using a for loop
new_lines = []
for line in lines:
if target_string.lower() not in line.lower():
new_lines.append(line)
# Write the updated content back to the file
with sftp.file(file_path, 'w') as file:
file.writelines(new_lines)
print(f"Lines containing '{target_string}' (case-insensitive) have been deleted from {file_path}!")
# Close the SFTP and SSH connections
sftp.close()
ssh.close()
except Exception as e:
print(f"An error occurred: {e}")
# Usage
host = "your_target_server_ip"
port = 22 # Default SSH port
username = "your_username"
password = "your_password"
file_path = "/home/nishanth/Desktop/tobedeleted/delete.txt"
target_string = "casb"
ssh_delete_line_with_string(host, port, username, password, file_path, target_string)