需求:

有一组windows vps,需要每天登录每台vps才能使其网络畅通,手动操作太麻烦,因此希望可以实现自动化操作。

实现:

方法1: 使用mstsc分别登录每台vps,同时勾选记录登录凭证,这样下次登录时就可以直接连接

           步骤1:  手动登录每天vps,记录登录凭证

           步骤2: 使用python 每天自动登录一次
import os
import time

fd = open('ip.txt')

for line in fd:
    line = line.strip()
    if line:
        os.system('mstsc %s /console ' % line)
        time.sleep(40)
        os.system('taskkill /IM mstsc.exe /F')
 方法2: 不需要手动操作一遍,直接使用python 调用  mstsc 和rdp配置文件实现远程登录

每次生成读取一个ip配置,然后生成rdp配置,进行访问
import os
import time

fd = open('ip.txt')

content = '''screen mode id:i:0   
desktopwidth:i:1680   
desktopheight:i:1050
session bpp:i:24   
winposstr:s:2,3,188,8,1062,721   
compression:i:1   
keyboardhook:i:2   
audiomode:i:0   
redirectdrives:i:0   
redirectprinters:i:0   
redirectcomports:i:0   
redirectsmartcards:i:0   
displayconnectionbar:i:1   
autoreconnection   
enabled:i:1   
username:s:administrator
domain:s:   
alternate shell:s:   
shell working directory:s:   
password 51:b:01000000D08C9DDF0115D1118C7A00C04FC297EB01000000150036A226D73049B94F47DFEAC25CF00000000200000000001066000000010000200000003948C31A914E0C434F65B9C706148F57123BDD56AF95945EC0BC7EBF7674BC53000000000E8000000002000020000000C6486E52178EB64EEEA3E6352F02E85D05EC1DA0CCD4BE3458772B04A0BAEFC0200000008C5B2E1FF95AEEE9428FE4E4087D1421A272F8E51B9FE2F3E5EF9C2700B555A540000000BFD5054EB7F8305CD2D32D07439922CA309D834ED16E50822ADB65CA73024DEB1574D67A19DEE634C34E2D1095268861D1081285AF57BB36BD252F42A12709F2
disable wallpaper:i:1   
disable full window drag:i:1   
disable menu anims:i:1   
disable themes:i:0   
disable cursor setting:i:0   
authentication level:i:0
bitmapcachepersistenable:i:1
'''

for line in fd:
    line = line.strip()
    if line:
        filename = 'tmp_auto.rdp'
        print line
        fw = open(filename,'w')
        fw.write(content)
        fw.write('full address:s:%s' % line)
        fw.close()
        os.system('mstsc %s /console ' % filename)
        time.sleep(40)
        os.system('taskkill /IM mstsc.exe /F')

问题1: 怎样生存password项,这个是加密项 参考说明: http://blog.sina.com.cn/s/blog_153c504650102w539.html

        使用python实现参考 http://blog.csdn.net/letv0907/article/details/38586359

        首先安装 pywin32  

        pip install pywin32

        其次安装 [pywin32-220.win32-py2.7.exe](https://superb-sea2.dl.sourceforge.net/project/pywin32/pywin32/Build%20220/pywin32-220.win32-py2.7.exe) 选择对应版本  参考 http://blog.csdn.net/qingche456/article/details/54587898

        如果发现python不在注册表里面 注册 使用如下代码进行注册
import sys

from _winreg import *

# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix

print installpath

regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
    installpath, installpath, installpath
)

def RegisterPy():
    try:
        reg = OpenKey(HKEY_CURRENT_USER, regpath)
    except EnvironmentError as e:
        try:
            reg = CreateKey(HKEY_CURRENT_USER, regpath)
            SetValue(reg, installkey, REG_SZ, installpath)
            SetValue(reg, pythonkey, REG_SZ, pythonpath)
            CloseKey(reg)
        except:
            print "*** Unable to register!"
            return
        print "--- Python", version, "is now registered!"
        return
    if (QueryValue(reg, installkey) == installpath and
        QueryValue(reg, pythonkey) == pythonpath):
        CloseKey(reg)
        print "=== Python", version, "is already registered!"
        return
    CloseKey(reg)
    print "*** Unable to register!"
    print "*** You probably have another Python installation!"

RegisterPy()

最后生成密码 使用C:\Python27\Lib\site-packages\pythonwin\pythinwin.exe 运行下面代码

import win32crypt,cmd  


u_pwd = unicode('123456')   #密码  
pwdHash = win32crypt.CryptProtectData(u_pwd,u'',None,None,None,0)  
pwd = 'password 51:b:' + binascii.hexlify(pwdHash).upper() #密码加密

但是发现总是提示一个安全框 很是麻烦,通过百度经验 https://jingyan.baidu.com/article/fec4bce2593467f2618d8b37.html

看到 mstsc 高级功能->设置是否显示告警,设置为连接不显示告警,在常规选项卡中 另保存设置 将其保存到rdp文件, 排查后确认添加 authentication

level:i:0 后 即可不会弹出对话框,但是经过测试每个windows生成的密码不同还是比较麻烦

最终生成的代码 如下:

import os
import time
import sys
import binascii
import win32crypt,cmd
import datetime


u_pwd = unicode('password')   
pwdHash = win32crypt.CryptProtectData(u_pwd,u'',None,None,None,0)
pwd = 'password 51:b:' + binascii.hexlify(pwdHash).upper() 

filename = 'ip.txt'

if len(sys.argv) >  1:

    filename = sys.argv[1]


content = '''screen mode id:i:0   
desktopwidth:i:1680   
desktopheight:i:1050
session bpp:i:24   
winposstr:s:2,3,188,8,1062,721   
compression:i:1   
keyboardhook:i:2   
audiomode:i:0   
redirectdrives:i:0   
redirectprinters:i:0   
redirectcomports:i:0   
redirectsmartcards:i:0   
displayconnectionbar:i:1
authentication level:i:0
autoreconnection
enabled:i:1   
username:s:administrator
domain:s:   
alternate shell:s:   
shell working directory:s:   
disable wallpaper:i:1   
disable full window drag:i:1   
disable menu anims:i:1   
disable themes:i:0   
disable cursor setting:i:0   
bitmapcachepersistenable:i:1
'''

while True:
    print datetime.datetime.now(), filename
    fd = open(filename)
    for line in fd:
        line = line.strip()
        if line:
            filename = 'tmp_auto.rdp'
            print line
            fw = open(filename,'w')
            fw.write(content)
            fw.write('full address:s:%s\n' % line)
            fw.write(pwd)
            fw.close()
            os.system('mstsc %s /console ' % filename)
            time.sleep(40)
            os.system('taskkill /IM mstsc.exe /F')
    print datetime.datetime.now()
    print 'wait for next 30 min'
    time.sleep(60*30)

将写好的python 程序发布出来:

http://blog.csdn.net/mrlevo520/article/details/51840217

pyinstall安装后在c:\python27\script下有个pyinstall.exe 直接执行 pyinstall.exe -F 1.py 即可 在c:\pythin32\script\dist下面找到打包好的exe文件

问题2: 连接后,怎样操作远程桌面,或者执行任意命令

       需要测试: http://blog.csdn.net/letv0907/article/details/38586359 

                        http://www.jb51.net/article/64102.htm

问题3: python 下 wmi 远程管理windows

 http://blog.csdn.net/hello\_lxc/article/details/49488731

results matching ""

    No results matching ""