Unknown hostname error when running SCP from Powershell script - powershell

I am trying to write a Powershell script that copies files from a Windows PC to a Linux box.
I am able to SSH into my remote Linux machine from a Powershell window using SSH keys (stored in %userprofile%/.ssh/ on the Windows machine. I can also run an SCP command from the terminal window copying files from the Windows machine to the Linux box.
However, the very same scp command fails when run from a script (script.ps1) with the following error:
ssh: Could not resolve hostname <hostname>: No such host is known.
lost connection
Anyone know why scp cannot resolve the hostname when run as a script in Powershell, when it can from the terminal?
The scp command I am running looks like:
& C:\Windows\System32\OpenSSH\scp.exe -r 'bin/release/net7.0/linux-x64/publish' <user_name>#<comp_name>:/home/<user_name>/containers/net_ws/app
As an aside, I tried to ping the <comp_name> from within the same script and it pings successfully, so there appears to be no issue resolving the comp_name host within the powershell context.

Problem solved, thought full explanation is lacking.
I have solved the script issue by replacing the comp_name with the ip address of the device.
& C:\Windows\System32\OpenSSH\scp.exe -r 'bin/release/net7.0/linux-x64/publish'
<user_name>#<ip_address>:/home/<user_name>/containers/net_ws/app
I'm still curious as to why scp is unable to resolve the ip address from the host name in this instance - i.e. when running as part of a script, rather than in the terminal, especially since other executables running in the same script context are able to find the host.

Related

PsExec connects using system name but not ipAddress

I need to use my local computer to simulate a test stand which will be on a domain and access a remote computer which is on a workgroup using PsExec. The testing computer is built from an imaging tool. The IP will be the same every time but the name isn't. The process I'm working with was used on an embedded XP system and is now being upgraded to WIN10. I've added network security using GPO and have found workarounds to be able to open the connection but for some reason just trying to run cmd on the remote machine does not work when using the IP, only the name. Using the IP returns the "access is denied" error. I have already added the token filter key to the registry. Has anyone heard of something like this before?
I have a script I'm trying to run but in the meantime I'm just trying to get
psexec \IP_ADDERESS -h -u USER_NAME(this is an admin) -p PASSWORD cmd
edit: I have to keep my computer on a domain but I have a spare that I was able to put on a workgroup with the test system. Running psexec went perfect. It makes no sense why it works for the name and not ip on a domain->workgroup connection and works exactly how I need it to on a workgroup->workgroup connection.

bash: powershell: command not found

For a node project I will have to work on a remote server and for that I will use filzilla for file transfer and ssh for console.
I use Visual Studio Code, I installed a remote ssh extension,
I did the hostname and user configurations as well as secretkey,
because before that I had another error The process tried to write to a nonexistent pipe ,
which I finally solved but now after I start ssh and log in with the password in cmd from Visual Studio Code, I get this error bash: powershell: command not found.
I tried one day to search the internet for that error.
https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_server_configuration?WT.mc_id=-blog-scottha
https://learn.microsoft.com/en-us/windows/wsl/troubleshooting
I've talked to people who have more experience but within the time limit my advice could be, but I still couldn't solve the error, either it's something from windows or ssh or I don't know.
You are the first time I try to do this in general I used heroku or aws that had some facilities.
I changed host machine in the settings JSON file windows to linux
When you got the option to Select Linux, Windows or Mac did you make sure that you know exactly the type of server you are connecting to. Bcs if you choose the wrong one this error gets thrown.

VSCode: how to ssh remote connect to remote WSL2

Is it possible in VSCode to edit files within WSL2 of a remote PC. (This is like a combination of Remote-WSL and Remote-SSH.) I can connect to that remote PC via ssh and RDP.
The path \\wsl$\ does not seem to be available in my remote-ssh connection.
PCA - me, local, VSCode
^
|
ssh and/or RDP
|
V
PCB - remote, WSL2
(I currently have Windows OpenSSH set up on the remote PC, with the default CMD shell. I tried setting the shell to Bash but then the remote extension wouldn't install.)
Update Setting PC-B's ssh server shell to bash.exe does solve all my problems. I'm not sure why it didn't work before. See my answer below for more details.
Answering my own question I was certain I'd tried this before and it had not worked, but now that I try it again it works. Perhaps I just needed a reboot of Windows and WSL2.
Enable Windows SSH server on PC-B and set the shell to be bash.exe
# Powershell as Administrator
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Set-Service -Name sshd -StartupType 'Automatic'
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\WINDOWS\System32\bash.exe" -PropertyType String -Force
If you want authorized_keys-based login to this PC and your account is an Administrator account, you will need to follow the special instructions here: https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_keymanagement (I have heard of having to manually fix the administrator_authorized_keys file permission at some point too. https://stackoverflow.com/a/64868357/600360 )
Using Remote-SSH connect VSCode from PC-A to PC-B (using Windows credentials) and tell VSCode that it is a linux server (because you're connecting to bash.exe within WSL2).
If you're behind a proxy server, add the proxy to your ~/.wgetrc.
Voila.
These steps taken from THE EASY WAY how to SSH into Bash and WSL2 on Windows 10 from an external machine where you can find more details.
If you want to connect to remote WSL, you probably should set it up to run own sshd instead of relying on hosting Windows to do the tunneling.
If I have got your problem statement right, the flow seems to be outlined in one of VS Code blog posts. Here I'll mention steps I think you will need to get to your target state.
Firstly, disable sshd on remote Windows so it does not occupy port 22. then, install and start sshd inside PCB:
# from PCB command prompt
# something like that, depending on your choice of distro
sudo apt remove openssh-server && sudo apt install openssh-server
# this would again depend on your chosen distro
sudo /etc/init.d/ssh start # after i do this - windows pops up a firewall prompt to allow me create a rule. you might need to add it manually
then you'd need to either enable password logins via ssh or (preferably) generate a key pair and put your public key into /home/your_name/.ssh/authorized_keys on PCB.
Assuming you have OpenSSH for Windows installed (this seems to be the client Remote SSH relies on), on your PCA follow something like so:
# something along these lines on your PCA
PS C:\WINDOWS\system32>ssh-keygen
# note location of .pub file and copy its contents into remote ./ssh/authorized_keys
# add generated private key to ssh-agent service
PS C:\WINDOWS\system32> Start-Service ssh-agent # if this fails - ensure service is installed and enabled
PS C:\WINDOWS\system32> ssh-add path\to\your\private_key # ensure you have dropped all permission except your own user
Watch out for permissions:
ssh keys are considered secret, so neither client nor server would start unless you drop all permissions from your key material. On linux do chmod 600 .ssh/authorized_keys, and for windows ssh-agent follow instructions from this SE answer.
The above may seem a bit daunting but is in fact very standard SSH setup procedure
easy steps
just update your vscode to the latest version
install Remote Development extension pack
allow WSL2 connection in the settings
if my answer helped you upvoted :D
I know it's been a while since the post, but here is a useful answer that uses Remote -- Tunnels extension.
https://stackoverflow.com/a/75389647
It appears that, for now, you need to manually install the CLI in WSL in order for it to work. The downloads can be found here. Since you are on Windows, I would recommend the x64 CLI download. When you extract the tar.gz file, you will get a file named code. I would recommend moving it to your home directory for ease. To open the tunnel, run ./code tunnel (from wherever you put the code file) to force the new CLI. If you use code tunnel it will still use the Windows version.
Source: https://github.com/microsoft/vscode/issues/171196

ssh not working for Amazon EC2 server

I have setup and launched an instance of Amazon EC2 server with Ubuntu in it. Now I have integrated cygwin with command prompt also so all linux commands are working in command prompt.
I tried to access the server using ssh -i munish.pem ubuntu#52.11.190.155 (munish.pem contains my secret key).
After running this command I get an error: 'ssh' is not recognized as an internal or external command, operable program or batch file. I searched net and could find solution for github not for Amazon EC2 service...
You can use putty software in window for connecting to the AWS EC2 instance.
Follow the below steps:
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/putty.html
Hope it helps..
You can open PowerShell and check ssh command is available or not.
If not, you can install OpenSSH in Windows 10. See following guide on how to install it.
https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse

How to uninstall a service on remote machine using powershell?

I am a newbie in powershell script. I want to uninstall a service on remote machine and re-install it again. I am able to stop the service, change the files that service is using and again start it.
But, now I want to completely uninstall it.
One more thing, I will be executing these scripts via TeamCity.
I have tried following ways, but each one is giving trouble:
using wmi class
running installutil command on remote machine. But, cant execute this on remote machine since there is problem in accessing that service.
Using Get-WmiObject command and then calling delete() on retrieved service.
Is there any promising solution ??
Any help will be appreciable..
After playing a lot with TC and many commands, finally I got my answer to uninstall a service on remote computer, and its working properly.
I have made use of "wmic" command and provided the remote computer name, username and password for /node:, /username:, and /password: parameters. And, provided 'installutil.exe' command to be executed remotely. Here, the actual command is..
"wmic /NODE:$serverName /USER:$userName /PASSWORD:$password process call create '$path_to_installutil_exe\installutil /u $exe_location_path_of_service'"