我制作了一个python脚本,可以在我父亲的学校 Windows 服务器上运行,该服务器执行powershell脚本来清除打印机队列(用example_script.sh
替换了脚本路径),只有一些老师是允许使用该脚本,因此我也在其计算机上安装了客户端脚本。他们都在使用Windows计算机。我应该说这只是一个临时解决方案,我绝不是python pro,因此,如果您看到除我现在要问的那个错误或安全性问题之外,请告诉我。< / p>
这是服务器脚本:
import socket
import subprocess as sp
def server():
# Define server
try:
host = socket.gethostbyname(socket.gethostname())
port = 1234
except Exception as e:
print("ERROR: Can't create host, enter the host IP address manually.")
raise e
# Create an INET streaming socket (i.e. IPv4 and TCP)
try:
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((host, port))
except Exception as e:
print("ERROR: Can't create socket.")
raise e
# Read whitelisted IPs and ports from file
try:
ip_file = open("allowed_ips.txt", "r")
ip_list = [line.strip('\n') for line in ip_file]
print("Allowed IP addresses: {}".format([ip for ip in ip_list]))
except IOError as e:
print("ERROR: allowed_ips.txt can't be opened. Check if it is in the same directory and has the name 'allowed_ips.txt'.")
raise e
ip_file.close()
try:
port_file = open("allowed_ports.txt", "r")
port_list = [int(line.strip('\n')) for line in port_file]
print("Allowed port numbers: {}".format([port for port in port_list]))
except IOError as e:
print("ERROR: allowed_ports.txt can't be opened. Check if it is in the same directory and has the name 'allowed_ports.txt'.")
raise e
port_file.close()
password = "testpassword"
# Listen for 1 connection at a time
server_socket.listen(1)
print("{} listening on port {}".format(host, port))
while True:
conn, address = server_socket.accept()
# Security checks, add allowed ip's and ports to lists
print("Connection from {} on port {}".format(str(address[0]), str(address[1])))
if str(address[0]) not in ip_list:
conn.close()
print("ERROR: forbidden IP address")
if address[1] not in port_list:
conn.close()
print("ERROR: forbidden port number")
data = "Please enter the password below.\n"
conn.send(data.encode())
recv = str(conn.recv(1024).decode())
print("Client > " + recv)
if recv.lower().strip() != password:
conn.send("Wrong password.".encode())
conn.close()
print("Error: wrong password! Connection closed.");
elif recv.lower().strip() == password:
try:
sp.run(["./example_script.sh"])
except IOError as e:
print("ERROR: Can't run executable. Please check if the path is correct.")
raise e
conn.send("Process completed.".encode())
conn.close()
print("Process completed. Connection closed.")
conn.close()
if __name__ == "__main__":
server()
这是客户端脚本:
import socket
import getpass
import time
def client():
source_port = 4444
# Gets IP address automatically
source_host = socket.gethostbyname(socket.gethostname())
print("Source host: {}".format(source_host))
# Set server IP address manually
host = "192.168.200.123"
port = 1234
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
client_socket.bind((source_host, source_port))
client_socket.connect((host, port))
exit_codes = ["wrong password.", "process completed."]
while True:
data = client_socket.recv(1024).decode()
print(data)
if data.lower().strip() in exit_codes:
print("Closing program...")
time.sleep(3)
break
message = getpass.getpass("Password: ")
client_socket.send(message.encode())
client_socket.close()
if __name__ == "__main__":
client()
因此,简而言之,服务器脚本在内部服务器上运行,并且需要运行需要管理访问权限的脚本,因此我设置了另一个密码,如果客户端输入正确的密码,该密码将授予服务器脚本访问权限以执行。 / p>
由于老师的计算机知识不是很高,所以我将可执行的python脚本放在他们的桌面上,以便他们可以单击它。这将打开与服务器的连接。但是由于getpass在输入密码的提示中未显示任何反馈,因此他们会慌张并关闭提示屏幕(X),由于某种原因,这会引发以下错误:
我在linux上工作,所以在测试时没有遇到此错误,你们是否有解决办法?