如何用Python 3.6编写执行whois命令的用户输入脚本并解决运行问题?
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. Thebash -cexpects a full command string, but you've left it unclosed. Also, modern GNOME Terminal versions have replaced the-eflag 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 entersexample.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 ofos.system) because it's more flexible and secure. - Proper gnome-terminal syntax: The
--commandflag tells the terminal what to run, andexec bashensures the terminal stays open afterwhoisfinishes. - 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:
- Get user input: Use
input()to capture user input, and clean it with.strip()to remove extra spaces. - Choose the right command method:
- Use
subprocess.run()for most cases (it returns aCompletedProcessobject 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.
- Use
- Handle command arguments safely:
- Instead of concatenating strings (like
whois " + w), pass arguments as a list tosubprocess.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
whoisargument, not as a separate terminal command.
- Instead of concatenating strings (like
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

