简体   繁体   中英

why python output doesn't save correctly

i'm trying to save output of a python code that actually it has written with os.system

import os
os.system("sudo nmap -p5433 -P0 -oG - -sS 127.0.0.1 | \
    sed -n 's/.* \([0-9\.]\{7,\}\).*\/open\/.*/\1/p' > result.txt")

As you can see at the end of a line,output should be save in "result.txt" and i'm sure the output should be an ip (127.0.0.1) but the output is something like this: 在此处输入图像描述

the output is a symbol or something like that,is there any way that i can save the output of this code correctly?

If I understood you correctly, you want to save only hosts, that are found in your nmap scan and have status up?

If so you could use:

sudo nmap -p5433 -P0 -oG - -sS 127.0.0.1 | grep 'Up' | grep -oP '\d*\.\d*\.\d*\.\d* > result.txt'

You use nmap, then you grep all lines, containing the Up status, then you only grep the ip-addresses and put them in the text file

If you just want all ip, addresses that return in the scan (without status Up check) you can simplify:

sudo nmap -p5433 -P0 -oG - -sS 127.0.0.1 | grep -oP '\d*\.\d*\.\d*\.\d*' > result.txt

Although there will be duplicates if nmap prints something like:

# Nmap 7.80 scan initiated Fri Dec 25 14:05:05 2020 as: nmap -p5433 -P0 -oG - -sS 127.0.0.1
Host: 127.0.0.1 (localhost) Status: Up
Host: 127.0.0.1 (localhost) Ports: 5433/closed/tcp//pyrrho///

As there are two lines with the same ip address

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM