How to execute a multiline command using ssh on a remote Linux machine? - powershell

My local machine has Windows OS. My remote machine has Linux OS. I'm trying to run some bash commands on the remote machine from my local machine (with the use of ssh). In the powershell script below I'm feeding a multiline comment to stdin input of ssh client. The code does not work and gives me an error. The problem is that each line passed to bash has \r (carriage return symbol) appended to it and bash interprets it as a part of a command. What are my options here?
Powershell script
ssh -tt "$Username#$Host" #"
cd /home/someuser/project
cd ~
"#
Bash error
bash: line 0: cd: $'/home/someuser/project\r': No such file or directory

I assume you execute ssh from Windows PowerShell or PowerShell Core on Windows as it is using CRLF for line breaks. bash doesn't like CRLF. So, concatenate your commands with line delimiters and pass it to ssh in one line:
ssh -tt "$Username#$Host" "cd /home/someuser/project; cd ~"
If it is a larger script, you can maintain it in a file an convert it automatically to a (not really) "one-liner":
$myBashScript = (Get-Content 'my-bash-script.txt') -join ';'
ssh -tt "$Username#$Host" $myBashScript

Related

Sqlplus version not printing in tempfile

I am trying to execute below unix script to fetch the oracle version from windows command prompt using ssh command to connect to unix terminal
#!/usr/bin/ksh
Freport=/tmp/test.txt
cd /usr/oracle2/product/11.2.0/bin
echo "Begin" > $Freport
set +x
/usr/oracle2/product/11.2.0/bin/sqlplus -V >> $Freport
set -x
pwd >> $Freport
echo "Completed" >> $Freport
Windows command prompt output
Begin
/usr/oracle2/product/11.2.0/bin
Completed
Same script ran in unix server and got below output
Putty output
Begin
SQL*Plus: Release 11.2.0.1.0
/usr/oracle2/product/11.2.0/bin
Completed
I want to print the oracle version in my windows command prompt output. Kindly help me to resolve it
Windows cmd prompt execution
C:\programfiles\PUTTY>putty.exe -ssh uname#ip -pw pwd -m windowsscriptpath/test.sh
Can someone please help me with this? I got struck here.. Is there any option to execute the unix server script directly from windows since shell script is working fine in unix server –

Input to psexec command

I am trying to run a batch file which is placed in a remote windows server from my local system.
For that I am using psexec command as shown: psexec \\ip address -u user\username -p password cmd /c "path to batch file". This is executing same way as it executes on remote windows server. but at some point we have to press Q on the command prompt to get to next line on the batch file.
this is how it looks:
on Remote system
But I am unable to press Q on my local psexec command prompt, this is how it looks:
on my local system
is there a way to take key inputs for psexec command?
Thanks in advance.
I tried providing -accepteula option and also pipe it with powershell and try running it.
But no luck on that.

Execute script file over ssh from powershell on windows

There is this solution to execute a shell script from a file. All good.
How to use SSH to run a local shell script on a remote machine?
On Windows in cmd these 2 commands do the same and they both work
cat .\my_script.sh | ssh user#ssh.server.com "bash -s"
ssh user#ssh.server.com "bash -s" < .\my_script.sh
How can I run the same in powershell in a similar way(using pipes) and not using any Get-Ouput and weird powershell commands?
If I run this script over powershell I get an error
cat .\my_script.sh | ssh user#ssh.server.com "bash -s"
bash: line 3: $'\r': command not found
' does not existuru
bash: line 6: $'\r': command not found
bash: line 12: $'\r': command not found

PSQL -f Parameter Hanging on Windows 8.1

I've been using PSQL 14 on my Windows 10 desktop with Git Bash for a while now without issue. Recently I've had to transition to a Windows 8.1 laptop, and I've come across a problem with running the filename parameter for PSQL. When attempting to run a SQL file with the line psql.exe -U <user> -f src/sql/test.sql the terminal hangs until I use Ctrl+C to exit the command. I can run psql -U <user> and then copy & paste the SQL file text into the terminal to get the results I want, but I don't get why this issue is happening in the first place.
I've checked my PATH environment variables and I do have both the /bin and /lib paths in there. I have also tested changing -f with the < operator, which didn't change anything. Running PSQL on Windows 8.1 isn't an issue, it's just this particular command.

Docker-machine ssh displays junk characters on windows 7

I have installed docker-machine on 64 bit Windows 7 machine. It also installed Oracle Virtual Box to which docker-machine connects to. The issue is that it is getting very difficult to work on docker-machine. Reason is ssh-ing to virtual machine displays a lot of junk characters. Below is what I get when I just vi newfile. Similar junk characters on cat existingfile. Or if I click on backspace to delete any character on command prompt.
I was having the same issue. I came across this page during my search which prompted me to try using bash.
Install git if you do not already have it. You can then run the following command from PowerShell to drop into a bash shell (assuming default location for git).
& "C:\Program Files\Git\bin\bash.exe"
I have created the alias 'bash' for this in my PowerShell profile folder, which you can find from here.
New-Alias bash "C:\Program Files\Git\bin\bash.exe"
Now drop into a bash shell first before using docker-machine ssh and there should be no more junk characters.