-
Notifications
You must be signed in to change notification settings - Fork 318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a command to open a VSCode Remote SSH connection from the command line via an already open SSH connection #3324
Comments
I believe this is related? #3738 |
Hi, Is this something that can be re open as it has almost 20 upvotes yet? The idea of running |
Sorry, this isn't really practical for us to do. It does work in the integrated terminal when you already have a remote session open. |
Is that also the case when the target device has VSCode (not the remote server) installed? Last time I tried that (around when I initially opened this issue) it opened VSCode on the target device instead of opening another remote session on my device or opening the file in my current remote session. |
Since vscode can run entirely in a browser, would it be possible to implement this feature as starting vscode on the remote server and providing a URL and authentication information in the terminal to quickly launch a local browser that connects to the remote machine? Obviously this would require an IP route to the destination machine, but given |
This has been working for me from related issue:
|
@will-sargent-eero that works for initiating a session from your workstation, not from the remote system you are already connected to. |
ssh back to client works. function rcode
set -l tuple (string split ' ' -- $SSH_CONNECTION)
set -l client $tuple[1]
set -l server $tuple[3]
for proj in $argv
set -l proj (realpath $proj)
ssh $client command code --folder-uri "vscode-remote://ssh-remote+$server/$proj" &
end
end |
^ The above script works well if you are using fish shell. For bash/zsh users: rcode() {
client=$(cut -d' ' -f1 <<<"$SSH_CONNECTION")
server=$(cut -d' ' -f3 <<<"$SSH_CONNECTION")
for proj in "$@"; do
proj=$(realpath $proj)
ssh $client /usr/local/bin/code --folder-uri "vscode-remote://ssh-remote+$server$proj"
done
}
|
Here is the PowerShell version for Windows users Requirement:
function c {
# $env:VSCODE_IPC_HOOK_CLI indicates that we might be in the vscode remote ssh terminal
# in case it handles the code call.
if ($env:SSH_CONNECTION -and ($null -eq $env:VSCODE_IPC_HOOK_CLI)) {
$arr = $env:SSH_CONNECTION -split " ";
$local = $arr[0];
$remote = $arr[2];
if ([System.IO.Path]::IsPathRooted($args[0])) {
$targetPath = $args[0];
}
else {
$targetPath = Join-Path -Resolve $Pwd $args[0];
}
$targetPath = $targetPath.Replace("\", "/");
$remoteURI = "vscode-remote://ssh-remote+$remote/$targetPath"
Write-Host "Opening $remoteURI on local machine..."
$user = "YOUR-USER-NAME-HERE"
$password = "YOUR-PASSWORD-HERE"
$supressCLIXML = '$ProgressPreference = "SilentlyContinue"'
$supressSTDOUT = '> $null 2> $null'
$startVSCode = "$supressCLIXML; Start-Process -WindowStyle Hidden code ""--folder-uri $remoteURI"""
$startVSCode_encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($startVSCode))
$sshCommand = @"
$supressCLIXML
if ((tasklist | findstr Code).length) {
echo "Running code --folder-uri $remoteURI"
code ""--folder-uri $remoteURI""
}
else {
psexec -d -u $user -p $password -i 1 powershell -NoProfile -EncodedCommand $startVSCode_encoded $supressSTDOUT
}
"@
ssh $local powershell -NoProfile -EncodedCommand $([Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($sshCommand)))
}
else {
code $args
}
} |
I wrote a script that works cross platform via node and can be installed via npm:
then you can run
|
Is this feature mentioned in any documentation? Maybe I'm just missing it, but a quick Related Stack Overflow question: How can I connect to a Remote SSH project from the command line? |
Thanks @starball5! We have documentation on this here: https://code.visualstudio.com/docs/remote/troubleshooting#_connect-to-a-remote-host-from-the-terminal. We also welcome contributions to VS Code docs, so if you have additional ideas, please feel free to open a PR, and I'm happy to review: https://github.com/microsoft/vscode-docs. |
@bamurtaugh thanks so much! Is there currently a way to open a remote via the CLI without providing a path? Ideally, I am looking for a similar experience when you use "Connect to Host", allowing you to select the folder in the subsequent VSCode window after the connection has been made. |
I don't believe so - that sounds like the original request from this issue. Though if I'm misunderstanding and it's actually different, please let me know. |
I found rcode() {
client=$(cut -d' ' -f1 <<<"$SSH_CONNECTION")
server=$(cut -d' ' -f3 <<<"$SSH_CONNECTION")
for proj in "$@"; do
proj=$(realpath "$proj")
ssh $client systemd-run --user --property=Type=forking /usr/local/bin/code --folder-uri="vscode-remote://ssh-remote+$server\"$proj\""
done
} |
@FreshlyBrewedCode Thank you a 1000x! It's easy, it's what I need, it's what should be included in vscode from the beginning. |
Adding this for anyone who finds your comment. You can now achieve this behaviour by doing the following:
This is documented here. Lose the path argument and it behaves like the connect to host UI option, not selecting the root by default. |
iTerm2 users who end up here my be interested in the following StackOverflow discussion, in which it is explained how to use iTerm2 to set up a command you can execute on the remote machine which triggers |
When using the VSCode Remote WSL features, I've come to appreciate and rely on the ability to open a folder or file in VSCode by using the
code
command in my WSL terminal and it starting up a VSCode on Windows -> VSCode Remote WSL window for the file or opening in a current window as if I had opened the file on my device directly.With the move to work from home, I've recently moved to using the VSCode Remote SSH features extensively to remote into my box at work instead of using RDP. I've even set up the ability to SSH into my machine via custom terminal profiles. However, I've encountered one issue that regularly messes up my workflow. Whenever I run
code .
in an ssh shell, it pops open VSCode on the remote machine instead of opening on my local machine and connecting to my remote machine. I'd love if there was a way to open VSCode with a remote SSH connection from the command line, run either on the target machine or the host or within or outside of a VSCode integrated terminal.The text was updated successfully, but these errors were encountered: