我正在研究一个python脚本,它是一个多工具,用于在企业环境中的服务器上获取DNS信息。到目前为止我的脚本使用的是python 3.5。我正在使用argparse创建命令行选项,我试图创建一个包含不同选择的if / elif / else语句。我得到的主要错误信息是:
./GetHostName.py
Traceback (most recent call last):
File "./GetHostName.py", line 34, in <module>
remoteServer = sys.argv[1]
IndexError: list index out of range
这是命令自己运行的时候。 当它以主机名结尾运行时./GetHostName.py hostName 它给出了这样的信息:
GetHostName.py: error: unrecognized arguments: hostName
我没有为安全问题提供真正的服务器名称.... 当我使用argparse选项说像-f选项获取FQDN时,它会给出这个响应......
./GetHostName.py -f hostName
3
-f
从它出现的方式来看,它将-f作为服务器名称的输入,而它应该只是argparse输入的输入。我已经尝试了一切我能想到的解决方法。我已经将主代码主体包含在主函数中,但是没有用,所以我删除了它。我使用try:语句和异常语句。那没用。我想知道此时我的编程逻辑是否存在基本上错误的东西...
这是脚本中的代码:
#!C:\Bin\Python35\python.exe
#
# import libraries
import sys, os
import argparse as ap
import socket
# Command Line interface setup
def argParse():
#Command Line arg parse
parser=ap.ArgumentParser(description='A tool to get a remote servers DNS information.')
parser.add_argument("-a", "--address", default="fqdn", help="Gets IP address from host name.")
parser.add_argument("-f", "--fqdn", default="fqdn", help="Gets the FQDN address of server.")
parser.add_argument("-d", "--addrinfo", default="fqdn", help="Gets the FQDN address of server.")
parser.add_argument("-l", "--local", default="fqdn", help="Gets info on local host.")
parser.add_argument("-Pr", "--proto", default="fqdn", help="Translate an Internet protocol name to a constant suitable for passing as the (optional) third argument to the socket() function.")
parser.add_argument("-n", "--nameinfo", default="fqdn", help="Gets name and port on remote host.")
parser.add_argument("-Sn", "--servbyname", default="fqdn", help="Translate an Internet service name and protocol name to a port number for that service.")
parser.add_argument("-Sp", "--servbyport", default="fqdn", help="Translate an Internet port number and protocol name to a service name for that service.")
parser.add_argument("-t", "--timeout", default="fqdn", help="Return the default timeout in seconds for new socket objects.")
parser.add_argument("-v", "--verbose", default="fqdn", help="Increase output verbosity")
return parser.parse_args()
#remoteServer = input().strip().split()
args=argParse()
if args.fqdn:
remoteServer = sys.argv[1]
print (len(sys.argv))
remoteServerIP = socket.getfqdn(remoteServer)
print (remoteServerIP)
elif args.address:
remoteServer = sys.argv[2]
print (len(sys.argv))
remoteServerIP = socket.gethostbyname(remoteServer)
print (remoteServerIP)
elif args.addrinfo:
remoteServer = sys.argv[3]
print (len(sys.argv))
remoteServerIP = socket.getaddrinfo(remoteServer)
print (remoteServerIP)
elif args.local:
remoteServer = sys.argv[4]
print (len(sys.argv))
remoteServerIP = socket.gethostname()
print (remoteServerIP)
elif args.proto:
remoteServer = sys.argv[5]
print (len(sys.argv))
remoteServerIP = socket.getprotobyname(remoteServer)
print (remoteServerIP)
elif args.servbyname:
remoteServer = sys.argv[6]
print (len(sys.argv))
remoteServerIP = socket.getservbyname(remoteServer)
print (remoteServerIP)
elif args.servbyport:
remoteServer = sys.argv[7]
print (len(sys.argv))
remoteServerIP = socket.getservbyport(remoteServer)
print (remoteServerIP)
elif args.timeout:
remoteServer = sys.argv[8]
print (len(sys.argv))
remoteServerIP = socket.getdefaulttimeout(remoteServer)
print (remoteServerIP)
elif args.verbose:
remoteServer = sys.argv[9]
print (len(sys.argv))
remoteServerIP = socket.gethostbyaddr(remoteServer)
print (remoteServerIP)
else:
args.nameinfo
remoteServer = sys.argv[10]
print (len(sys.argv))
remoteServerIP = socket.getnameinfo(remoteServer)
print (remoteServerIP)
任何帮助将不胜感激。请注意,当我在其中运行一个脚本时,它可以正常工作:
#!C:\Bin\Python35\python.exe
#
import sys, os
import argparse
import socket
# Command Line interface setup
def main():
remoteServer = sys.argv[1]
remoteServerIP = socket.gethostbyaddr(remoteServer)
print (remoteServerIP)
if __name__ == '__main__':
main()
提前谢谢。
-Betzelel
P.S。由于必须复制并粘贴到此博客中,并且手动在每行上放置4个空格以使其显示为代码lol,因此代码可能看起来格式不合适。
答案 0 :(得分:0)
我很久以前就遇到过这个问题。为了解决这个问题,我已经改变了
parser.add_argument("-a", "--address", default="fqdn", help="Gets IP address from host name.")
在
parser.add_argument("-a", "--address", default="fqdn", help="Gets IP address from host name.", dest=ipfromhostname)
因此,要从-a
获取值,您必须更改
remoteServer = sys.argv[1]
在
remoteServer = args.ipfromhostname
其中ipfromhostname
是dest
值。
编辑:
您必须为每个parser.add_argument