|
| 1 | + |
| 2 | +# Using Pre- and Post-Connection Scripts in 1Remote |
| 3 | + |
| 4 | +1Remote is a remote access application that allows users to establish connections to remote computers. One of its features is the ability to run pre- and post-connection scripts. These scripts can be written in cmd(*.bat), PowerShell(*.ps1) or Python(*.py) and can be used to perform various tasks before a connection is established or after it disconnected. |
| 5 | + |
| 6 | +```plantuml format="svg" |
| 7 | +@startuml |
| 8 | +(*) |
| 9 | +if (Run script befor conncect?) then (yes) |
| 10 | + if (Script exit code is 0?) then (no) |
| 11 | + :Terminate connection; |
| 12 | + stop |
| 13 | + endif |
| 14 | +endif |
| 15 | +-Open connection |
| 16 | +-Have fun with your server |
| 17 | +-Disconnected |
| 18 | +-Run script disconnected |
| 19 | +stop |
| 20 | +@enduml |
| 21 | +``` |
| 22 | + |
| 23 | +To facilitate the script in identify each sessions, 1Remote will start the script with **`SESSION_ID`**, **`SERVER_ID`**, **`SERVER_NAME`**, and **`SERVER_HOST`** as environment variables. |
| 24 | + |
| 25 | +!!! note |
| 26 | + Noted:For the same server, the **SESSION_ID** for each session is different, while **SERVER_ID**, **SERVER_NAME**, and **SERVER_HOST** remain the same. |
| 27 | + |
| 28 | +## Setting Up Scripts |
| 29 | + |
| 30 | +To set up a script in 1Remote: |
| 31 | + |
| 32 | +1. Find a place to keep your script files, and create scripts there. |
| 33 | +2. Navigate to the `Edit` page of your server in 1Remote. |
| 34 | +3. Click the `Select` button and select the script file you want. |
| 35 | + |
| 36 | +  |
| 37 | + |
| 38 | +4. Click `Test` button to test the script, the debug info will popup. |
| 39 | + |
| 40 | +  |
| 41 | + |
| 42 | +5. If it is all green, click `Save` button to save. |
| 43 | +6. Then your script will be run every time you start this server. |
| 44 | + |
| 45 | +## Example Pre-Connection Script |
| 46 | + |
| 47 | +Here is an example of a script. The script will be executed in the directory where the script file is. |
| 48 | + |
| 49 | +``` powershell |
| 50 | +$sessionID = $env:SESSION_ID |
| 51 | +$serverID = $env:SERVER_ID |
| 52 | +$serverName = $env:SERVER_NAME |
| 53 | +$serverHost = $env:SERVER_HOST |
| 54 | +$serverTags = $env:SERVER_TAGS |
| 55 | +
|
| 56 | +echo "SESSION_ID: $sessionID" |
| 57 | +echo "SERVER_ID: $serverID" |
| 58 | +echo "SERVER_NAME: $serverName" |
| 59 | +echo "SERVER_HOST: $serverHost" |
| 60 | +echo "SERVER_TAGS: $serverTags" |
| 61 | +
|
| 62 | +
|
| 63 | +Set-Content -Path "temp.txt" -Value "SESSION_ID: $sessionID`r`nSERVER_ID: $serverID`r`nSERVER_NAME: $serverName`r`nSERVER_HOST: $serverHost`r`SERVER_TAGS: $serverTags" |
| 64 | +
|
| 65 | +# in the script before connect: |
| 66 | +# TODO: save $sessionID into file in pre-connect script |
| 67 | +# TODO: if $serverTags contains "HOME" then open VPN1 |
| 68 | +# TODO: if $serverTags contains "WORK" then open VPN2 |
| 69 | +
|
| 70 | +
|
| 71 | +# in the script after disconnected: |
| 72 | +# TODO: read $sessionID from file in post-connect script |
| 73 | +# TODO: if $sessionID == "12345" then close VPN1 |
| 74 | +# TODO: if $sessionID == "ABCDE" then close VPN2 |
| 75 | +
|
| 76 | +
|
| 77 | +# do some long time jobs, like opening a VPN |
| 78 | +sleep 3 |
| 79 | +
|
| 80 | +# exit with non-zero will terminate your connection request. |
| 81 | +exit 0 |
| 82 | +``` |
| 83 | + |
| 84 | +A typical application is to open a VPN before establishing a remote connection, and automatically close the VPN after terminating the remote connection. |
| 85 | + |
| 86 | +[Learn more](/usage/misc/pre-post-script-control/open-a-vpn-before-connect){ .md-button } |
| 87 | + |
| 88 | +{% include 'footer.md' %} |
0 commit comments