inserting the Linux shell script file from windows to Linux box using powershell - linux

so i am working on power shell and created the shell script file in C drive now i want to send that file using plink.exe by using power shell to Linux box.
Tried below commands
PS C:\> .\plink.exe -pw -i "R***t" root#192.168.1.12 ".\adduser.sh"
Unable to open connection:
PS C:\> .\plink.exe -i ssh "R***t" root#192.168.1.12 ".\adduser.sh"
PS C:\> .\plink.exe -i ssh "R***t" -P 22 root#192.168.1.12 ".\adduser.sh"
Unable to open connection:
PS C:\> .\plink.exe -i ssh "R***t" -P 22 root#192.168.1.12 ".\adduser.sh"
Unable to open connection:
basically how we can connect to Linux box usin g plink.exe through power shell
if we need any sshkey let me confirm and how can we generate the ssh
key for plink.exe and what are the options need to use for ssh key
so if any valuable suggestions appreciable and thanks in advance.....

plink is only a command-line interface for putty. Similar to the ssh command in unix/linux. You cannot upload/transfer files using plink.
You can use the pscp to do it.
Here's a reference to how you can create SSH keys to connect to your server without using a password.
https://the.earth.li/~sgtatham/putty/0.67/htmldoc/Chapter8.html#pubkey

plink is one tool in the entire suite of Simon Tatham's SSH package called PuTTY.
PuTTY provides SSH capabilities to WIndows users, and it comes with a bunch of compiled command line tools.
Plink is the equivalent of the SSH (secure (remote) shell) command on a linux machine.
Pscp is the equivalent of the SCP (Secure CoPy) command.
Psftp is the equivalent of SFTP (Secure File Transfer Protocol) which uses the same transfer methods as SCP but with an FTP like comman line interface.
PuTTY itself is a graphical tool that uses the same code as plink to create remote shells or commands over SSH or Telnet protocols (and many more), but it's probably a bit much to go into right now.
From the looks of it, it seems you are trying to get a shell script copied to your Linux server, so you should not be using the command to open a shell, but rather use the command to do a secure copy.
In short, use PSCP, not PLINK. (and maybe read the manual on it, so you get the arguments right)

TO insert the shell script file from windows machine to Linux box you need to download the "plink" file from internet and keep it in C:\drive and run the command like below
PS C:\> .\pscp.exe .\abc.sh root#1.2.3.4.:/root
after that you can use the plink to connect the linux box by using below command
PS C:\> .\plink.exe -pw "L****e" root#1.2.3.4 -P 22 "ls"
here "-pw" means password and "-P" means port number

I have briefly tried "Post-SSH" third-party module to Powershell, and it looks quite good. :)
Find-module Posh-SSH
Find-module Posh-SSH | Install-module
After that you can
$session = New-SSHSession -ComputerName "1.2.3.4" -Credential (Get-Credential)
Invoke-SSHCommand -Session $session -Command 'ls -l'
And finally close the session again
$session | Remove-SSHSession
http://www.thomasmaurer.ch/2016/04/using-ssh-with-powershell/

Related

Using ssh to login to linux terminal from windows and run command in a logged in shell

First of all, this may seem like a duplicate question but I have searched stack overflow/various other forum sites and still haven't managed to find a solution.
A few example forum posts I have reviewed to prove I've done my research before asking a question:
https://superuser.com/questions/130443/remotely-run-script-on-unix-get-output-locally
https://linuxconfig.org/executing-commands-remotely-with-ssh-and-output-redirection
https://zaiste.net/posts/few-ways-to-execute-commands-remotely-ssh/
https://unix.stackexchange.com/questions/474533/get-output-of-this-command-from-another-server-via-ssh
Run ssh and immediately execute command
https://www.cyberciti.biz/faq/unix-linux-execute-command-using-ssh/
There's hundreds more but I won't include them all.
I essentially need a shell script to open a command prompt on windows, login to a remote linux system and run a command.
I am aware this can be done with the following:
start cmd /k ssh user#host ls
But the problem with the above is that the ssh connection is closed upon completion of the task.
I am also aware I can keep the ssh connection open by adding:
bash -l
in some cases.
For my use case, I need to run a launch file for ROS (robot operating system) and for this I need to see the output from the command.
And when attempting to run roslaunch launchFile.launch (in place of ls above):
start cmd /k ssh user#host "roslaunch launchFile.launch"
the command prompt returns
bash: roslaunch: command not found
I've obviously sanitised the specific name of my launch file but
roslaunch launchFile.launch
runs perfectly if I login to the linux PC first:
ssh user#host
then run the command.
I have achieved this exact use case on MacOS but I now need reimplement the same solution on windows:
osascript -e 'tell app "Terminal"
do script "ssh quantum#172.23.199.1 \n
roslaunch launchFile.launch"
end tell'
Thanks in advance for any help or advice.
Try this :
start cmd /k ssh user#host "/full/path/to/roslaunch launchFile.launch; exec /bin/bash"

establish ssh connection and execute command remotely [duplicate]

I wish to run a script on the remote system and then wish to stay there.
Running following script:-
ssh user#remote logs.sh
This do run the script but after that I am back to my host system. i need to stay on remote one. I tried with..
ssh user#remote logs.sh;bash -l
somehow it solves the problem but still not working exactly as a fresh login as the command:-
ssh user#remote
Or it will be better if i could include something in my script that would open the bash terminal in the same directory where the script was running. Please suggest.
Try this:
ssh -t user#remote 'logs.sh; bash -l'
The quotes are needed to pass both commands to ssh. The -t option forces a pseudo-tty allocation.
Discussion
Consider:
ssh user#remote logs.sh;bash -l
When the shell parses this line, it splits it into two commands. The first is:
ssh user#remote logs.sh
This runs logs.sh on the remote machine. The second command is:
bash -l
This opens a login shell on the local machine.
The quotes were added above to prevent the shell from splitting up the commands this way.

Execute a Linux application remotelly from PowerShell

I'm trying to execute a application console command (cslogin) on a Linux box from PowerShell. I used the SSH module from SSH.NET to access the Linux server. The following is a snapshot of my script, I'm able to establish the ssh session.
Import-Module SSH-Sessions
$user = "user"
$password = "pass"
$hostname = "192.168.1.X"
C:\plink.exe -ssh -l $username -pw $password $hostname "cslogin"
But once the cslogin command is executed the script hangs with the following message:
SEC054 A device has connected to, or disconnected from, a pseudo tty without
authenticating
At this point if I hit the enter key I am able to get the application prompt, which is what I want. I am trying to understand why does my script hangs and how do I get around this issue.
You only import the SSH-Sessions module, without actually using it. You then run PLink, that is not related to SSH.NET at all.
Pure SSH.NET solution is like:
Import-Module SSH-Sessions
New-SshSession -ComputerName "192.168.1.X" -Username "user" -Password "pass"
Invoke-SshCommand -InvokeOnAll "cslogin"
Other than that your PLink solution does the same. As it does not work, the SSH.NET solution above won't probably work either.
Your actual problem is that the application (cslogin) requires an interactive terminal (TTY). PLink by default does not allocate one. And I believe that SSH.NET does not either. With PLink, you can force TTY using -t switch:
$user = "user"
$password = "pass"
$hostname = "192.168.1.X"
plink.exe -ssh -l $username -pw $password $hostname -t "cslogin"
See Using the command-line connection tool Plink.
As far as I can see you're not actually using ssh.net here. Sure, you import the module but instead of using the New-SSHSession or Invoke-SSHCommand cmdlets you then run plink to connect and run cslogin. I confess I've not heard of that Linux command but bear in mind that plink expects to connect via SSH, execute a command and disconnect - so you won't get an interactive prompt.
You can either have plink run a script on the Linux server, or using the -m switch get plink to read a list of commands drone a file.
Alternatively you can setup a putty saved session then run plink to get interactive session but you might get some oddities in the powershell command window when it tries to interpret the output from plink.
for more info see this link

Putty as an interface between cmd and sh

What do I have:
a local Windows machine and a remove Linux server
a cmd script and an sh script on the local machine
a specific restriction to keep no scripts on the remote machine
What do I need:
the local cmd script to start an SSH client with specified parameters (ip:port, username#password), with that client executing the local sh script on the remote machine and passing the output back to the local machine, to the cmd script's STDOUT
Example:
whatever.cmd:
#echo off
client.exe -ip 192.168.1.1 -port 22 -username notroot -password mypwd -exec remote.sh >192.168.1.1_media.txt
Alternatively, if I convert the sh script to a set of commands sepated by ; symbol, the example may look like this:
#echo off
client.exe -ip 192.168.1.1 -port 22 -username notroot -password mypwd -command 'head `ps -aux`;df -h | grep media' >192.168.1.1_media.txt
Is there an SSH client that can be run in this way? If not, how can I make Putty do the job without using GUI?
Maybe plink is something for you. It is like putty but instead of keyboard and screen as input and output you can use pipes for input and output.
This way you can echo the lines of your script to the server.
Plink is downloadable at the putty site: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
There is also an extensive and detailed manual on that site.

Send commands to Linux terminal with PuTTY

How can I send a command (not to open a file) with PuTTY that will run on a Linux terminal?
For example:
putty.exe 10.31.2.121 -l root -pw password | echo "Hi"
So that I will see the "Hi" on the Linux console?
Use plink.exe (from the developer of PuTTY).
And do something like the following
C:\putty\plink.exe user1#192.168.0.1 -pw P#55W0rD!
-m command.txt
I took this from Automate Cisco SSH connections with plink on Windows.

Resources