溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python實(shí)現(xiàn)的遠(yuǎn)程登錄windows系統(tǒng)功能示例

發(fā)布時(shí)間:2020-10-02 02:00:29 來源:腳本之家 閱讀:165 作者:hello_lxc 欄目:開發(fā)技術(shù)

本文實(shí)例講述了Python實(shí)現(xiàn)的遠(yuǎn)程登錄windows系統(tǒng)功能。分享給大家供大家參考,具體如下:

首先安裝wmi 命令:

pip install wmi 

然后會(huì)報(bào)錯(cuò)缺少pywin32-219.win-amd64-py2.7.exe包,去下面這個(gè)地址下載
http://sourceforge.net/projects/pywin32/files/pywin32/

尋找適合自己電腦位數(shù)和python的包下載安裝

下面是遠(yuǎn)程連接的代碼:

# -*- 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 #系統(tǒng)信息
    print sys.OSArchitecture.encode("UTF8") # 系統(tǒng)的位數(shù)
    print sys.NumberOfProcesses # 系統(tǒng)的進(jìn)程數(shù)
if __name__ == '__main__':
  sys_version(ipaddress="ip", user="用戶名", password="密碼")

附:python使用socket遠(yuǎn)程執(zhí)行命令,并返回值操作示例

#!/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()

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進(jìn)程與線程操作技巧總結(jié)》、《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI