如何获取远程桌面服务器上客户端的ip

发布于 2025-01-09 15:58:39 字数 395 浏览 0 评论 0原文

我正在使用以下函数来实现一个程序,该程序根据所连接 PC 的 IP 改变其行为。
此功能有一个问题,如果尝试登录但失败,它可能会获取失败者的 IP。
既然我们遇到了这种可能性,程序就坏了。
我需要进行哪些编辑才能使该函数按预期运行?

import psutil

def get_ip(port=3389):
    ip = ""
    for x in psutil.net_connections():
        if x.status == "ESTABLISHED" and x.laddr.port == port:
            ip = x.raddr.ip
            break

I am using the following function to implement a program that changes its behavior depending on the IP of the connected PC.
There is a problem with this function that if something tries to login and fails, it may get the IP of the failed one.
And now that we've encountered that possibility, the program is broken.
What edits do I need to make to make this function behave as expected?

import psutil

def get_ip(port=3389):
    ip = ""
    for x in psutil.net_connections():
        if x.status == "ESTABLISHED" and x.laddr.port == port:
            ip = x.raddr.ip
            break

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几味少女 2025-01-16 15:58:39

我根据 Bijay Regmi 的评论更改了该功能。谢谢。 wmi对我来说很难,所以我用win32evtlog一点一点地读出来。我正在努力提高可读性并一点一点地发现错误。

    def systime(xml):
        return datetime.fromisoformat(xml.find(f'{ns}System/{ns}TimeCreated').get('SystemTime')[:-2] + "+00:00")

    def last_event(handle,
                   event_id,
                   condition: Callable[['Event'], bool] = None) -> Optional['Event']:
        now = datetime.now(tz=timezone.utc)
        while True:
            events = win32evtlog.EvtNext(handle, 20)
            if not events:
                break
            for event in events:
                xml_content = win32evtlog.EvtRender(event, win32evtlog.EvtRenderEventXml)
                obj = Event(ET.fromstring(xml_content))
                if obj.EventID == event_id:
                    if obj.SystemTime + timedelta(minutes=5) < now:
                        return None
                    if condition and not condition(obj):
                        continue
                    return obj

    class Event:
        def __init__(self, xml: ET.Element):
            self.EventID = xml and xml.find(f'{ns}System/{ns}EventID').text
            self.SystemTime = xml and systime(xml)
            self.xml = xml
            if self.EventID == '24':
                self.IpAddress = xml.find(f'{ns}UserData/{{Event_NS}}EventXML/{{Event_NS}}Address').text
            elif self.EventID == '4624':
                self.IpAddress = xml.find(f'{ns}EventData/{ns}Data[@Name="IpAddress"]').text
            else:
                self.IpAddress = None

I changed the function based on Bijay Regmi's comment. Thank you. wmi was difficult for me, so I used win32evtlog to read it out little by little. I am working on improving readability and finding bugs little by little.

    def systime(xml):
        return datetime.fromisoformat(xml.find(f'{ns}System/{ns}TimeCreated').get('SystemTime')[:-2] + "+00:00")

    def last_event(handle,
                   event_id,
                   condition: Callable[['Event'], bool] = None) -> Optional['Event']:
        now = datetime.now(tz=timezone.utc)
        while True:
            events = win32evtlog.EvtNext(handle, 20)
            if not events:
                break
            for event in events:
                xml_content = win32evtlog.EvtRender(event, win32evtlog.EvtRenderEventXml)
                obj = Event(ET.fromstring(xml_content))
                if obj.EventID == event_id:
                    if obj.SystemTime + timedelta(minutes=5) < now:
                        return None
                    if condition and not condition(obj):
                        continue
                    return obj

    class Event:
        def __init__(self, xml: ET.Element):
            self.EventID = xml and xml.find(f'{ns}System/{ns}EventID').text
            self.SystemTime = xml and systime(xml)
            self.xml = xml
            if self.EventID == '24':
                self.IpAddress = xml.find(f'{ns}UserData/{{Event_NS}}EventXML/{{Event_NS}}Address').text
            elif self.EventID == '4624':
                self.IpAddress = xml.find(f'{ns}EventData/{ns}Data[@Name="IpAddress"]').text
            else:
                self.IpAddress = None
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文