You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python 3.6编写执行whois命令的用户输入脚本并解决运行问题?

Fixing Your Python Script & General Guide to Running Terminal Commands with Python 3.6

Hey there! Let's tackle your two questions—first fixing that unresponsive gnome-terminal issue in your script, then going over the general approach to getting user input and running terminal commands in Python 3.6.

Why Your Current Script Isn't Working

Let's break down the problems in your code:

  • Broken gnome-terminal command: The line os.system("gnome-terminal -e 'bash -c \'") has messed-up quote escaping and an incomplete command. The bash -c expects a full command string, but you've left it unclosed. Also, modern GNOME Terminal versions have replaced the -e flag with --command (or just -- for short).
  • Wrong order of operations: You're trying to open the terminal before getting the user's target URL. That means even if the terminal opened, it wouldn't have the input needed to run whois.
  • Security risk: Directly concatenating user input into os.system("whois " + w) leaves you open to command injection attacks (e.g., if someone enters example.com; rm -rf ~, you'll have a bad time).

Fixed Script for Your Linux Whois Task

Here's a corrected, safer version that does exactly what you want: gets a URL input, then opens a gnome-terminal to run the whois command (and keeps the terminal open afterward so you can read the output):

import subprocess

# Clear the current terminal (optional)
subprocess.run(["clear"])

# Get user input safely
target_url = input("Who is your target? ").strip()

# Make sure the input isn't empty
if target_url:
    # Open gnome-terminal and run whois, then keep the terminal open
    subprocess.run([
        "gnome-terminal",
        "--command",
        f"bash -c 'whois {target_url}; exec bash'"
    ])
else:
    print("Please enter a valid target URL!")

Key Improvements:

  • Using subprocess.run(): This is the recommended way to run terminal commands in Python (instead of os.system) because it's more flexible and secure.
  • Proper gnome-terminal syntax: The --command flag tells the terminal what to run, and exec bash ensures the terminal stays open after whois finishes.
  • Basic input validation: Checks that the user didn't just enter an empty string.
  • Safer input handling: Adding .strip() removes extra whitespace—for even more security, you could sanitize the input to only allow valid URL characters.

General Guide: Get User Input & Run Terminal Commands in Python 3.6

If you want a more general approach (not just for whois/gnome-terminal), follow these steps:

  1. Get user input: Use input() to capture user input, and clean it with .strip() to remove extra spaces.
  2. Choose the right command method:
    • Use subprocess.run() for most cases (it returns a CompletedProcess object with exit codes and output if you want it).
    • Avoid os.system() unless you have a specific reason—it's less secure and gives you less control over the command's execution.
  3. Handle command arguments safely:
    • Instead of concatenating strings (like whois " + w), pass arguments as a list to subprocess.run() to avoid command injection. For example:
      subprocess.run(["whois", target_url])
      
    • This way, even if the user enters malicious characters, they'll be treated as part of the whois argument, not as a separate terminal command.

Example: Run a Command in the Current Terminal

If you don't need a new gnome-terminal and just want to run whois in the same terminal where you're running the script:

import subprocess

target_url = input("Who is your target? ").strip()

if target_url:
    # Run whois in the current terminal and print the output
    result = subprocess.run(["whois", target_url], capture_output=True, text=True)
    print(result.stdout)
    # If there's an error, print that too
    if result.stderr:
        print("Error:", result.stderr)
else:
    print("Please enter a valid target URL!")

This captures the output of whois and prints it directly in your current terminal (like IDLE's shell).

内容的提问来源于stack exchange,提问作者Jack Christie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:03:22