-1

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: enter image description here

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

  • 1
    Have you tested your commandline function without the python wrapper? Because there seems to be hardly anything in the python part that can go wrong. – Kraay89 Dec 24 '20 at 12:42
  • Is there a specific reason, why you run nmap as sudo? It could be, that the process asks for a sudo password and therefore doesn't complete – NationBoneless Dec 24 '20 at 15:10
  • I've tested your script without the 'sed' part and it works correctly. – SergeiMinaev Dec 24 '20 at 18:58
  • i tried but it had the same result @NationBoneless – Erfan jafari Dec 24 '20 at 21:31
  • yes it works but i need that part. because i just need the ip address in result @SergeiMinaev – Erfan jafari Dec 24 '20 at 21:32
  • What are you using the sed command for? I can't make sense of your command. You are using nmap on the specific port 5433, with an old version of no ping (newer would be -Pn instead of -P0), outputting it in greppable format, with a TCP SYN Scan. Then you are using sed and trying to replace something in your lines, but I can't figure out what – NationBoneless Dec 25 '20 at 13:08
  • This does not appear to be a python problem at all. You might get better help tagging this question differently, or even asking on a different stackexchange site such as serverfault or superuser. If you want to treat this as a python problem, do the processing and writing in python instead of sed/sh. – MisterMiyagi Dec 25 '20 at 13:20

1 Answers1

0

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

NationBoneless
  • 308
  • 2
  • 12