简体   繁体   中英

Starting a netcat listener in new terminal window from python script

Total python noob here but trying to learn

I am trying to launch a new terminal window which is running a netcat listener on port 7777 from a python script.

I have tried...

import os

netcat = '--command \"nc -nvlp 7777\"'
print ("Starting listener on port 7777")
from subprocess import call
call(['xfce4-terminal', netcat])

and

from subprocess import call
call(['xfce4-terminal', '--command', '"nc -nvlp 7777"')]

but no dice. The second example will actually launch the terminal and look for a command, but doesn't execute the netcat "argument".

This seems to be caused by the fact that using --command requires my netcat argument to be surrounded by quotes.

Thanks in advance for all help

After consulting a friend, we were able to find a solution that works.

We ended up putting the entire command to call the terminal with netcat into a variable and then passed that variable to the call() like so...

import os

netcat = 'xfce-terminal --command "nc -nvlp 7777"'
print ("Starting listener on port 7777")
from subprocess import call
call(netcat,shell=True)

You'll notice that we also had to use shell=True even though there was a warning against it.

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