Skip to content

Commit 8dacc99

Browse files
committed
First push V1.0
1 parent d366fea commit 8dacc99

File tree

5 files changed

+242
-1
lines changed

5 files changed

+242
-1
lines changed

Link.bat

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@echo off
2+
setlocal enabledelayedexpansion
3+
4+
:: Get the directory where this batch script is located
5+
set "script_dir=%~dp0"
6+
7+
:: Check if marker file exists
8+
if not exist "%script_dir%\.first_run_marker.txt" (
9+
:: Run the installSSH.ps1 script
10+
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%script_dir%installSSH.ps1""' -Verb RunAs -Wait}"
11+
12+
:: Create hidden marker file
13+
echo First run completed > "%script_dir%\.first_run_marker.txt"
14+
attrib +h "%script_dir%\.first_run_marker.txt"
15+
)
16+
17+
:: Run the connect.ps1 script
18+
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -File "%script_dir%connect.ps1"

README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,47 @@
1-
# windowsRDP-SSH-tunnel-script
1+
# windowsRDP-SSH-tunnel-script
2+
3+
These scripts are to be used to open an SSH connection and create an SSH tunnel for windows RDP and/or FTP, the script can be edited by editing the connect.ps1 file.
4+
5+
it will also install all dependancies on first run
6+
7+
## Setup
8+
9+
### Step 1 - Put Required files in folder
10+
11+
rename your private key file to ``key`` and put it in the folder
12+
13+
create a ``connection.rdp`` file in the folder, for the server and user you would like to connect to if you want the script to open the RDP for you.
14+
15+
### Step 2 - Edit config.env
16+
17+
Open the config.env file in your prefered editor E.g VSCode and edit the variables at the very top only changing the ones below the comment if you are sure of what you are doing
18+
19+
```
20+
USER=User here
21+
IP=public IP of Server Here
22+
PORT=SSH Port here
23+
RDP1=port you would like to use to access RDP
24+
FTP1=port you would like to use to access FTP
25+
COMPANY_NAME=Name here
26+
SERVER_NAME=Server Name
27+
KEY_PATH=.\key
28+
29+
30+
## ONLY EDIT BELOW THIS LINE IF YOU ARE SURE YOU WANT TO MAKE THE CHANGES, ADDING MORE VARIABLES WILL MEAN YOU NEED TO EDIT THE CONNECT.PS1 File
31+
H1=127.0.0.1
32+
H1P=3389
33+
H2P=21
34+
```
35+
36+
### Optional Step - Hide everything except for the link.bat file
37+
38+
as the title says if you want the folder to be simple to use hide everything except the link.bat file
39+
40+
## Step 3 - Run
41+
42+
to run the script just double click the link.bat file
43+
44+
#### Note
45+
46+
on first run the script will ask for admin privilages this is to run the installSSH.ps1 script which install the required dependancies and then creates a blank text file which the script will find in future and know it doesnt need to install them.
47+

config.env

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
USER=User here
2+
IP=public IP of Server Here
3+
PORT=SSH Port here
4+
RDP1=port you would like to use to access RDP
5+
FTP1=port you would like to use to access FTP
6+
COMPANY_NAME=Name here
7+
SERVER_NAME=Server Name
8+
KEY_PATH=key
9+
10+
11+
## ONLY EDIT BELOW THIS LINE IF YOU ARE SURE YOU WANT TO MAKE THE CHANGES, ADDING MORE VARIABLES WILL MEAN YOU NEED TO EDIT THE CONNECT.PS1 File
12+
H1=127.0.0.1
13+
H1P=3389
14+
H2P=21

connect.ps1

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Function to load env variables from a file
2+
function Load-EnvFile {
3+
param (
4+
[string]$path
5+
)
6+
7+
if (-not (Test-Path $path)) {
8+
throw "Env file '$path' does not exist."
9+
}
10+
11+
$lines = Get-Content -Path $path
12+
foreach ($line in $lines) {
13+
if ($line -match '^\s*#') {
14+
continue
15+
}
16+
if ($line -match '^\s*(.+?)\s*=\s*(.+?)\s*$') {
17+
[System.Environment]::SetEnvironmentVariable($matches[1], $matches[2])
18+
}
19+
}
20+
}
21+
22+
# Load the env file
23+
$envFilePath = ".\config.env"
24+
Load-EnvFile -path $envFilePath
25+
26+
# Read env variables
27+
$user = [System.Environment]::GetEnvironmentVariable("USER")
28+
$ip = [System.Environment]::GetEnvironmentVariable("IP")
29+
$port = [System.Environment]::GetEnvironmentVariable("PORT")
30+
$RDP1 = [System.Environment]::GetEnvironmentVariable("RDP1")
31+
$FTP1 = [System.Environment]::GetEnvironmentVariable("FTP1")
32+
$h1 = [System.Environment]::GetEnvironmentVariable("H1")
33+
$h1p = [System.Environment]::GetEnvironmentVariable("H1P")
34+
$h2p = [System.Environment]::GetEnvironmentVariable("H2P")
35+
$CompanyName = [System.Environment]::GetEnvironmentVariable("COMPANY_NAME")
36+
$ServerName = [System.Environment]::GetEnvironmentVariable("SERVER_NAME")
37+
$keyPath = [System.Environment]::GetEnvironmentVariable("KEY_PATH")
38+
39+
# Set correct permissions on the private key file
40+
try {
41+
icacls $keyPath /inheritance:r /grant:r "$($env:USERNAME):(R)" /remove "Everyone" /T
42+
Write-Host "Permissions set on the key file successfully." -ForegroundColor Green
43+
} catch {
44+
Write-Host "Failed to set permissions on the key file. Please check the file path and permissions manually." -ForegroundColor Red
45+
Exit
46+
}
47+
48+
$knownHostsPath = "$env:userprofile\.ssh\known_hosts"
49+
$hostFingerprint = (ssh-keygen -l -F $ip | Out-String).Trim()
50+
if (-not $hostFingerprint) {
51+
ssh-keyscan $ip | Out-File -Append -Encoding utf8 $knownHostsPath
52+
}
53+
54+
Write-Host ""
55+
Write-Host ""
56+
Write-Host ""
57+
Write-Host ""
58+
Write-Host "" # These are needed to prevent the below loop from covering the text
59+
Write-Host ""
60+
Write-Host ""
61+
Write-Host ""
62+
Write-Host ""
63+
Write-Host ""
64+
Write-Host "****************************************" -ForegroundColor DarkYellow
65+
Write-Host "* Connecting to $CompanyName Servers *" -ForegroundColor DarkYellow
66+
Write-Host "****************************************" -ForegroundColor DarkYellow
67+
Write-Host ""
68+
Write-Host "Establishing link to $ServerName." -ForegroundColor DarkYellow
69+
Write-Host ""
70+
71+
# Prompt user for which tunnels to open
72+
$rdpResponse = Read-Host "Would you like to open the RDP tunnel? (yes/y/no)"
73+
$ftpResponse = Read-Host "Would you like to open the FTP tunnel? (yes/y/no)"
74+
75+
# Build SSH command based on user responses
76+
$sshCommand = "ssh -p $port $user@$ip -N"
77+
if ($rdpResponse -eq "yes" -or $rdpResponse -eq "y") {
78+
$sshCommand += " -L ${RDP1}:${h1}:${h1p}"
79+
}
80+
if ($ftpResponse -eq "yes" -or $ftpResponse -eq "y") {
81+
$sshCommand += " -L ${FTP1}:${h1}:${h2p}"
82+
}
83+
$sshCommand += " -i $keyPath"
84+
85+
# Try to start the SSH process
86+
try {
87+
$sshProcess = Start-Process powershell -ArgumentList "-NoExit", "-Command `"$sshCommand`"" -NoNewWindow -ErrorAction Stop
88+
} catch {
89+
Write-Host "Failed to establish SSH connection. Please check your credentials and key file." -ForegroundColor Red
90+
Exit
91+
}
92+
93+
# Set timeout duration in seconds
94+
$timeout = 60
95+
96+
# Check for SSH tunnel connection
97+
$connectedRDP = $false
98+
$connectedFTP = $false
99+
$startTime = Get-Date
100+
101+
while ((-not $connectedRDP -and ($rdpResponse -eq "yes" -or $rdpResponse -eq "y")) -or (-not $connectedFTP -and ($ftpResponse -eq "yes" -or $ftpResponse -eq "y")) -and (New-TimeSpan -Start $startTime -End (Get-Date)).TotalSeconds -lt $timeout) {
102+
if ($rdpResponse -eq "yes" -or $rdpResponse -eq "y") {
103+
$connectedRDP = Test-NetConnection -ComputerName $h1 -Port $RDP1 -WarningAction SilentlyContinue | Select-Object -ExpandProperty TcpTestSucceeded
104+
}
105+
if ($ftpResponse -eq "yes" -or $ftpResponse -eq "y") {
106+
$connectedFTP = Test-NetConnection -ComputerName $h1 -Port $FTP1 -WarningAction SilentlyContinue | Select-Object -ExpandProperty TcpTestSucceeded
107+
}
108+
Start-Sleep -Seconds 1
109+
}
110+
111+
if ((-not $connectedRDP -and ($rdpResponse -eq "yes" -or $rdpResponse -eq "y")) -or (-not $connectedFTP -and ($ftpResponse -eq "yes" -or $ftpResponse -eq "y"))) {
112+
Write-Host ""
113+
Write-Host "Connection could not be established within $timeout seconds." -ForegroundColor Red
114+
Write-Host "Your link to the $CompanyName servers has not been established." -ForegroundColor Red
115+
Write-Host "Please make sure you have your credentials ready, close this window and try again" -ForegroundColor Red
116+
Write-Host ""
117+
Exit # Exits if the tunnel isn't established
118+
}
119+
120+
Write-Host ""
121+
Write-Host "Link to $ServerName Established" -ForegroundColor Green
122+
Write-Host ""
123+
Write-Host "---------------------------------------------------------" -ForegroundColor Blue
124+
Write-Host "If the remote connection doesn't automatically open and" -ForegroundColor Blue
125+
Write-Host "start connecting, Use the below Details to connect to" -ForegroundColor Blue
126+
Write-Host "the relevant services:" -ForegroundColor Blue
127+
Write-Host "---------------------------------------------------------" -ForegroundColor Blue
128+
if ($rdpResponse -eq "yes" -or $rdpResponse -eq "y") {
129+
Write-Host "Remote Desktop Connection:" -ForegroundColor Blue
130+
Write-Host "IP: $h1" -ForegroundColor Blue
131+
Write-Host "Port: $RDP1" -ForegroundColor Blue
132+
}
133+
if ($ftpResponse -eq "yes" -or $ftpResponse -eq "y") {
134+
Write-Host "FTP:" -ForegroundColor Blue
135+
Write-Host "IP: $h1" -ForegroundColor Blue
136+
Write-Host "Port: $FTP1" -ForegroundColor Blue
137+
}
138+
#Write-Host "User: $user" -ForegroundColor Blue
139+
Write-Host "---------------------------------------------------------" -ForegroundColor Blue
140+
Write-Host "" -ForegroundColor Blue
141+
142+
# Ask the user if they want to open the remote desktop connection
143+
if ($rdpResponse -eq "yes" -or $rdpResponse -eq "y") {
144+
$openRDP = Read-Host "Would you like to open the remote desktop connection now? (yes/y/no)"
145+
if ($openRDP -eq "yes" -or $openRDP -eq "y") {
146+
Write-Host "Opening remote desktop connection window" -ForegroundColor DarkYellow
147+
# Open RDP window
148+
Start-Process mstsc 'connection.rdp'
149+
Write-Host ""
150+
Write-Host "Window Opened" -ForegroundColor Green
151+
} else {
152+
Write-Host "You chose not to open the remote desktop connection." -ForegroundColor Yellow
153+
}
154+
}
155+
156+
Write-Host ""
157+
Write-Host "Keep this PowerShell window open!" -ForegroundColor Blue
158+
Write-Host "When you close this window, it will close your link to the $CompanyName servers." -ForegroundColor Blue

installSSH.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Add OpenSSH.Client if not already installed
2+
$sshCapability = Get-WindowsCapability -Online | Where-Object { $_.Name -like 'OpenSSH.Client*' }
3+
if (!$sshCapability) {
4+
Add-WindowsCapability -Online -Name OpenSSH.Client*
5+
}

0 commit comments

Comments
 (0)