Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Whois script help

Okay i can get the whois info to out put to the screen but I'm having problems saving to a text file. It isn't saving any of the output in the text file for some reason. Maybe someone could help me

Code
from subprocess import call
import socket

hostname = raw_input("Enter Host Name : ")
IP = socket.gethostbyname(hostname)
output = call(["whois", IP])
print (output)


save= raw_input("Would you like to save to file Y or N")
if save=='Y' or save =='y' or save =='YES' or save=='yes':
    print " Save file as(Ex example.txt)"
    file = raw_input("File Name: ")
    with open(file,'a')as outputs:
    #file_open = open(file,'w')
        outputs.write(str(output))
    #file_open.close
else:
    print "You decided not to save the output"
    pass

Im only having problems saving output to txt file. I know how to save output to text files normally but its not working in this case for some reason.

If no one can help ill just make a webscrapper and do it that way.
Usually you have to specify it as a text file, were you writing it and getting a blank file?

Not sure why you do with open and as output but you'd do:

output=your stuff from before
filename=raw_input("Text file name: ")

outputfile=open(filename+".txt", "w")
outputfile.write(output)

I tried the above with "a" but I wasn't able to write to the text file, when switched to "w" it worked properly. You can then use outputfile.read() to skim whatever you need if you intend on expanding your script to fetch and automate things.

edit: don't name one of your strings "file" either as it's reserved for other things.

I didn't bother to install your modules to test it out but this should work:

Code
from subprocess import call
import socket

hostname = raw_input("Enter Host Name : ")
IP = socket.gethostbyname(hostname)
output = call(["whois", IP])
print (output)

save= raw_input("Would you like to save to file Y or N")
if save=='Y' or save =='y' or save =='YES' or save=='yes':
    print " Save file as(Ex example.txt)"
    filename= raw_input("File Name: ")
    outputfile=open(filename+".txt", "w")
    outputfile.write(output)
    outputfile.close()
else:
    print "You decided not to save the output"
    pass
You realize that there is a Whois library right?

It would make your life so much easier.
(Jun 19, 2015 08:33 PM)selinux Wrote:  You realize that there is a Whois library right?

It would make your life so much easier.

Nope i didn't know that haha ill try that


(Jun 19, 2015 08:19 PM)totenkopf Wrote:  Usually you have to specify it as a text file, were you writing it and getting a blank file?

Not sure why you do with open and as output but you'd do:

output=your stuff from before
filename=raw_input("Text file name: ")

outputfile=open(filename+".txt", "w")
outputfile.write(output)

I tried the above with "a" but I wasn't able to write to the text file, when switched to "w" it worked properly. You can then use outputfile.read() to skim whatever you need if you intend on expanding your script to fetch and automate things.

edit: don't name one of your strings "file" either as it's reserved for other things.

I didn't bother to install your modules to test it out but this should work:

Code
from subprocess import call
import socket

hostname = raw_input("Enter Host Name : ")
IP = socket.gethostbyname(hostname)
output = call(["whois", IP])
print (output)

save= raw_input("Would you like to save to file Y or N")
if save=='Y' or save =='y' or save =='YES' or save=='yes':
    print " Save file as(Ex example.txt)"
    filename= raw_input("File Name: ")
    outputfile=open(filename+".txt", "w")
    outputfile.write(output)
    outputfile.close()
else:
    print "You decided not to save the output"
    pass
Umm ya I'm still getting a blank file for some reason. C can output to a file when i don't use the subprocess module but for some reason the call(["whois", IP]) seems to be preventing me from saving its out put to a file i can only print it to the screen. I think ima just used the whois library instead of trying to re invent the wheel.
I've made a similar program to this.
It uses Whatismyipaddress to get information on an IP in command line.
You can easily reuse it to work with or another whois lookup site.

Replace the [dot] with actual dots...
(Can't post links/etc bullshit)

Enjoy!

Code
#!/usr/bin/env python
# coding=utf-8

# '------'
#   IP Bullshit nice person Fuck
# '------'

#Created by Respire
#Copyright RespireDev © 2015
#github [dot] com/respiredev

from bs4 import BeautifulSoup

import mechanize
import cookielib
import sys

ip = sys.argv[1]

br = mechanize.Browser()
cj = cookielib.LWPCookieJar()

host = '/www [dot] whatismyipaddress [dot] com/ip/' + ip

def getip(br, cj, host):
      br.set_cookiejar(cj)
      br.addheaders = [('User-agent','Firefox')]
      br.set_handle_referer(True)
      br.set_handle_robots(False)
      code = br.open(host)
      html = code.read()
      soup = BeautifulSoup(html)
      table = soup.findAll('table')[0]
      rows = table.findAll('tr')
      for row in rows:
            print row.th.text,
            print row.td.text

def main():
      getip(br, cj, host)

main()
OP, just make your life easy and use some of the many whois python libraries.
ipwhois seems to work well:

Code
from ipwhois import IPWhois
from pprint import pprint

def whois(address, f=False, fname=None):

    if not f:
      pprint(IPWhois(address).lookup())

    elif f:
      pprint(IPWhois(address).lookup())

    with open(fname, 'w') as f:
      f.write(IPWhois(address).lookup())

    else:
      pass
(Jun 19, 2015 10:22 PM)TreaZon Wrote:  -Snip-

You can do that in literally 1 line...

Code
for k, v in (__import__('json').loads((__import__('requests').get('http://www.telize.com/geoip/' + __import__('sys').argv[1]).text))).items(): print '%s: %s' % (k, v)

That's the most retarded script I've seen no offense 😉
(Jun 20, 2015 10:51 AM)selinux Wrote:  You can do that in literally 1 line...

Code
for k, v in (__import__('json').loads((__import__('requests').get('http://www.telize.com/geoip/' + __import__('sys').argv[1]).text))).items(): print '%s: %s' % (k, v)

That's the most retarded script I've seen no offense 😉

Yeah, because geoip is the same thing as whois. That's the most retarded script I've seen no offense 😉
oh wait he's closed he won't read this lol im gay


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by mrkoolaidOct 23, 2016 03:13 AM 3 132 Oct 23, 2016 03:13 AM
Last Post: BahNahNah
[.NET] by SniperWarriorDec 30, 2014 09:29 AM 11 550 Dec 30, 2014 09:29 AM
Last Post: xVipX
[.NET] by Gr0wlitJul 22, 2012 09:15 AM 6 360 Jul 22, 2012 09:15 AM
Last Post: Gr0wlit