Send commands to Linux terminal with PuTTY - linux

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.

Related

Executing SSH with the Linux/Unix at command

I place this ssh call in the following a shell script on our Linux box named "tstz" and then call it with the linux "at" command in order to schedule it for later execution.
tstz script:
#! /bin/ksh
/usr/bin/ssh -tt <remote windows server> pmcmds ${fl} ${wf} < /dev/null >/tmp/test1.log 2>&1
at command syntax:
at -f tstz now + 1 minute
The ssh call executes remote command as expected, but the ssh connection closes immediately before the remote command has completed. I need the connection to stay open until the remote command has completed and then return control to the tstz script with an exit status.
This is the error I get in the /tmp/test1.log:
tcgetattr: Inappropriate ioctl for device
^[[2JConnection to dc01nj2dwifdv02.nj.core.him closed.^M
NOTE: When using the "at" command to schedule tstz, if I don't use -tt, the ssh command will not execute the remoted command "pmcmds ${fl} ${wf}". I believe this is because a terminal is required. I can however run tstz from the Linux command prompt in the foreground without the -tt on the ssh command line and it runs as expected.
Any help would be greately appreciated. Thanks!
As I understand you need to specify a command to execute on the REMOTE machine after successfully connecting to the server, not on LOCAL machine.
I use following command
ssh -i "key.pem" ec2-user#ec2-XX-XX-XX-XX.us-west-2.compute.amazonaws.com -t 'command; bash -l -c "sudo su"'
where you should replace "sudo su" with your own command, I guess with "pmcmds DFD_ETIME wf_TESTa"
So, try to issue, maybe
/usr/bin/ssh -tt <remote windows server> 'command; bash -l -c "pmcmds DFD_ETIME wf_TESTa"'
P.S. I have discovered interesting service on google called "explainshell"
which helped me to understand that "command;" keyword is crucial inside quotes.

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

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/

node.js unavailable via ssh

I am trying to call an installation of node.js on a remote server running Ubuntu via SSH. Node has been installed via nvm.
SSHing in and calling node works just fine:
user#localmachine:~$ ssh user#remoteserver
(Server welcome text)
user#remoteserver:~$ which node
/home/user/.nvm/v0.10.00/bin/node
However if I combine it into one line:
user#localmachine:~$ ssh user#remoteserver "which ls"
/bin/ls
user#localmachine:~$ ssh user#remoteserver "which node"
No sign of node, so I tried sourcing .bashrc and waiting 10 seconds:
user#localmachine:~$ ssh user#remoteserver "source ~/.bashrc; sleep 10; which node"
Only node seems affected by this. One thing I did notice was that if I ssh in and then check which shell I'm in it says -bash whilst if I ssh direct it gives me /bin/bash. I tried running the commands inside a bash login shell:
user#localmachine:~$ ssh user#remoteserver 'bash --login -c "which node"'
Still nothing.
Basically my question is: Why isn't bash finding my node.js installation when I call it non-interactively from SSH?
Another approach is to run bash in interactive mode with the -i flag:
user#localmachine:~$ ssh user#remoteserver "bash -i -c 'which node'"
/home/user/.nvm/v0.10.00/bin/node
$ ssh user#remoteserver "which node"
When you run ssh and specify a command to be run on the remote system, ssh by default doesn't allocate a PTY (pseudo-TTY) for the session. Not having a TTY causes your remote shell process (ie, bash) to initialize as a non-interactive session instead of an interactive session. This can alter how it interprets your initialization files--.bashrc, .bash_profile, and so on.
The actual problem is probably that the line which adds /home/user/.nvm/v0.10.00/bin to your command PATH isn't executing for non-interactive sessions. There are two ways to resolve this:
Find the command in your initialization file(s) which adds /home/user/.nvm/v0.10.00/bin to your command path, figure out why it's not running for non-interactive sessions, and correct it.
Run ssh with the -t option. This tells it to allocate a PTY for the remote session. Or add the line RequestTTY yes to your .ssh/config file on the local host.

Best way to script remote SSH commands in Batch (Windows)

I am looking to script something in batch which will need to run remote ssh commands on Linux. I would want the output returned so I can either display it on the screen or log it.
I tried putty.exe -ssh user#host -pw password -m command_run but it doesn't return anything on my screen.
Anyone done this before?
The -m switch of PuTTY takes a path to a script file as an argument, not a command.
Reference: https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter3.html#using-cmdline-m
So you have to save your command (command_run) to a plain text file (e.g. c:\path\command.txt) and pass that to PuTTY:
putty.exe -ssh user#host -pw password -m c:\path\command.txt
Though note that you should use Plink (a command-line connection tool from PuTTY suite). It's a console application, so you can redirect its output to a file (what you cannot do with PuTTY).
A command-line syntax is identical, an output redirection added:
plink.exe -ssh user#host -pw password -m c:\path\command.txt > output.txt
See Using the command-line connection tool Plink.
And with Plink, you can actually provide the command directly on its command-line:
plink.exe -ssh user#host -pw password command > output.txt
Similar questions:
Automating running command on Linux from Windows using PuTTY
Executing command in Plink from a batch file
You can also use Bash on Ubuntu on Windows directly. E.g.,
bash -c "ssh -t user#computer 'cd /; sudo my-command'"
Per Martin Prikryl's comment below:
The -t enables terminal emulation. Whether you need the terminal emulation for sudo depends on configuration (and by default you do no need it, while many distributions override the default). On the contrary, many other commands need terminal emulation.
As an alternative option you could install OpenSSH http://www.mls-software.com/opensshd.html and then simply ssh user#host -pw password -m command_run
Edit: After a response from user2687375 when installing, select client only. Once this is done you should be able to initiate SSH from command.
Then you can create an ssh batch script such as
ECHO OFF
CLS
:MENU
ECHO.
ECHO ........................
ECHO SSH servers
ECHO ........................
ECHO.
ECHO 1 - Web Server 1
ECHO 2 - Web Server 2
ECHO E - EXIT
ECHO.
SET /P M=Type 1 - 2 then press ENTER:
IF %M%==1 GOTO WEB1
IF %M%==2 GOTO WEB2
IF %M%==E GOTO EOF
REM ------------------------------
REM SSH Server details
REM ------------------------------
:WEB1
CLS
call ssh user#xxx.xxx.xxx.xxx
cmd /k
:WEB2
CLS
call ssh user#xxx.xxx.xxx.xxx
cmd /k

Remote plink ssh program ends prematurely

I have a program that can be run via remote plink ssh on a different PC using windows command prompt
plink [username]#[ip address] -pw [password] ./program
or:
plink -ssh -l [username] -pw [password] [ip address] ./program
but somehow the program will end prematurely. Program runs, but terminates before the program had run its course. It doesn't happen to all my programs though.
To clearify, I can run my program with following command line
plink [username]#[ip address] -pw [password]
which log me in to remote system, from there I can run ./program to its entirety.
but if I try plink [username]#[ip address] -pw [password] ./program in one line, it will quit in the middle.
I wonder if anyone encounter the same problem before? How do you solve it? Thanks
plink [username]#[ip address] -pw [password] -m program.txt
Execute remote commands from local file. You can create a local command file with Unix commands in it, and run it remotely and automatically. Example -- "find /usr/local -name pmapp -print" in cmds.txt can be run with:
so use -m option that help you to execute remote commands
cheers.

Resources