使用Python让用户登录到网络中的每台计算机

2024-04-23 09:51:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一种方法,可以监视用户是否使用Python登录到网络中的某些计算机,并将数据返回到一台计算机。理想情况下,我希望能够通过ip地址检查每台计算机,并查看用户是否登录。计算机有Windows

所以我想构建一个这样的函数:

IP_ADDRESS = '123.123.123.123'
check_user(IP_ADDRESS)

输出: 登录用户:Ed

输出: 登录用户:不适用


Tags: 数据方法函数用户ip网络addresswindows
2条回答

您必须像这样准备MS Windows服务:How do you run a Python script as a service in Windows? 您的服务必须打开端口进行检查,您可以使用FastAPI。您可以通过以下方式检查当前用户:https://docs.python.org/3/library/getpass.html#getpass.getuser

下面的解决方案工作得很好

如果有人知道如何使用python命令解锁计算机,请告诉我。我从网上的一个人那里得到了这个代码,并根据我的目的修改了它

如果有人认出了我,让我知道,这样我们就可以给信用。我记不起他的名字了

这是我的服务器

import socket
import argparse
import threading 
import csv



parser = argparse.ArgumentParser(description = "This is the server for the multithreaded socket demo!")
parser.add_argument(' host', metavar = 'host', type = str, nargs = '?', default = socket.gethostname())
parser.add_argument(' port', metavar = 'port', type = int, nargs = '?', default = 9999)
args = parser.parse_args()

print(f"Running the server on: {args.host} and port: {args.port}")

sck = socket.socket()
sck.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

try: 
    sck.bind((args.host, args.port))
    sck.listen(5)
except Exception as e:
    raise SystemExit(f"We could not bind the server on host: {args.host} to port: {args.port}, because: {e}")


def on_new_client(client, connection):
    ip = connection[0]
    port = connection[1]
    print(f"THe new connection was made from IP: {ip}, and port: {port}!")
    while True:
        msg = client.recv(1024)

        # with open('aa.csv', 'w', newline='') as csvfile:
        #     thewriter = csv.writer(csvfile)
        #     thewriter.writerow([msg.decode(),'computer 2'])

        if msg.decode() == 'exit':
            break
            
        if msg.decode() == 'lock computer 1':
            reply = 'lock'
            client.sendall(reply.encode('utf-8'))

        elif msg.decode() == 'open computer 1':
            reply = 'open'
            client.sendall(reply.encode('utf-8'))

        # print(f"The client said: {msg.decode()}")
        # reply = f"You told me: {msg.decode()}"


        # client.sendall(reply.encode('utf-8'))
    #print(f"The client from ip: {ip}, and port: {port}, has gracefully diconnected!")
    #client.close()

while True:
    try: 
        client, ip = sck.accept()
        threading._start_new_thread(on_new_client,(client, ip))
    except KeyboardInterrupt:
        print(f"Gracefully shutting down the server!")
    except Exception as e:
        print(f"Well I did not anticipate this: {e}")

sck.close()

这是我的客户

import socket 
import argparse
import subprocess
import time
import subprocess

parser = argparse.ArgumentParser(description = "This is the client for the multi threaded socket server!")
parser.add_argument(' host', metavar = 'host', type = str, nargs = '?', default = socket.gethostname())
parser.add_argument(' port', metavar = 'port', type = int, nargs = '?', default = 9999)
args = parser.parse_args()

print(f"Connecting to server: {args.host} on port: {args.port}")

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sck:
    try:
        sck.connect((args.host, args.port))
    except Exception as e:
        raise SystemExit(f"We have failed to connect to host: {args.host} on port: {args.port}, because: {e}")

    while True:
        # msg = input("What do we want to send to the server?: ")

        time.sleep(2)
        # process_name='LogonUI.exe'
        # callall='TASKLIST'
        # outputall=subprocess.check_output(callall)
        # outputstringall=str(outputall)
        # if process_name in outputstringall:
        #     msg = 'Free'
        # else:
        #     msg = 'occupied'
        msg = ('lock computer 1')
        sck.sendall(msg.encode('utf-8'))

        if msg =='exit':
            print("Client is saying goodbye!")
            break

        data = sck.recv(1024)
        if data.decode() == 'lock':
            print('should I lock the computer?')
            process_name='LogonUI.exe'
            callall='TASKLIST'
            outputall=subprocess.check_output(callall)
            outputstringall=str(outputall)
            if process_name in outputstringall:
                print("Already locked")
            else: 
                print("needs to be locked.")

        print(f"The server's response was: {data.decode()}")

相关问题 更多 >