Python输出错误

时间:2017-04-02 15:32:54

标签: python curl popen

我正在使用python制作一个简单的脚本来检查一个IP是否在vpn上是否使用主机名,但我使用的是vpn进行测试,我使用的是真正的IP,结果显示“vpn not”在使用中“即使它和输出(”来自curl命令)也是如此。

#!/usr/bin/env python
import os
import subprocess
from subprocess import Popen,PIPE
import socket
import time
import optparse
parser = optparse.OptionParser()
parser.add_option("-t", "--hst", action="store", dest="host", help="Target host to check if is using VPN", default=False)
options, args = parser.parse_args()
host = socket.gethostbyname(options.host)
def hostname_check(hst):
        check = subprocess.Popen(['curl ipinfo.io/'+hst], shell=True)
        if('No Hostname' in str(check)):
                print("Host: %s - Virtual Private Network in use") % host

        elif('No Hostname' not in str(check)):
                print("Host: %s - Virtual Private Network NOT in use") % host
hostname_check(options.host)

执行命令:./ vpnchecker.py --hst 162.224.81.174 输出:

    Host: 162.244.81.174 - Virtual Private Network NOT in use
user@LAPTOP:~$ {
  "ip": "162.244.81.174",
  "hostname": "No Hostname",
  "city": "",
  "region": "",
  "country": "RO",
  "loc": "46.0000,25.0000",
  "org": "AS19624 Data Room, Inc"
}

它应显示为:Host:162.244.81.174 - 正在使用的虚拟专用网络,我不希望curl命令的输出显示在屏幕上。

1 个答案:

答案 0 :(得分:0)

我使用了一个使用请求的不同方法,因此我没有使用curl来提取Web数据,而是从请求lib中获取了相同的数据。以下是任何有兴趣的人的代码:

#!/usr/bin/env python
import os
import subprocess
from subprocess import Popen,PIPE
import socket
import time
import requests
import optparse
parser = optparse.OptionParser()
parser.add_option("-t", "--hst", action="store", dest="host", help="Target host to check if is using VPN", default=False)
from requests import *
options, args = parser.parse_args()
host = socket.gethostbyname(options.host)
def hostname_check(hst):
        check = requests.get('http://ipinfo.io/'+hst).text
        if('No Hostname' in check):
                print("Host: %s - Virtual Private Network in use") % host

        elif('No Hostname' not in check):
                print("Host: %s - Virtual Private Network NOT in use") % host
hostname_check(options.host)