Reputation: 3222
I'm very new to python. I'm trying to get the whois status from a certain domain trough a script. I'm using whois (https://technet.microsoft.com/en-us/sysinternals/whois) from Microsoft.
I need to run a simple command to retrieve the status
whois stackoverflow.com
With this I will get an output that looks like this:
L:\Programming>hello.py
Starting...
Whois v1.12 - Domain information lookup utility
Sysinternals - www.sysinternals.com
Copyright (C) 2005-2014 Mark Russinovich
Connecting to COM.whois-servers.net...
Connecting to whois.name.com...
Domain ID: 108907621_DOMAIN_COM-VRSN
Registrar WHOIS Server: whois.name.com
Registrar URL: http://www.name.com
Updated Date: 2014-05-09T17:51:17-06:00Z
Creation Date: 2003-12-26T19:18:07-07:00Z
Registrar Registration Expiration Date: 2015-12-26T19:18:07-07:00Z
Registrar: Name.com, Inc.
Registrar IANA ID: 625
Reseller:
Domain Status: clientTransferProhibited
Registry Registrant ID:
Registrant Name: Sysadmin Team
Registrant Organization: Stack Exchange, Inc.
Registrant Street: 1 Exchange Plaza , Floor 26
Registrant City: New York
Registrant State/Province: NY
Registrant Postal Code: 10006
Registrant Country: US
Registrant Phone: +1.2122328280
Registrant Email: [email protected]
Registry Admin ID:
Admin Name: Sysadmin Team
Admin Organization: Stack Exchange, Inc.
Admin Street: 1 Exchange Plaza , Floor 26
Admin City: New York
Admin State/Province: NY
Admin Postal Code: 10006
Admin Country: US
Admin Phone: +1.2122328280
Admin Email: [email protected]
Registry Tech ID:
Tech Name: Sysadmin Team
Tech Organization: Stack Exchange, Inc.
Tech Street: 1 Exchange Plaza , Floor 26
Tech City: New York
Tech State/Province: NY
Tech Postal Code: 10006
Tech Country: US
Tech Phone: +1.2122328280
Tech Email: [email protected]
Name Server: cf-dns02.stackoverflow.com
Name Server: cf-dns01.stackoverflow.com
DNSSEC: Unsigned Delegation
Registrar Abuse Contact Email: [email protected]
Registrar Abuse Contact Phone: +1.17203101849
URL of the ICANN WHOIS Data Problem Reporting System: http://wdprs.internic.net/
>>> Last update of WHOIS database: 2015-11-08T04:07:29-07:00 <<<
The Data in the Name.com, Inc. WHOIS database is provided by Name.com, Inc. for
information purposes, and to assist persons in obtaining information about or re
lated to a domain name registration record. Name.com, Inc. does not guarantee i
ts accuracy. By submitting a WHOIS query, you agree that you will use this Data
only for lawful purposes and that, under no circumstances will you use this Dat
a to: (1) allow, enable, or otherwise support the transmission of mass unsolici
ted, commercial advertising or solicitations via e-mail (spam); or (2) enable hi
gh volume, automated, electronic processes that apply to Name.com, Inc. (or its
systems). Name.com, Inc. reserves the right to modify these terms at any time.
By submitting this query, you agree to abide by this policy.
Found!
What I want to achieve is to retrieve the expiration data from that domain, and put it a file. I can find if "Registrar Registration Expiration Date:" exists, but how do I retrieve the date after the "date:" ? I hope you can help me. This is what I got sofar
print("Starting...")
import subprocess
findStatus = "Registrar Registration Expiration Date:"
whoOutput = subprocess.Popen(["whois", "stackoverflow.com"],shell=True,stdout=subprocess.PIPE).stdout.read().decode("utf-8")
print(whoOutput)
if findStatus in whoOutput:
print("\nFound!")
else:
print("\nNot found")
Upvotes: 0
Views: 188
Reputation: 22282
Use regex:
import re
re.search('Registrar Registration Expiration Date: (.*)', whoOutput)
Without regex:
[i.replace('Registrar Registration Expiration Date: ', '') for i in whoOutput.splitlines() if 'Registrar Registration Expiration Date' in i][0]
or more simple:
[i.split(': ')[1] for i in whoOutput.splitlines() if 'Registrar Registration Expiration Date' in i][0]
Upvotes: 1
Reputation: 11730
What you want to is to invoke whois, iterate through the results, scan each line with regex and write to a file when you find a match. Something like this:
from subprocess import check_output
import re
with open('so-expiration.txt', 'wt') as fout:
for line in check_output(["whois", "stackoverflow.com"]).split('\n'):
if re.match(r'Registrar Registration Expiration Date', line):
fout.write(line)
Upvotes: 0