Python - How to take input from command line and pipe it into socket.gethostbyaddr("")

1.5k views Asked by At

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

4

There are 4 answers

0
Angus On

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.

0
janbrohl On

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

2
holdenweb On

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'])
0
zwol On

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.