Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python in Kali Linux
#1
Hi, I'm new to this forum so apologies if this is in the wrong part of the forum.

I'm using Kali Linux and am running it on VirtualBox. I've created a python file and saved it in a folder called bin and think it is in the home directory.
This is the code I've used for the python file:

#!/bin/python import sys, os
if len(sys.argv) == 2 :
os.system("nmap -n -sn -oG - " + sys.argv[1] + "|head -n -1|tail -n +2|cut -d' ' -f2")
exit()

I'm trying to get the file to run via the terminal window but am unsure which code to use for this in the terminal window.
This is the current line I'm trying out in the terminal window to run the .py file:
chmod o+x ~/home/bin/listip.py

Has anyone got any suggestions why this won't work? Alternatively if a similar question has been posted can someone direct me to that?


Thanks
Reply
#2
(Nov-13-2019, 12:50 AM)coolcassie Wrote: I'm trying to get the file to run via the terminal window but am unsure which code to use for this in the terminal window.
First you should test Python version that comes with Kali Linux and pip(pip3 -V).
Example:
tom@tom-VirtualBox:~$ python -V
Python 2.7.15rc1
 
tom@tom-VirtualBox:~$ python3 -V
Python 3.6.5 
So you should by using Python 3,then use python3 my_code.py to run from command line.

Should not be using os.system() anymore,use subprocess it's more powerful and safer.
A example run command and catch output.
# n_map.py
import subprocess

ip = '198.18.32.1'
out = subprocess.run(['nmap', '-n', '-sn', '-oG', '-', ip], capture_output=True)
print(out.stdout.decode())
Output:
# Nmap 7.12 scan initiated Wed Nov 13 02:57:35 2019 as: nmap -n -sn -oG - 198.18.32.1 Host: 198.18.32.1 () Status: Up # Nmap done at Wed Nov 13 02:57:35 2019 -- 1 IP address (1 host up) scanned in 0.30 second
So if run code over for you it could be python3 n_map.py.
This is the normal way to run code,there is no need to chmod o+x(Make the script as executable) every time to run.
Reply
#3
(Nov-13-2019, 02:21 AM)snippsat Wrote:
(Nov-13-2019, 12:50 AM)coolcassie Wrote: I'm trying to get the file to run via the terminal window but am unsure which code to use for this in the terminal window.
First you should test Python version that comes with Kali Linux and pip(pip3 -V).
Example:
tom@tom-VirtualBox:~$ python -V
Python 2.7.15rc1
 
tom@tom-VirtualBox:~$ python3 -V
Python 3.6.5 
So you should by using Python 3,then use python3 my_code.py to run from command line.

Should not be using os.system() anymore,use subprocess it's more powerful and safer.
A example run command and catch output.
# n_map.py
import subprocess

ip = '198.18.32.1'
out = subprocess.run(['nmap', '-n', '-sn', '-oG', '-', ip], capture_output=True)
print(out.stdout.decode())
Output:
# Nmap 7.12 scan initiated Wed Nov 13 02:57:35 2019 as: nmap -n -sn -oG - 198.18.32.1 Host: 198.18.32.1 () Status: Up # Nmap done at Wed Nov 13 02:57:35 2019 -- 1 IP address (1 host up) scanned in 0.30 second
So if run code over for you it could be python3 n_map.py.
This is the normal way to run code,there is no need to chmod o+x(Make the script as executable) every time to run.

I only just saw your reply but will definitely try this out and let you know how it goes.
Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 710 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  How to use a variable in linux command in python code? ilknurg 2 1,584 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  Python syntax in Linux St0rmcr0w 2 49,964 Jul-29-2021, 01:40 PM
Last Post: snippsat
  Login to NordVPN on Linux with python script AGreenPig 2 5,947 Feb-09-2021, 10:44 AM
Last Post: AGreenPig
  how to run linux command with multi pipes by python !! evilcode1 2 6,303 Jan-25-2021, 11:19 AM
Last Post: DeaD_EyE
  where to get portable Python for Linux (Fedora)? python001 5 6,275 Nov-01-2020, 05:23 PM
Last Post: Larz60+
  Python in Linux environment on RPI kendias 22 11,041 Sep-05-2020, 03:04 AM
Last Post: K_Research
  How do I pick the right python in Linux env? MDRI 9 3,634 Jun-27-2020, 05:40 PM
Last Post: snippsat
  control a linux program with python Fifoux082 9 4,073 May-08-2020, 04:24 PM
Last Post: Fifoux082
  Python version on Linux whois1230 5 3,461 Apr-10-2020, 07:12 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020