How to use a new Windows Terminal app for SSH? [closed] - windows

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
The Windows Terminal app is advertised as a central hub for all terminal work, so I'm interested in a way to bring my SSH connections into it and replace ancient PuTTY.

You can use a commandline field in your profile configuration to initiate an SSH connection on tab creation.
Step-by-step guide:
Ensure you have an SSH client (try to connect to the server from a Command Prompt tab). #dhgouveia2's post details this step.
Open Settings (Ctrl+,)
Find the "list" array in the "profiles" object
Find a Command Prompt profile ("commandline": "cmd.exe")
Duplicate the profile (copy-paste the whole object, watch for the comma between objects)
Change the "guid" value to a new GUID (for example, from here)
Change the commandline value to "commandline" : "ssh me#my-server -p 22 -i ~/.ssh/id_rsa" (use your own connection command).
Change the profile's "name"
Add an "icon" : "ms-appx:///ProfileIcons/{9acb9455-ca41-5af7-950f-6bca1bc9722f}.png" item to use a Tux icon (default icons are here)
You should have something like this:
{
"$schema": "https://aka.ms/terminal-profiles-schema",
"profiles":
{
"list":
[
// ...
{
"guid": "{1d43c510-93e8-4960-a18b-e432641e0930}",
"name": "ssh my-server",
"icon" : "ms-appx:///ProfileIcons/{9acb9455-ca41-5af7-950f-6bca1bc9722f}.png",
"commandline": "ssh me#my-server -p 22 -i ~/.ssh/id_rsa"
}
]
}
}
Save the configuration and enjoy the new item in the New Tab drop-down.

You can use native ssh client from Windows 10,
From powershell
Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'
# This should return the following output:
Name : OpenSSH.Client~~~~0.0.1.0
State : NotPresent
Name : OpenSSH.Server~~~~0.0.1.0
State : NotPresent
Install the OpenSSH Client
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
It should return the following output:
Path :
Online : True
RestartNeeded : False
Uninstall the OpenSSH Client
Remove-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
Add the hosts to your ssh config file
From your home folder, go to the .ssh/config file, the folder may not exist if the ssh application has not been used, so it will be necessary to create it on you home folder
C:\Users\%USERPROFILE%\.ssh
#Damo post a very good documentation about the ssh config.
e.g config
Host test
User test
HostName 127.0.0.1
Port 22
IdentityFile ~/.ssh/id_rsa
Windows Terminal
Similar to the #Himura instructions, but instead of using "bash.exe" you will using "ssh.exe".
For connection to the remote host, you can use the hostname from the.ssh/config file e.g ssh.exe test, if you don't want to use a config file, you can use the user#ip ssh.exe test#127.0.0.1 and the password dialog will be promt
Edit your profile.json from the settings on Windows Terminal,
Duplicate a profile
Change the "guid" value to a new GUID
Change the commandline value with ssh.exe, e.g "commandline" : "ssh.exe test"
Change the profile's "name"
e.g
C:\Users\%USERPROFILE%\.ssh\config
Host vagrant
Hostname 127.0.0.1
Port 2222
User vagrant
IdentityFile ~/.ssh/vagrant.key
profile.json
...
{
"acrylicOpacity" : 0.75,
"closeOnExit" : true,
"colorScheme" : "One Half Dark",
"commandline" : "ssh.exe vagrant",
"cursorColor" : "#FFFFFF",
"cursorShape" : "bar",
"fontFace" : "DejaVu Sans Mono for Powerline",
"fontSize" : 10,
"guid" : "{1777cdf0-b2c4-5a63-a204-1111f349ea7c}",
"historySize" : 9001,
"icon" : "ms-appx:///ProfileIcons/{9acb9455-ca41-5af7-950f-6bca1bc9722f}.png",
"name" : "Vagrant",
"padding" : "0, 0, 0, 0",
"snapOnInput" : true,
"startingDirectory" : "%USERPROFILE%",
"useAcrylic" : true
}
....
If you want to set the new entry as default, search for the defaultProfile key
....
"globals" :
{
"alwaysShowTabs" : true,
"copyOnSelect" : false,
"defaultProfile" : "{1777cdf0-b2c4-5a63-a204-1111f349ea7c}",
"initialCols" : 120,
"initialRows" : 30,
....

If you want to stay in the terminal and easily manage all your ssh connections inside WSL then i would recommend using the built in ssh config management in the ssh command.
Basically you put all your different ssh configurations in to the file ~/.ssh/config
There is a good post documenting the basic use of this here
Hope this helps.

If you want to connect to a machine on Google Compute Engine using Windows Terminal, you can write a script to replace the default command and use ssh instead of putty.exe. More details here.

Related

shell script skipping EOF when boot

I added some shell scripts changing some contents of file in /etc/init.d directory.
#!/bin/bash
set -x
DBMS_ID='testuser'
DBMS_PW1='*****'
CONFIG_EXEC="./some_program"
${CONFIG_EXEC} config <<EOF
yes
127.0.0.1
${DBMS_ID}
${DBMS_PW1}
yes
EOF
cp -f A.txt B.txt
so I expected result below
DB configuration...
HOST : 127.0.0.1
USER : testuser
PASS : ********
PORT : 3306
DB : mysql
UNIX_SOCKET : /tmp/mysql.sock
TESTING CONFIGURATION ...
[ERROR] - Mysql connect error[1045] : Access denied for user 'testuser'#'127.0.0.1' (using password: YES)
Do you want to setup database. [yes/no] : HOST : USER : PASS :
PORT (Default: 3306) : DB (Default: mysql) : UNIX_SOCKET (Default: /var/lib/mysql/mysql.sock) :
TESTING CONFIGURATION ...
[OK]
Done.
+ cp -f A.txt B.txt
but I got some error when reboot
DB configuration...
HOST : 127.0.0.1
USER : testuser
PASS : ********
PORT : 3306
DB : mysql
UNIX_SOCKET : /var/lib/mysql/mysql.sock
TESTING CONFIGURATION ...
[OK]
Do you want to setup database. [yes/no] : HOST : USER : + cp -f A.txt B.txt
some content in EOF block skipped.
why this problem happened?
and How can i fix it?
I found why this happened.
My program (some_program in question) is made with C++, and it uses the tcgetattr function to hide the input password.
On boot, this function leads to the error below, so it looks like skipping. Actually, the problem is caused by this program.
Inappropriate ioctl for device
So it is a different problem with shell.
If someone suffered this problem, or a similar situation, check if some program or script uses tcgetattr or stream-related functions.

Show hostname for local host with powerline

I am using powerline with bash, fish, and tmux. The hostname shows up when logging in to remote systems with SSH. But I want to also enable the hostname segment for local users. Here is how it looks on a remote system.
Here is how it looks on as a local user. Note that I have hostname set on the local machine.
The tmux theme works fine.
I have tried editing the default theme by omitting the priority to always show the hostname segment disregarding the display width, and also edited the "only_if_ssh": false argument. How can I enable showing the hostname in the shell prompt as well?
I had to change the following config files to show the hostname for a local system.
Change powerline/config.json to make the key ext:shell:theme point to my custom theme .json file
Customize the theme file to your liking. I have the following in addition to other entries.
{
"segments": {
"left": [
{
"function": "powerline.segments.common.net.hostname",
"priority": 10
}
}
}
Edit __main__.json as follows.
{
"segment_data": {
"hostname": {
"args": {
"only_if_ssh": false
}
}
}
}

Windows terminal ubuntu how to change starting directory

I am wondering how to change the starting directory of my windows terminal from /home/user/ to C:/Users/user. I tried a few things I found, but nothings works. This is my current profile specified in the settings.json file:
{
"acrylicOpacity" : 1,
"closeOnExit" : true,
"colorScheme" : "One Half Dark",
"commandline" : "ubuntu",
"cursorColor" : "#FFFFFF",
"cursorShape" : "bar",
"fontFace" : "Consolas",
"fontSize" : 10,
"guid" : "{ba50f801-2d96-4517-a737-575f32f0fb61}",
"historySize" : 9001,
"icon" : "C:/Users/user/Pictures/ubuntu.png",
"name" : "Ubuntu",
"padding" : "0, 0, 0, 0",
"snapOnInput" : true,
"startingDirectory" : "C:\\Users\\user",
"useAcrylic" : true
}
but when I open the terminal the prompt is user#laptop:~$ and pwd gives /home/user/
If you mean wsl, you can set startingDirectory just like this:
"startingDirectory": "//wsl$/Ubuntu/home/user"
You can explore the path \\wsl$ in Windows Explorer.
This works for me (on Windows Terminal 1.0.1401.0, WSL2 and Ubuntu 20.04):
"startingDirectory": "C:/Users/user"
The issue was that there was an auto-generated ubuntu profile in addition to the one I created. I started using the auto-generated one and removed the "commandline" : "ubuntu" option and it now listens to the startingDirectory
for me it worked when i added the version number: //wsl$/Ubuntu-20.04/home/username
To change the starting directory of windows terminal:
Open windows terminal settings
Go to Ubuntu profile page and change "Starting Directory" to \\wsl$\Ubuntu\home\<user>
Note: I posted another answer cause it did not work for me using forward slashes

Multiple script commands through SSH on Juniper OS

My question is concerning networking equipments, especially Juniper OS.
I would like to execute a lot of commands through SSH on the switch.
And not manually, with a script.
But, when I push a command through SSH (example : 'configure') to manipulate software configuration, it changes the prompt indeed.
And the next command, available only in this level of configuration, doesn't work, because the level up is for sure not registrated since last command, so new command = come back to inital prompt.
Example in Ruby with net/ssh :
ssh = Net::SSH.start("X.X.X.X", LOGIN, :password => PASSWORD)
ssh.exec!("configure") # -> Entering configuration mode
ssh.exec!("set system services telnet") # -> error: unknown command: set
ssh.close
On Juniper ILC, there isn't '&' or ';' to add mutlipe commands.
Is it possible to insert a carriage return in this kind of command and then put all commands in one request ?
Otherwise how can I execute several commands, keeping the link between them ?
Thanks in advance.
Ok, the only solution I found is to concatenate the instructions into one connection.
Example :
ssh.exec!('configure;
set system services telnet;
delete system services web-management;
set system login class READ permissions view-configuration;
set system login class READ allow-commands show;
...
commit;')
Hope this will help somebody, don't hesitate to improve it !
I know it's about Ruby but i hope my investigation results in Java could be useful. I used construction like this in eBay/parallec(i think in JSch it'll work too) :
public static void sshVmWithPassword() {
ParallelClient pc = new ParallelClient();
pc.prepareSsh().setTargetHostsFromString(HOST)
.setSshCommandLine("show version;\nshow log")
.setSshUserName(USERNAME)
.setSshPassword(PASSWORD)
.execute(new ParallecResponseHandler() {
public void onCompleted(ResponseOnSingleTask res,
Map<String, Object> responseContext) {
System.out.println("Responose:" + res.toString()
+ " host: " + res.getHost()
+ " errmsg: " + res.getErrorMessage());
}
});
pc.releaseExternalResources();
}

Can I ask Windows what permissions I need to perform an action? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Is there a way to say "I'm about to issue some commands, and if they can't run figure out why, and fix it." Ideally it would give me a simple Y/N option to fix it (either one time or forever). Or some override like how run as administrator is supposed to work that just skips all permissions checking? Or a way to turn permissions checking off?
Something like this:
C:> delete printer GHI
You can't delete printer WXY because 1) you don't have permission XYZ, and 2) you aren't a member of group WXY, 3) there are 4394568 unprinted jobs in the queue for the GHI printer, and 4) you don't have ABC to do DEF and 5) your JKL is set to MNO.
Would you like Windows to grant you permission to XYZ, add you to membership of group WXY, and give you ABC to do DEF, set your JKL to PQR and delete the 4,394,568 unprinted jobs, and remove the printer GHI? [O/Y/N] ([O]ne time only, [Y]es permanently, [N]o)? Y
Provide an administrator username and password for domain GHI.
Username: GHI/Administrator
Password: password
Printer GHI has been deleted. Have a nice day.
C:>
To get history and command line editing features like in Bash, check out the PSReadline module. PowerShell already has pretty good tab-completion but PSReadline makes it better.
BTW PowerShell isn't so much about working with APIs as it is about working with objects. Managing printers is pretty straight forward:
8> Get-Printer
Name ComputerName Type DriverName PortName Shared Publishe
d
---- ------------ ---- ---------- -------- ------ --------
Send To OneNote 2013 Local Send to Microsoft OneN... NUL: False False
Quicken PDF Printer Local Amyuni Document Conver... NUL: False False
Microsoft XPS Document Writer Local Microsoft XPS Document... PORTPROMPT: False False
HP Photosmart 7520 Local HP Photosmart 7520 ser... 192.168.1.127_1 False False
hp LaserJet 1300 PCL 5 Local hp LaserJet 1300 PCL 5 DOT4_001 True False
Fax Local Microsoft Shared Fax D... SHRFAX: False False
9> Remove-Printer 'Microsoft XPS Document Writer'
Remove-Printer : Access was denied to the specified resource.
At line:1 char:1
+ Remove-Printer 'Microsoft XPS Document Writer'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (MSFT_Printer (N...= "", Type = 0):ROOT/StandardCimv2/MSFT_Printer) [R
emove-Printer], CimException
+ FullyQualifiedErrorId : HRESULT 0x80070005,Remove-Printer
Fair point on having more helpful error messages. This is something the product has been getting better at e.g.:
10> Invoke-Command -ComputerName . {Get-Service spooler}
[localhost] Connecting to remote server localhost failed with the following error message : The client cannot connect
to the destination specified in the request. Verify that the service on the destination is running and is accepting
requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly
IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and
configure the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help
topic.
+ CategoryInfo : OpenError: (localhost:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : CannotConnect,PSSessionStateBroken

Resources