I made a lot of files for RDP connection with Python

Introduction

When it is necessary to make an RDP connection to multiple terminals, I thought that it would be troublesome to manually connect by looking at the Excel list, so I tried to make RDP files collectively in Python. . !!

Preparation

The input information and Python file are shown below.

Input information

Create the following two files.

  1. RDP connection destination list (connection destination list \ _sample.xlsx)
  2. RDP template (template.rdp)

1. RDP connection destination list (connection destination list \ _sample.xlsx)

image.png

2. RDP template (template.rdp)

Save it in your working directory as template.rdp, using "Computer Name" as "Computer Address", "User Name" as "UserName \ _for \ _RDP", and "Save As" as shown below. Put. image.png

The RDP connection file (template.rdp) is as follows when opened with a text editor. It can be read that the password is not saved in the RDP file itself.

template.rdp


screen mode id:i:2
use multimon:i:1
desktopwidth:i:1920
desktopheight:i:1080
session bpp:i:32
winposstr:s:0,1,759,0,980,270
compression:i:1
keyboardhook:i:2
audiocapturemode:i:0
videoplaybackmode:i:1
connection type:i:7
networkautodetect:i:1
bandwidthautodetect:i:1
displayconnectionbar:i:1
enableworkspacereconnect:i:0
disable wallpaper:i:0
allow font smoothing:i:0
allow desktop composition:i:0
disable full window drag:i:1
disable menu anims:i:1
disable themes:i:0
disable cursor setting:i:0
bitmapcachepersistenable:i:1
full address:s:ComputerAddress
audiomode:i:0
redirectprinters:i:1
redirectcomports:i:0
redirectsmartcards:i:1
redirectclipboard:i:1
redirectposdevices:i:0
autoreconnection enabled:i:1
authentication level:i:2
prompt for credentials:i:0
negotiate security layer:i:1
remoteapplicationmode:i:0
alternate shell:s:
shell working directory:s:
gatewayhostname:s:
gatewayusagemethod:i:4
gatewaycredentialssource:i:4
gatewayprofileusagemethod:i:0
promptcredentialonce:i:0
gatewaybrokeringtype:i:0
use redirection server name:i:0
rdgiskdcproxy:i:0
kdcproxyname:s:
drivestoredirect:s:
camerastoredirect:s:*
devicestoredirect:s:*
username:s:UserName_for_RDP

Python files

RDP_File_Generator.py



# -*- coding: utf-8 -*-
"""
RDP file creation PGM
"""

import tkinter, tkinter.filedialog, sys, os
import pandas as pd

dir1 = r"C:\Users\XXXXX\Desktop\xxxxxx"
#↑ Specify the file path that stores the "RDP connection destination list" and "RDP template"

##The rule of tkinter.
root = tkinter.Tk()
root.withdraw()

msg1 = 'Please select a destination list'
typ1 = [('Excel file','*.xlsx')] 
inFile1 = tkinter.filedialog.askopenfilename(title=msg1, filetypes = typ1, initialdir = dir1) 
if (not inFile1): #[Cancel]Processing at the time of clicking
  print('Please select a file.')
  sys.exit

input_book1 = pd.ExcelFile(inFile1)
input_sheet_name1 = input_book1.sheet_names
input_sheet_df1 = input_book1.parse(input_sheet_name1[0],header=3)
df_s = input_sheet_df1.iloc[:,2:]

msg2 = 'Please select an RDP file'
typ2 = [('RDP file','*.rdp')] 
inFile2 = tkinter.filedialog.askopenfilename(title=msg2, filetypes = typ2, initialdir = dir1) 
if (not inFile1): #[Cancel]Processing at the time of clicking
  print('Please select a file.')
  sys.exit
  
  
path_name = os.path.dirname(inFile2)
output_folder_path = os.path.join(path_name,"output")
##Create RDP file output destination folder (exist)_ok:Skip if existing)
os.makedirs(output_folder_path,exist_ok = True)
  
##Open the RDP template file as a text file
with open(inFile2,encoding='utf_16') as f:
  s = f.read()

##Generate as many RDP files as there are destinations in the destination list
for i in range(len(df_s)):
  temp = s
  temp = temp.replace("UserName_for_RDP", df_s["User ID"].iat[i])
  temp = temp.replace("ComputerAddress", df_s["IP address"].iat[i])
  path_w = os.path.join(output_folder_path,df_s["User ID"].iat[i]+".rdp")
  with open(path_w,mode="w") as f:
    f.write(temp)

Try to run

Below is the execution image.

First, when you execute "RDP_File_Generator.py", a dialog by tkinter will be displayed, so select the connection destination list. image.png

Then select a template for your RDP file. image.png image.png

An output folder is created, and RDP files are output in a batch in that folder! It's a success! !!

in conclusion

Since the password is not embedded in the RDP file, there is still the trouble of entering the password and saving the credentials on the connection source PC at the first connection, but I think it is convenient, so please refer to it if you like. I would be happy if you could!

Recommended Posts

I made a lot of files for RDP connection with Python
I made a fortune with Python.
I made a daemon with Python
I made a character counter with Python
I made a Hex map with Python
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
I made a library to easily read config files with Python
I made a python dictionary file for Neocomplete
I made a competitive programming glossary with Python
I made a GUI application with Python + PyQt5
I made a Twitter fujoshi blocker with Python ①
[Python] I made a Youtube Downloader with Tkinter.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made a Twitter BOT with GAE (python) (with a reference)
I made a Christmas tree lighting game with Python
I made a window for Log output with Tkinter
I made blackjack with python!
I made a net news notification app with Python
I made a VM that runs OpenCV for Python
I made a Python3 environment on Ubuntu with direnv.
I made a LINE BOT with Python and Heroku
[Python] I made a classifier for irises [Machine learning]
I made a python text
I made blackjack with Python.
I made wordcloud with Python.
I made a simple typing game with tkinter in Python
I made a package to filter time series with python
[VSCode] I made a user snippet for Python print f-string
I made a simple book application with python + Flask ~ Introduction ~
I made a resource monitor for Raspberry Pi with a spreadsheet
I made a puzzle game (like) with Tkinter in Python
I tried a stochastic simulation of a bingo game with Python
Turn an array of strings with a for statement (Python3)
I made a Line-bot using Python!
I made a simple circuit with Python (AND, OR, NOR, etc.)
I made a package that can compare morphological analyzers with Python
I made a Nyanko tweet form with Python, Flask and Heroku
I searched for a similar card of Hearthstone with Deep Learning
I tried to create a list of prime numbers with python
The story of making a standard driver for db with python.
[Python] I made an image viewer with a simple sorting function.
I made a shuffle that can be reset (reverted) with Python
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
I made a scaffolding tool for the Python web framework Bottle
I made a poker game server chat-holdem using websocket with python
I made a segment tree with python, so I will introduce it
Get a list of files in a folder with python without a path
I made a Python wrapper library for docomo image recognition API.
I did a lot of research on how Python is executed
I made a dash docset for Holoviews
I drew a heatmap with seaborn [Python]
Connect a lot of Python or and and
I tried a functional language with Python
What I did with a Python array
Debug for mysql connection with python mysql.connector
I made a life game with Numpy
I made a stamp generator with GAN