Powershell command substitution over SSH - powershell

I am trying to connect from a unix server to a windows server via SSH, run a powershell command, and then process the output on the unix server.
The SSH connection and remote execution works without error, and powershell is the login shell presented by the SSH server running on the windows host.
Whenever I try to run a powershell command with command substitution, however, the command substitution is not parsed by powershell but rather is treated as a string literal.
For example, as part of a larger command I want to get the hostname of the windows server. Given a windows host at HOSTNAME, when SSH'd into an interactive powershell session, I can run
$ echo "$(hostname)"
HOSTNAME
But when running from the unix server
$ ssh HOSTNAME "echo \"\$(hostname)\""
"$(hostname)"
From my reading, I am inclined to believe that this is due to either a quoting error, which seems unlikely as powershell isn't complaining about syntax, or to powershell operating in a different state than it does when run interactively. The latter seems unlikely as well, but I'm coming from unix/bash to windows/powershell so the general control flow may very well be different than I am accustomed to.
Thus, my question is how do I get powershell to perform command substitution on remotely executed commands?

Related

Noninteractive Remote Commands with Powershell

I am developing a script which uses ssh to connect to a windows host, run a powershell command, and parse the output. While I can connect to the host and run the command, powershell will not exit and return control back to the local script until I press the enter key.
At the moment, the specific command being run is ssh HOSTNAME 'echo $(hostname)' and the ssh server is configured to pass remote execution requests to powershell -noninteractive -command CMD, where HOSTNAME is the name of the windows host and CMD is the remote command to be run (in this case echo $(hostname)).
The end goal is to have the script which is calling remote powershell commands to run completely noninteractively, but this is currently impossible as the powershell command will not run noninteractively.
How do I get powershell to run remote commands noninteractively?
I've resolved the issue. While I haven't determined what was causing the issue, I can at least overcome the issue at hand by redirecting stdin to /dev/null on the side that initiates the ssh connection.
$ ssh HOSTNAME "CMD" </dev/null
This solution doesn't involve powershell at all, but rather treats the symptoms from the other side of the connection.

Run perl script on remote server

Is it possible to run perl script, which is located on a remote server, on that server from Windows? There is a job on a remote server that I want to get done every time I make something on Windows.
You have to have something listening for an instruction to run the script, and then you have to send the instruction.
There are lots of approaches you could take to that, including:
Running an SSH server and then connecting to it from an ssh client on the windows machine
Running an HTTP server, running the script through FastCGI, and then requesting the URL for it from curl or a browser on the Windows machine
Writing a custom protocol, listening on a socket, and then writing a custom client that you run on the Windows machine
Absolutely.
You can use plink to run commands on the server from Windows, assuming the server is running sshd.
plink user#a.domain.ext echo hi
This will print "hi\n" to the standard output.
Substitute /path/to/perl/script for echo above and substitute hi with any command line argument that the script needs.
plink is available here: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
One cautionary personal note from doing this many times is that the environment in which the perl script will be run is much less complete than what you would experience when logging in via a full SSH session and running the command interactively. Many environment variables you would normally expect are unset.
For instance using "set | wc -l" in the command above produces only 39 environment variables defined, but from an interactive SSH session, there are 57 environment variables defined. You have to make sure your perl script isn't depending on an environment variable that hasn't been set. For instance, you may need to use full paths for any modules that it uses, or by using the -I flag in the shebang line, because #INC may not be what you expect it to be.

Run a perl script inside another script using ssh

I have a perl script where I need to connect to another machine using ssh and there run another perl script. I tried using this line:
system("ssh $admin_server 'perl /Perl/scripts/capture_server_restarts_gse.pl $month $date'");
But everytime the script gets to that line, I get the prompt for the remote machine and the script doesn't run.
How can I fix this so the script runs automatically on the other machine without showing the prompt.
Note: I don't need the password and user to connect to the remote machine we already solved that.
Why not copy your public key onto the other machine ? That way you'll be pre-authorised.
Here are the instructions on how to do this using ssh-keygen
Otherwise you have to feed ssh with your password, and that's tricky since ssh normally takes input from a tty and you have to configure your script with the password.
The SSH server may be configured to run always some custom shell instead of the command passed from the client.
Try just running some simple command from the command line, i.e.:
ssh server ls
A less likely possibility is that the perl variables interpolated into the system argument could contain some shell metacharacters requiring better escaping. For instance, a semicolon inside $admin_server.

Run a perl script on remote machine from local machine using Telnet or SSH with Perl

I want to run a Perl script in a remote machine using telnet or ssh. The script is on my local host.how can do this. Can anyone please help me on this?
If you for some reason don't want to copy the script to the remote host and then run it, you can send the script to the Perl interpreter over stdin. If perl doesn't get either a script name of a script on the command line it tries to read the script on stdin. So this would work:
ssh user#remote perl < my_script.pl
Of course this requires that all necessary modules are already installed on the remote host. If you script only have pure perl dependencies you can work around this restriction by using App::FatPacker to make your script (more) self contained.
But if this is an recurring task I would recommend getting the script deployed correctly to your remote host.
scp your script to remote machine.
ssh user#remote 'perl /path/to/remote/script.pl'
Using HERE document across SSH might also do the trick you are after. You can run at least a BASH script without first separately copying it to remote. I have not verified anything else than BASH but no reason to doubt either. Please see:
ssh + here document + interactive mode

executing win32ole script on remote windows machine through telnet

I am trying to create a word document on a remote windows machine. What I am trying is to telnet to the remote windows machine and run a perl script that creates word document through Win32::OLE. But it doesn't seem to work. Is this possible? Because my script has {visible} set to 1 but will that telnet session have access to instances of word application? Atleast I tried it didn't work.
Telnet may not be the best tool to accomplish this, I'm not sure what kind of permissions it has. I recommend using PsExec, which allows remote command execution on windows servers. If it works locally, it will work using PsExec.
For example:
PsExec.exe \\remotecomputer -u userName -p Password Perl C:\path\to\file\file.pl
You can use the -s flag to run as system account, and the -i flag to run it interactively on the desktop. Without the -i flag, it will run in the console session.