0

I have been scouring the internet looking for the answer to this. Please not my python coding skills are not all that great. I am trying to create a command line script that will take the input from the command line like this:

$python GetHostID.py serverName.com   

the last part is what I am wanting to pass on as a variable to socket.gethostbyaddr("") module. this is the code that I have so far. can someone help me figure out how to put that variable into the (" "). I think the "" is creating problems with using a simple variable name as it is trying to treat it as a string of text as appose to a variable name. here is the code I have in my script:

#!/bin/python
# 
import sys, os
import optparse
import socket


remoteServer = input("Enter a remote host to scan: ")
remoteServerIP  = socket.gethostbyaddr(remoteServer)
socket.gethostbyaddr('remoteServer')[0]
os.getenv('remoteServer')
print (remoteServerIP)

any help would be welcome. I have been racking my brain over this... thanks

holdenweb
  • 33,305
  • 7
  • 57
  • 77
betzelel
  • 1
  • 2
  • 1
    Why do you think you need to put `remoteServer` in quotes at all? – zwol Aug 01 '16 at 16:10
  • when I do it with out the quotes it seems to error out. File "C:\Bin\Scripts\PyTools\tiniGetHost.py", line 10, in socket.gethostbyaddr('remoteServer')[0] socket.gaierror: [Errno 11004] getaddrinfo failed – betzelel Aug 01 '16 at 16:25
  • i removed the quotes, it ran successfully in the python shell, but when i run it under the bash shell, it seems to not notice that i added the server name, and then errors out... ➤ python tiniGetHost.py serverName Enter a remote host to scan: serverName # had to take real name for posting.... # Traceback (most recent call last): File "tiniGetHost.py", line 8, in remoteServer = input("Enter a remote host to scan: ") File "", line 1, in NameError: name 'serv' is not defined – betzelel Aug 01 '16 at 16:30
  • I think you've tripped over one of the really nasty gotchas in the Python 2 standard library. [You almost always want `raw_input`, not `input`.](https://www.smallsurething.com/the-difference-between-input-and-raw_input-in-python/) Or `sys.stdin.readline`. Or, for this particular program, `sys.argv` is even better. – zwol Aug 01 '16 at 16:35
  • oh ok, ya in the direct python shell i am using 3.5.2, but in the MobaXterm version i am using 2.7 (i think...) that would explain why in the bash shell it is having a hard time running then. – betzelel Aug 01 '16 at 16:38

4 Answers4

1

The command line arguments are available as the list sys.argv, whose first element is the path to the program. There are a number of libraries you can use (argparse, optparse, etc.) to analyse the command line, but for your simple application you could do something like this:

import sys
import sys, os
import optparse
import socket
remoteServer = sys.argv[1]
remoteServerIP = socket.gethostbyaddr(remoteServer)
print (remoteServerIP)

Running this program with the command line

$ python GetHostID.py holdenweb.com

gives the output

('web105.webfaction.com', [], ['108.59.9.144'])
holdenweb
  • 33,305
  • 7
  • 57
  • 77
  • awesome, thank you so much! i had another question related to this. for the socket module, is there a function that can be used to tell what remote OS the server is running? i am in a situation where i am running behind a heavely firewalled / security locked down env, so i am not able to install python modules such as scapy... so i am having to rely on what i am able to code with the default python 3.5 package. any ideas would be great. – betzelel Aug 01 '16 at 17:21
  • Not an easy problem, and I am personally not aware of a library that ,might solve it. Most such "client fingerprinting" relies of arcane knowledge about the differences between TCP/IP stacks and subtle differences between their responses to particular probes. You might do some reading around the `nmap` utility to get an idea of what's possible, but aggressive client fingerprinting isn't a simple or a common task. – holdenweb Aug 02 '16 at 07:43
0

os.getenv('remoteserver') does not use the variable remoteserver as an argument. Instead it uses a string 'remoteserver'.

Also, are you trying to take input as a command line argument? Or are you trying to take it as user input? Your problem description and implementation differ here. The easiest way would be to run your script using

python GetHostID.py

and then in your code include

remoteServer = raw_input().strip().split()

to get the input you want for remoteserver.

Angus
  • 349
  • 3
  • 10
0

you can use sys.argv

for

$python GetHostID.py serverName.com  

sys.argv would be

['GetHostID.py', 'serverName.com'] 

but for being friendly to the user have a look at the argparse Tutorial

janbrohl
  • 2,626
  • 1
  • 17
  • 15
0

In Python 2, input reads text and evaluates it as a Python expression in the current context. This is almost never what you want; you want raw_input instead. However, in Python 3, input does what raw_input did in version 2, and raw_input is not available.

So, if you need your code to work in both Python 2 and 3, you should do something like this after your imports block:

# Apply Python 3 semantics to input() if running under v2.
try:
    input = raw_input
    def raw_input(*a, **k):
        raise NameError('use input()')
except NameError:
    pass

This has no effect in Python 3, but in v2 it replaces the stock input with raw_input, and raw_input with a function that always throws an exception (so you notice if you accidentally use raw_input).

If you find yourself needing to smooth over lots of differences between v2 and v3, the python-future library will probably make your life easier.

zwol
  • 135,547
  • 38
  • 252
  • 361