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

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?

Related

How to ssh into remote linux server using username / ip address and password from window using shell script?

What I am trying to do is to login to remote Linux server using SSH from my Windows machine by running a shell script via git bash.
I would like to write a script which will be used by an user with basic IT knowledge. This script will execute a bunch of commands on the remote machine, so it does need to establish a SSH connection.
What I have tried to write in this script so far is:
ssh username#ip <password>
EDIT: You should consider installing Jenkins on the remote system.
Running a command on a remote system can be done via:
ssh user#host command arg1 arg2
If you omit the password, a password prompt will appear, to get around the password prompt, you should consider setting passwordless SSH login. https://askubuntu.com/questions/46930/how-can-i-set-up-password-less-ssh-login
Rephrasing what you said, you want to write a script (namely script1.sh) which does the following (in this order):
Start a ssh connection with a remote
Execute some commands, possibly written in another script (namely script2.sh)
Close the connection / Keep it open for additional command line commands
If you want to put the remote commands on script2.sh instead of listing them in script1.sh, you need to consider that script2.sh must be on the remote server. If not, then you may consider to delegate to script1.sh the creation/copy of script2.sh on the remote machine. You may copy it in a temporary folder.
In this case, my suggestion is to write script1.sh as follows:
Copy of script2.sh with
scp /path/to/local/script2.sh user#host:/path/to/remote/script2.sh
Switch bash sheel to remote shell with
ssh user#host
Execution of script2.sh
sh /path/to/remote/script2.sh
Instead, if you prefer to list everything in just one script file, you may want to write:
Switch bash sheel to remote shell with
ssh user#host
Execution of commands
echo "This command has been executed on the remote server"
echo "This command has also been executed on the remote server"
..
Possibly closing the SSH connection to prevent that the user execute additional commands
You may consider to copy the ssh-keys on the remote server so to avoid password prompts. More information here.

run bash script in PowerShell with ssh fails with invalid format

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

SSH to remote Windows machine using username and password through shell script

I am trying to SSH to a remote Windows machine (an AWS ec2 instance) by using an username and password of the remote machine. I need to automate this connection to run some remote commands from my script (either shell or Power shell) without prompting me for a password, My script shouldn't fail by expecting a password to be prompted
I don't want to use sshpass or any generated keys (by using ssh-keygen). Since the source machine where I run this command/script is not a dedicated machine, I may run it on a different machine everytime. I also gave a try to connect using the .PEM file provided by AWS as below (thought it could be easy while using it the script).
$ssh -i aws_keypair.pem Administrator#10.10.10.10
Administrator#10.10.10.10's password:
It is still expecting me for a password even if I used the .PEM file, I also tried to created an file 'authorized_keys' in the remote Windows machine under the path "C:\Users\Administrator.ssh\". Still it is prompting me for a password.
Expectation :
Connect to remote Windows machine using PEM file and run some remote commands.
(or)
It shouldn't prompt me for a password while I try for the connection from some script (shell/power shell).
Can be done without any 3rd party tools like this:
$env:TMPPW=Get-Content -Path 'secure_file.txt' ; $un='MyUserName'
$j=Start-Job -ScriptBlock{Start-Sleep -Seconds 1
(New-Object -ComObject wscript.shell).SendKeys("$env:TMPPW{ENTER}")}
& ssh.exe -q -4 -l $un 127.0.0.1 'whoami'
$env:TMPPW=([guid]::NewGuid()).Guid ; $env:TMPPW=$null
I am able to achieve this using Plink command (Installation of Putty is required on source machine to execute this command). So now, I am able to successfully pass the username and password with in the script (Shell script) and everything is working as expected.
FYI, I am pasting the exact command which worked for me
$echo y | plink -ssh Administrator#10.10.10.10 -pw abc123 "dir"

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

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/

Resources