Windows-to-Linux: Execute a shell script as user process (as bkgrnd) on remote machines
I am trying to do the following:
1) Run a shell script on remote machine 1,2,and 3 (all Linux) from Windows machine.
2) On my Windows machine I have plink which I am using in the following way:
plink -i <my_private_key> [email protected] -t "bash <script_loc> args &"
When I do that, the expected process does not get started. I confirmed that by remote logging into machineX(1,2,or3) and typing:
ps -A -F | grep -i "whatever"
I can start it on a command line by typing:
cmd /c start plink -i <my_private_key> [email protected] -t ""bash <script_loc> args ""&""""
but that means it will open a new terminal and upon logging off Windows machine, these will be terminated.
My goal is to remotely start these scripts and keep them running as background processes or daemons. I will be able to find pid etc. since I know what they will be running via command line.
Is this doable without creating a service ?
Regards,
Solution 1:
If you are running plink, then you are not using PS remoting to do this, as you are just running a plink command one of the PS hosts (console / ISE / VSCode). This is not a PS issue, but a how to run and exe in PS. I do plink to Linux boxes daily without issue, but as you've noted in your second command and start command issue.
If you are in one of the PS consoles, you have to provide a execution command, and for external .exe, you should get into the habit of fully qualifying the exe.
For example, here it the way I do this successfully to all my Linux boxes. That & (ampersand) means - Execute string as command. Basically that same thing you are doing by calling cmd.exe start, but without the additional terminal window.
# Using PowerShell and Plink for Linux remote management
# Example
$PlinkPath = 'D:\Tools\plink.exe'
$linuxhostname = 'SomeLinuxHostName'
$linuxusername = 'SomeLinuxUserName'
$LinuxSessionName = 'LinuxSessionNameName' # Putty Saved session name
$GetIptablesNat = & $PlinkPath -load $LinuxSessionName 'sudo -s iptables -L -nv --line-numbers -t nat';$GetIptablesNat
$GetIptablesFilter = & $PlinkPath -load $LinuxSessionName 'sudo -s iptables -L -nv --line-numbers -t filter';$GetIptablesFilter
$RestartIptablesService = & $PlinkPath -load $LinuxSessionName 'sudo -s service iptables restart';$RestartIptablesService
# Network statistics
$GetNetworkStatistics = & $PlinkPath -load $LinuxSessionName 'sudo -s netstat -tN';$GetNetworkStatistics
# Connectivity check
(
'SomeLinuxHostFQDN','SomeLinuxIPA'
) | % { Test-Connection -ComputerName $_ }
# From Linux NAT Gateway
$TestConnectivity = & $PlinkPath -load $LinuxSessionName 'sudo -s ping SomeHostIPA';$TestConnectivity
If you want stuff to run in the background that is what PS jobs are for.
See this article:
https://codingbee.net/tutorials/powershell/powershell-running-tasks-in-the-background
See also the PS help file info regarding parallel processes and Runspaces.
Get-Help -Name about_Jobs
Get-Help -Name about_Parallel
Get-Help -Name about_PSSessions
Get-Help -Name about_PSSession_Details
Get-Help -Name about_Workflows
Get-Help -Name about_WorkflowCommonParameter
Beginning Use of PowerShell Runspaces
https://blogs.technet.microsoft.com/heyscriptingguy/2015/11/26/beginning-use-of-powershell-runspaces-part-1
I've not however tried this with Plink.
But there are lots of articles regarding PS and Plink use cases, even for Cisco switches.
SSH PowerShell tricks with plink.exe
http://www.virtu-al.net/2013/01/07/ssh-powershell-tricks-with-plink-exe
Sample PowerShell module for managing SSH enabled Linux hosts using SMA
This is a sample Windows PowerShell module to manage SSH enabled Linux hosts. It wraps the Putty tool Plink.exe to perform the SSH commands.
https://gallery.technet.microsoft.com/scriptcenter/Sample-PowerShell-module-8d961a1c
And point of note, PS has an SSH module, so, you don't have to use Plink specifically to interop with Linux clients.
Using SSH with PowerShell
https://www.thomasmaurer.ch/2016/04/using-ssh-with-powershell
Posh-SSH 2.0.2
Provide SSH and SCP functionality for executing commands against remote hosts.
https://www.powershellgallery.com/packages/Posh-SSH/2.0.2