Skip to content

Flush file buffer when using stdout - #811

Merged
Frky merged 6 commits into
robertdavidgraham:masterfrom
rafa-br34:master
Apr 23, 2026
Merged

Frky merged 6 commits into
robertdavidgraham:masterfrom
rafa-br34:master

Conversation

@rafa-br34

@rafa-br34 rafa-br34 commented Jan 25, 2025 •

Copy link
Copy Markdown
Contributor

Currently, Masscan does not flush the file buffer when using the -oL - flags. This does not allow other programs (in my case, Python) to easily parse the output stream.

Here's the code I'm using in case anyone wants to reproduce the issue.

import subprocess

def masscan_iterate(path, port, rate=1000):
        process = subprocess.Popen(
                [
                        path,
                        "-oL", "-",
                        "-p", str(port),
                        "--rate", str(rate),
                        "--exclude", "255.255.255.255",
                        "0.0.0.0/0",
                        "--output-flush", # Added by this PR
                ],
                stdout = subprocess.PIPE,
                stderr = subprocess.DEVNULL
        )

        while process.poll() == None:
                line = process.stdout.readline() # Hangs with the current Masscan version, works with the PR

                if line != b'':
                        print(line)

masscan_iterate("./masscan", 22)

@Frky

Frky commented Apr 20, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the contribution. Could you do two small modifications to your PR?

  1. Could you add the same logic for the banner output as well (in output_report_banner)?
  2. In order to avoid unnecessary overhead when not needed (for instance when flushing regularly is not a requirement), could you add a command line option to enable the flush only when necessary?

@rafa-br34

Copy link
Copy Markdown
Contributor Author

Of course! I'll do that tomorrow.

@rafa-br34

rafa-br34 commented Apr 20, 2026 •

Copy link
Copy Markdown
Contributor Author

@Frky
Please let me know if I missed anything or if you would like me to change anything.

@Frky Frky left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, just a few remarks if you don't mind:

  • I suggest to rename the option to --output-flush to match other output options (such as --output-show, etc.)
  • Could you add the option to print_nmap_help? (don't mind the function name)
  • As mentioned in a comment in the review, maybe the check for fp == stdout isn't necessary anymore now that there is a CLI option to flush explicitly?

What do you think?

Comment thread src/main-conf.c Resolved
Comment thread src/output.c Resolved
Also added it to `print_nmap_help`
@rafa-br34

Copy link
Copy Markdown
Contributor Author

Done! Anything else you would like to change?

@Frky

Frky commented Apr 23, 2026

Copy link
Copy Markdown
Collaborator

No, looks good to me. Thank you.

@Frky
Frky merged commit 85fae8d into robertdavidgraham:master Apr 23, 2026
2 checks passed
@rafa-br34

Copy link
Copy Markdown
Contributor Author

You're welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants