Python实现的远程登录windows系统功能示例

本文实例讲述了Python实现的远程登录windows系统功能。分享给大家供大家参考,具体如下:

首先安装wmi 命令:

pip install wmi

然后会报错缺少pywin32-219.win-amd64-py2.7.exe包,去下面这个地址下载
http://sourceforge.net/projects/pywin32/files/pywin32/

寻找适合自己电脑位数和python的包下载安装

下面是远程连接的代码:

# -*- coding:utf-8 -*-
#! python2
import wmi
def sys_version(ipaddress, user, password):
  conn = wmi.WMI(computer=ipaddress, user=user, password=password)
  for sys in conn.Win32_OperatingSystem():
    print "Version:%s" % sys.Caption.encode("UTF8"),"Vernum:%s" % sys.BuildNumber #系统信息
    print sys.OSArchitecture.encode("UTF8") # 系统的位数
    print sys.NumberOfProcesses # 系统的进程数
if __name__ == '__main__':
  sys_version(ipaddress="ip", user="用户名", password="密码")

附:python使用socket远程执行命令,并返回值操作示例

#!/usr/bin/env python
# TCP-Server
import socket
import subprocess
sk_obj = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sk_obj.bind(('127.0.0.1',8000))
sk_obj.listen(5)
while True:
  conn,ipaddr = sk_obj.accept()
  print ('connection from ip: %s' % ipaddr[0])
  while True:
    try:
      from_recv = conn.recv(8096)
      if len(from_recv) == 0:continue
      print ('from ip : %s information : %s' % (ipaddr[0],from_recv))
      res = subprocess.Popen(from_recv.decode('utf-8'),shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
      msg = res.stdout.read()
      if len(msg) == 0:
        msg = res.stderr.read()
      conn.send(msg)
    except Exception:
      break
  conn.close()
sk_obj.close()
#!/usr/bin/env python
# TCP-Client
import socket
import sys
sk_obj=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sk_obj.connect(('127.0.0.1',8000))
while True:
  msg = raw_input('-->').strip()
  if len(msg)==0:continue
  sk_obj.send(msg.encode('utf-8'))
  data = sk_obj.recv(8096)
  print ('Server send information : %s' % data.decode('utf-8'))
sk_obj.close()

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python进程与线程操作技巧总结》、《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

相关推荐