Perl Net::SSH::Perl not loading my environment variables vs connecting through SSH
My problem is connecting throguh Net::SSH::Perl vs Connecting through SSH. my shell variables ( command: export ) aren't the same.
This fact prevents me from executing user's script that depends on the shell env vars.
For example:
if i execute through perl ( Net::SSH::Perl ) the command: "export" I get:
MAIL=/var/mail/username
PATH=/usr/local/bin:/bin:/usr/bin
PWD=/username
SHELL=/bin/ksh
SSH_CLIENT='myIPAddress 1022 22'
SSH_CONNECTION='myIPAddress 1022 remoteIPAddress 22'
USER=myusername
while executing the same co开发者_StackOverflow社区mmand through regular ssh connecting I get 42 rows of ENV VARS.
Other example:
if i execute through perl ( Net::SSH::Perl ) the command: "tty" I get:
not a tty
while executing the same command through regular ssh connecting I get:
/dev/pts/3
What am I missing here ?
For the environment variables, it sounds like ~/.bashrc
isn't getting sourced. You should be able to source it yourself:
$ssh->cmd(". ~/.bashrc");
For the terminal, allocating a pty
is not necessary for many tasks, so it is not normally done for non-interactive shells; however, you can pass the use_pty
option when creating your ssh object to tell it to create a pty
.
from the Net::SSH::Perl
documentation:
use_pty
Set this to 1 if you want to request a pseudo tty on the remote machine. This is really only useful if you're setting up a shell connection (see the shell method, below); and in that case, unless you've explicitly declined a pty (by setting use_pty to 0), this will be set automatically to 1. In other words, you probably won't need to use this, often.
The default is 1 if you're starting up a shell, and 0 otherwise.
Chas, that would not work either as Net::SSH::Perl (and for that matter, most other Perl SSH clients) runs every command in a new shell session, and so side effects do not persist between commands.
The solution is to combine both commands as follows:
$ssh->cmd(". ~/.bashrc && $cmd");
精彩评论