Why my perl script requires to export packages path into #INC to run remotely via ssh - perl

The server perl script - with its required packages - works locally by the user "my_user".
But if I run the script remotely (ssh), I need to export PERL5LIB=/usr/local/share/perl/5.10.0/my_modules before calling the perl script to get it working.
Why this and how can I turn around this in order to avoid exporting PERLIB each time I need to call a remote perl script ?
WORKING :
ssh my_user#remote_server "export PERL5LIB=/usr/local/share/perl/5.10.0/my_modules; /cgi-bin/my_perl_script.pl --option1 foo --option2 '*';"
NOT WORKING :
ssh my_user#remote_server "/cgi-bin/my_perl_script.pl --option1 foo --option2 '*';"
returns :
Can't locate my_package1.pm in #INC
That might be rather an ssh question than a strict perl point : why the remote user running the perl script does not inherit from its ENV local datas.
Thx

As suggested by #mu_is_too_short (no friction is good as well), and linking to a more detailed explanation here, there are different types of shells : "the SSH command execution shell is a non-interactive shell, whereas your normal shell is either a login shell or an interactive shell".
So the solution is what I did on purpose (eg adding "export PERL5LIB" before running the script), or better, source the whole environement from the remote user to run the remote shell with the expected behavior.

Related

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

Permission denied (publickey,keyboard-interactive)

Permission denied (publickey,keyboard-interactive) got this error while i am trying to cvs checkout from perl.
what is issue and how to reslove this ?
Code :
system ( "CSVROOT:--- CVSRSH:--- cvs co a ");
# i have proper value in cvs root and cvs rsh .
its running alone and using ssh key
Steps to diagnose the error:
Are you using an SSH key?
Does that key have a passphrase?
Does it work when you run it by hand?
Is the script running as the same user as when you run it by hand?
Is the script running under the same environment as when you run it by hand? (e.g. cron jobs do not run under the same environment)
If you think all of the answers are yes, then most likely the last answer is really no. If the script is running from a scheduler like cron it most likely does not run with the same environment as when you run it by hand. The way I normally solve this is to use a shell script between the scheduler and the Perl script:
#!/bin/bash
source /home/USERNAME/.profile
#set any other environment variables it needs like
export CSVROOT=:pserver:USERNAME#HOST:/path/to/repo
export CVSRSH=ssh
/path/to/perl/script/script.pl
Follow-up investigations after Chas.'s questions:
Does that command normally run under /bin/sh or some other shell?
To test, execute /bin/sh command to start Bourne shell and try the command by hand again.
I'm not familiar with "CVSROOT:---" notation - is that meant to set CVSROOT environmental variable? In Bourne shell it's usually done using "=", never saw ":" used.
Does the command, when run by hand, expect some input from you? I never saw cvs co to do so, but I don't use it with ssh.
Try to add a redirect to the end of the command and look what's in the file after running:
system ( "CSVROOT:--- CVSRSH:--- cvs co a > /tmp/log_cmd 2>&1");

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.