powershell start program on remote machine, webservice interface not working - web-services

I have a remote server where I have a program (C#, WCF) with a webservice interface that I need to start from another machine. Currently I have a powershell script on the remote machine with:
Start-Process -WindowStyle Minimized 'path to exe'
This works if I start the script on the server directly with the following in a cmd prompt.
Powershell.exe start.ps1
Webservice interface is fully functional.
However when I run the script remotely through powershell with
Invoke-WmiMethod –class Win32_process -name Create -ArgumentList "powershell.exe c:\Start.ps1" -ComputerName $serverName -credential $Cred
The program starts up on the remote machine, but webservice interface isn't working ? I have tried to look at the wsdl but it doesn't show.
Is there any special permissions or something that needs to be added when calling the script from remotely to get it to work ?

If I understood you right, your problems lies in starting the exe on a remote server. If you're using WIN2012 R2 (or higher) Powershell remoting should be activated per default (if you're using an older WIN version perform some Google queries about how to activate Powershell remoting).
Via Powershell remoting you can use Invoke-Command to start the exe on the remote. Example:
Invoke-Command -ComputerName 1.2.3.4 -Credential (Get-Credential) -Scriptblock { Start-Process "your.exe" }
Using Powershell remoting has some advantages.
Standardized mechanism usage via WSMAN.
Remoting uses HTTP(S) for transport. Therefore working with firewalls becomes easier.
Hope that helps.

Related

Creating a CLI application with root access

I am developing a php application which serves as a GUI for a seever side application. Because of the nature of the application, it needs to run exec commands which require root privileges. (things like restarting a service). I was able to get around it by giving nginx sudo access to specific commands. But it still requires a few functions which will be easy to make with a CLI.
Now the problem I am facing is starting this application from php with arguments as root. This is how I launch my app,
path/application - e "command I want"
The web app will be only one installed on the server (kind of like a control panel). Should I focus on making a service instead of an application? If I do make an service how would I let php contact it? I have developed windows applications in the past using .NET and c++.
I did look at dotnet core to make a Linux service, but I don't think it'll be what I need. Can I have any suggestions? All I need the app to have is root access, possibly without sudo.
Could the application be a setuid root application? Please test it for security before doing so
chown root /path/to/binary
chmod u+s /path/to/binary

How can I connect web app with azure virtual linux macine on same azure account?

In my project I have to get two files (fastq it's Bioinformatics file) from user through HTML form and analyse it through some UNIX program (I used PHP in server side scripting) which is supported only in UNIX OS (I'm just using shell_exec() to run all UNIX program). For now I have implemented it in local host and every thing is working fine.
Now I have to do the same in Azure. I have created a webpage in Azure Web app and I have a UNIX VM in the same account. I want to know how i can connect both for free (I have already seen Integrate your app with an Azure Virtual Network but they are implementing additional charges).
According to your description, based on my understanding, I think you want to deploy your app on Azure WebApps & VM based on Linux and make it works as well as on your local host, which is a PHP app and call some UNIX-like program in shell via PHP method shell_exec.
Per my experience, there are two ways to do your deployment.
You need to create an instance of Azure WebApps for Linux, which is a Docker container based on Ubuntu. Then, you can install any services you want to deploy your PHP app and directly call the UNIX-like programs via PHP method shell_exec without any instance of Azure Linux VM to run the UNIX-like programs.
You can use an instance of Azure WebApps which is based on Windows, but it's impossible to run the UNIX-like programs hosted on Azure Linux VM via shell_exec method. For calling remote UNIX-like programs on Azure Linux VM, there are two ways to change your PHP app as below.
2.1. Via PHP methods ssh2_connect/ssh2_auth_password/ssh2_exec to create a ssh session to connect the remote Linux VM and do the SSH authentication to run the UNIX-like program via ssh2_exec method, if you had installed PHP extension package ssh2 on Windows for Azure WebApps. My sample code is as below.
<?php
$host = '<your Azure linux vm host>';
$port = 22; // First enable the 22 port for Azure Linux VM
$user='<ssh user>';
$passwd='<ssh password>';
$session=ssh2_connect($host, $port);
if($session)
echo "connect successfully.\n";
if(ssh2_auth_password($session, $user, $passwd)) {
echo $user." login OK.\n";
$stream=ssh2_exec($session, "pwd");
stream_set_blocking($stream, true);
if ($stream === FALSE) die("pwd failed");
echo 'pwd: '.stream_get_contents($stream).'<br/>';
}
?>
2.2. Or you can create a new PHP app to call the UNIX-like programs on Azure Linux VM to publish a WebService API or a RESTful API for being called by your PHP app on Azure WebApps.
Hope it helps.

how to run python script located on a remote server

I have python script that I run on my cmd local machine.
now I want to run it also on a remote server(Windows)
How can I do it?
It is possible using ssh. Python accepts hyphen(-) as argument to execute the standard input,
cat hello.py | ssh user#192.168.1.101 python -
Run python --help for more info.
You can use the ssh approach, but also the PSEXEC approach which is easier to use, although you may need admin privileges on the server to execute processes and both windows machines better share the same user/password.
Download PsExec from here
https://technet.microsoft.com/en-us/sysinternals/bb896649
Run as follows:
psexec /ACCEPTEULA \\servermachine python fullpath_to\python_script.py
fullpath_to\python_script.py should be accessible from the server. If not, you have to copy it first here, or just put the script on a shared/networked drive visible by both machines.
Of course python must be installed on the server as well.
I'm not saying it is the best way. Jenkins is a good way to run stuff on a given server. But it does the job.

Execute command or transfer files to remote windows machine

What I'm actually looking for is the equivalent off ssh connection in windows environment. As per requirement, my controller machine can connect to remote machine with username password using some kind of utility/protocol such as ssh or telnet or rpc or tpc and using this session I can transfer files or execute command on remote machine. This connection execution must be done without any intervention from remote machine, i.e. I don't need to install any module or install any utility on remote machine or run any script.
My both controller and remote environment are windows.
Can someone suggest me python module or utility, using which I'll be able do this ?
I explore few option however I'm not sure if they are the best way to do so. Pleas provide your thoughts on this too.
connect using psexec utility through popen.
using socket to communication channel, however I'm not sure if I can execute any command using this channel.
making use of Telnet. but I didn't explore the python module to make telnet connection and execute command.
using module such as Pyro or rpyc.
Any help is appreciated. Thanks a lot in advanced.
take a look at the pexpect module. It can be used for ftp, ssh ...
pexpect doc

Understanding fabric

I've just stumbled upon Fabric and the documentation doesn't really make it obvious how it works.
My educated guess is that you need to install it on both client-side and server-side. The Python code is stored on the client side and transferred through Fabric's wire-protocol when the command is run. The server accepts connections using the OpenSSH SSH daemon through the ~/.ssh/authorized_keys file for the current user (or a special user, or specified in the host name to the fab command).
Is any of this correct? If not, how does it work?
From the docs:
Fabric is a Python (2.5 or higher) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
It provides a basic suite of operations for executing local or remote shell commands (normally or via sudo) and uploading/downloading files, as well as auxiliary functionality such as prompting the running user for input, or aborting execution.
So it's just like ssh'ing into a box and running the commands you've put into run()/sudo().
There is no transfer of code, so you only need to have ssh running on the remote machine and have some sort of shell (bash is assumed by default).
If you want remote access to a python interpreter you're more looking at something like execnet.
If you want more information on how execution on the remote machine(s) work look to this section of the docs.
Most what you are saying is correct, except that the "fabfile.py" file only has to be stored on your client. An SSH server like OpenSSH needs to be installed on your server and an SSH client needs to be installed on your client.
Fabric then logs into one or more servers in turn and executes the shell commands defined in "fabfile.py". If you are located in the same dir as "fabfile.py" you can go "fab --list" to see a list of available commands and then "fab [COMMAND_NAME]" to execute a command.
The user on the server does not need to be added to "~/.ssh/authorized_keys" but if it is you don't have to type the password every time you want to execute a command.