Run perl script on remote server - perl

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.

Related

Powershell command substitution over SSH

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?

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

How to open ssh session and execute commands from a Perl script?

I have a Perl script running on a Windows machine. I need this script to open a ssh session to a remote Unix machine, and to be able to execute certain commands on that Unix machine and to be able to get the output returned from these commands.
These commands are generated during the run-time of the script, and there are many of them executed at different times.
How can I do it?
Approach 1: Use CYGWIN: http://perlwin32ssh.blogspot.com/2007/07/test_4418.html
Approach 2: Use Net::SSH::W32Perl module.
This is one thread discussing how to install it: http://code.activestate.com/lists/perl-win32-users/29180/ (It seems to require downloading custom version of the module)
This thread should help with the problems arising from dependencies on math libraries needed for ssh calculations: http://www.issociate.de/board/post/494356/I%27m_trying_to_install_%27Net::SSH::Perl%27_on_a_Windows_Box..html
Caveat emptor: I never installed this, the above is just result of some analysis of google results.
#!/usr/bin/perl
system("ssh foo 'ls -l'");
Or go through the hassle of using ptmx(4) on the local side and ssh -t for remote.

Is it possible to have Perl run shell script aliases?

Is it possible to have a Perl script run shell aliases? I am running into a situation where we've got a Perl module I don't have access to modify and one of the things it does is logs into multiple servers via SSH to run some commands remotely. Sadly some of the systems (which I also don't have access to modify) have a buggy SSH server that will disconnect as soon as my system tries to send an SSH public key. I have the SSH agent running because I need it to connect to some other servers.
My initial solution was to set up an alias to set ssh to ssh -o PubkeyAuthentication=no, but Perl runs the ssh binary it finds in the PATH instead of trying to use the alias.
It looks like the only solutions are disable the SSH agent while I am connecting to the problem servers or override the Perl module that does the actual connection.
Perhaps you could put a command called ssh in PATH ahead of the ssh which runs ssh as you want it to be run.
Alter the PATH before you run the perl script, or use this in your .ssh/config
Host *
PubkeyAuthentication no
Why don't you skip the alias and just create a shell script called ssh in a directory somewhere, then change the path to put that directory before the one containing the real ssh?
I had to do this recently with iostat because the new version output a different format that a third-party product couldn't handle (it scanned the output to generate a report).
I just created an iostat shell script which called the real iostat (with hardcoded path, but you could be more sophisticated), passing the output through an awk script to massage it into the original format. Then, I changed the path for the third-party program and it started working fine.
You could declare a function in .bashrc (or .profile or whatever) with that name. It could look like this (might break):
function ssh {
/usr/bin/ssh -o PubkeyAuthentication=no "$#"
}
But using a config file might be the best solution in your case.