ssh_file_insert_lines_after_match
Insert one or more lines into a remote file via SSH after an exact matching line. Fails if the match is not unique, ensuring safe edits.
Instructions
Insert one or more new lines immediately after a matching line. match_line
must match EXACTLY ONE line in the file (whitespace-trimmed, literal text - not
a pattern); if it matches zero lines or more than one, the operation fails with
a descriptive error rather than guessing. Use ssh_file_find_lines_with_pattern
first if you're not sure the line is unique.
PARAMETERS:
file_path: Path to the file to modify
match_line: Exact line content to match (whitespace-trimmed)
lines_to_insert: List of lines to insert after the match
To insert multiple lines: use ["first line", "second line", ...]
To insert a single line: use ["line to insert"]
To insert an empty line: use [""]
use_sudo: Use sudo for the operation (default: false)
force: No longer needed (kept for compatibility) - use_sudo reads root-only files with sudo (default: false)
RETURNS:
On success: {'success': True, 'lines_inserted': <len(lines_to_insert)>} (note:
the key is lines_inserted here, vs. lines_written on
ssh_file_replace_line/ssh_file_replace_line_multi). On failure (match not found,
match not unique, file not found/unreadable): {'success': False, 'error': str}
not a raised exception. Does NOT return
file_pathin the response.
EXAMPLES: Example 1: Insert configuration lines after a marker
{
"file_path": "/etc/nginx/nginx.conf",
"match_line": "http {",
"lines_to_insert": [" server_tokens off;", " client_max_body_size 20M;"]
}Example 2: Add a new host entry after localhost
{
"file_path": "/etc/hosts",
"match_line": "127.0.0.1 localhost",
"lines_to_insert": ["192.168.1.10 myserver.local"]
}Long-running calls: if this takes longer than the server's wait cap (default 50s), it
returns {"status": "in_progress", "handle_id": ...} instead of the result - the work keeps
running. Get the result by polling ssh_cmd_check_status(handle_id=...), which returns this
tool's normal response once finished. Do not call it again meanwhile. Only one such
operation runs at a time; another one started meanwhile fails with a 'busy' error.Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| force | No | No longer needed (kept for compatibility): with use_sudo=true the file is read with sudo when the user can't read it. If it can't be read at all, the edit fails - it never reports success without having seen the file. | |
| use_sudo | No | Use sudo for the operation | |
| file_path | Yes | Path to the file to modify | |
| match_line | Yes | Exact line content to match | |
| lines_to_insert | Yes | Line(s) to insert after the match |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||