run bash script in PowerShell with ssh fails with invalid format - bash

I created a simple bash script that runs fine on Ubuntu 18/20.
Decided to port it onto PowerShell.
I start PowerShell in Windows 10.
Then type: ssh 192.168.1.56
This allows me to reach the target.
(the key is located in /c/users/joe90/.ssh/)
On the other hand, the bash script does the same thing:
#!/bin/bash
ssh 192.168.1.56
Yet, I keep getting this error:
load pubkey "/c/Users/joe90/.ssh/mykey-xyz": invalid format
The only thing I was able to sort out is that typing from PowerShell:
ssh -V
return ==> OpenSSH_for_Windows_7.7p1, LibreSSL 2.6.5
But when the myBash.sh bash script runs (/usr/bin/bash ...):
ssh -V
return --> OpenSSH_8.3p1 ...
Any thoughts ?
Additional Notes:
The answer seems to lie here. It does ssh onto target but always leave this error mentioned. I tried to make a public key with no success.

Run
/usr/bin/ssh 192.168.1.56
instead (assuming this is the one you want to run).
Verify it using
/usr/bin/ssh -V
form bash, or
sh -c '/usr/bin/ssh -V'
from powershell.

Any time I see "invalid format" while using Windows Subsystem for Linux, I run dos2unix on that file and that seems to clear up a lot of errors. In WSL you just type: dos2unix fileName.txt

Related

ssh-copy-id on windows doesn't work: No such file or directory

I want to copy public keys to my server from a windows system.
And the problem I have is, that I don't get the path to the key.
I've tried things like:
ssh-copy-id -i C:/Users/username/.ssh/mykey.pub user#serverIP
ssh-copy-id -i ~/.ssh/mykey.pub user#serverIP
The result is always
No such file or directory
What am I doing wrong? I'm using Git Bash for this on Windows 10
Git's ssh is a version of OpenSSH. You can confirm it by running ssh -V under path\to\git\usr\bin. Here is what it evals on my machine:
OpenSSH_7.7p1, OpenSSL 1.0.2o 27 Mar 2018
ssh-copy-id script internally executes some *nix shell command (like exec, cat, etc. You can find more by opening the one under path\to\git\usr\bin in text mode), so it works only against *nix machines. That's the reason why ssh-copy-id under path\to\git\usr\bin is not a executable file.
According to this issue of PowerShell/Win32-OpenSSH, ssh-copy-id is not supported on Windows.
However, there are some alternative ways to do the same thing:
A powershell version of this answer can be
Get-Content $env:USERPROFILE\.ssh\id_rsa.pub | ssh <user>#<hostname> "cat >> .ssh/authorized_keys"
There is also a python script to do the same thing: ssh-copy-id for Windows.

Running bash command through ssh that only exists on remote

I am writing a bash script that involves ssh-ing into a remote host and running commands there. That in itself is not a problem. The issue is that I want to run a command which doesn't exist locally, only on the remote. The script fails with bash: line 1: type: remote_only_command: not found, even though it's successfully connecting to the remote host and can run basic commands without issue.
I can run the command on the remote host if I ssh in and run it manually. I've tried writing a separate bash script on the remote host and running that through the script (sh remote_script.sh), but that gets the same command not found error.
ssh $REMOTE var=$var 'bash -s' << 'EOF'
ls # works no problem, lists files on the remote server
remote_only_command # bash: line 1: type: remote_only_command: not found
EOF
Is it possible to run a command that is only accessible from the remote host and not locally where the script is being run?
I think this is the way it should work, as the command is only executed on the remote host. But i suspect your problem is the environment, which is NOT permitted over ssh. Try to use the complete path to the command, eg:
/path/to/remote_command

Command doesn't run from shell script on remote windows machine: Unable to execute command or shell in remote system

I have a Windows Client machine and I would like to invoke a powershell script on that client from my local machine i.e. Mac.
I have ssh on Windows via freesshd.
Also, i need to provide password via script hence I ended up using sshpass.
From my Mac Terminal, while the command:
sshpass -v -p xxx ssh administrator#x.x.x.x "powershell.exe dir"
runs perfectly and returns the content of whatever directory the shell lands in, i am unable the get the same result from inside a shell script.
The simple script is as below:
cmd='sshpass -v -p xxx ssh administrator#x.x.x.x "powershell.exe dir"'
echo `$cmd`
What I get is:
Unable to execute command or shell on remote system: Failed to Execute
process.
I have tried various permutations of " and ' to get the correct command string but to no avail.
What is wrong here?

How can I run a shell script via SSH such that the environment of the remote computer is similar to that of the local computer?

I welcome rephrasing of my question, because I'm not sure exactly what the problem is called.
I am trying to run a shell script via SSH using a command of the following form:
ssh -o StrictHostKeyChecking=no lxplus0035 "cd ~/test; bash script1.sh"
When I do this, the complex script script1.sh breaks with many syntax errors, operand errors and other errors. An example is as follows:
stty: standard input: Invalid argument
The script works fine when run directly, not via SSH, in the local system and in the remote system (when connected in an SSH session). Why might executing the script via SSH cause these problems?
The stty problem is not related to your environment, it's a result of the SSH command not allocating you a TTY (What is Pseudo TTY-Allocation? (SSH and Github)). Adding -t argument for ssh should fix it.
Further information:
http://go2linux.garron.me/linux/2010/11/ssh-t-open-pseudo-tty-run-commands-remote-server-809/
http://capistranorb.com/documentation/faq/why-does-something-work-in-my-ssh-session-but-not-in-capistrano/

Continuing shell script execution after SSHing into guest machine?

I have an Ubuntu guest box setup on my Windows host using Vagrant and VirtualBox. I'm trying to write a shell script that will...
vagrant up
vagrant ssh once vagrant up is complete
cd into a specific project directory in the guest machine once successfully SSHed into the guest machine
Right now my vagrant_shell_script.sh file contains the following:
vagrant up && vagrant ssh && echo 'cd vagrant/rails_tutorial/sample_app'
Everything works fine when I execute it in Git Bash, up to and including connecting via SSH to the guest machine, however after it successfully connects, the script seems to stop working and does not execute the final cd command. I presume this is because it is no longer able to communicate directly with my host machine through that particular Bash instance (please correct me if I'm wrong).
Is there any way to have it navigate directly to the target directory once the SSH connection is successful?
Please forgive me if this is a dumb question--relatively new to bash scripting.
This solved it. It's kind of hacky, but running
vagrant up && vagrant ssh -- -t 'cd /vagrant/rails_tutorial/sample_app; /bin/bash' gets you in. For some reason vagrant keeps kicking you out if you don't launch the shell.
vagrant ssh -- allows you to pass commands into the SSH client. This is vagrant's own utility. The next flag, -t is an SSH flag and it allows SSH to execute certain commands before it hands control back to you. You put your command after the -t flag, but make sure to end it with <last command> ; /bin/bash so that it launches a shell for you and you don't get kicked out.
you can also use Heredoc to run the commands after you ssh by using something similar to this in your script:
# Use heredoc to send script over ssh
$ssh_cmd << 'END_DOC'
cd <path>
commands
exit
END_DOC
echo $ssh_cmd

Resources