Keychain is quite a useful tool for automating SSH logins without having to use password-less keys: http://www.gentoo.org/proj/en/keychain/
Normally it is used like this to set the SSH_AUTH_SOC and SSH_AGENT_PID variables: source ~/.keychain/hostname-sh
(This is what's in hostname-sh) SSH_AUTH_SOCK=/tmp/ssh-XXn47DUn/agent.16721; export SSH_AUTH_SOCK; SSH_AGENT_PID=16722; export SSH_AGENT_PID;
I would like these environment variables to be made available to a Perl script so that any "system" commands contained in the perl script will inherit them. How do you do this in Perl?
On Wed, Mar 5, 2008 at 10:20 AM, Sean Carolan scarolan@gmail.com wrote:
I would like these environment variables to be made available to a Perl script so that any "system" commands contained in the perl script will inherit them. How do you do this in Perl?
If the variables are exported when you call Perl, they will be available inside Perl. And, when you call "system", they will be available for those processes as well. Are you having any specific problems doing this? If you can't make it work, please send more details on what exactly is not working as supposed to.
Filipe
If the variables are exported when you call Perl, they will be available inside Perl. And, when you call "system", they will be available for those processes as well. Are you having any specific problems doing this? If you can't make it work, please send more details on what exactly is not working as supposed to.
The variables are not exported when I call Perl. This is what I am trying to do. How do I get those variables to be available to the bash "system" commands within the script?
The variables are not exported when I call Perl. This is what I am trying to do. How do I get those variables to be available to the bash "system" commands within the script?
Just so it is clear what I am trying to do, there are some scp and ssh commands like:
system ("/usr/bin/scp /tmp/filename server1:/tmp/filename");
we want the scp command to use the running ssh-agent's credentials to log onto the remote server.
On Wed, Mar 5, 2008 at 11:15 AM, Sean Carolan scarolan@gmail.com wrote:
Just so it is clear what I am trying to do, there are some scp and ssh commands like:
system ("/usr/bin/scp /tmp/filename server1:/tmp/filename");
we want the scp command to use the running ssh-agent's credentials to log onto the remote server.
One solution would be to "source ~/.keychain/hostname-sh" in the shell before calling the perl script. That should work.
Another one would be to source it before calling scp:
system ("source ~/.keychain/hostname-sh; /usr/bin/scp /tmp/filename server1:/tmp/filename");
The third one (if you don't use a shell to invoke the perl and you don't want to change every "system" call) would be to parse the file inside perl and to modify the %ENV variable to include these two variables. What you want is the equivalent of:
$ENV{SSH_AUTH_SOCK} = "/tmp/ssh-XXn47DUn/agent.16721"; $ENV{SSH_AGENT_PID} = "16722";
But of course you'd have to parse the file to get the real values. Anyway, if you set that in the begining of your code, every "system" you use will have these two variables exported as well.
Does this answer your question?
Filipe
One solution would be to "source ~/.keychain/hostname-sh" in the shell before calling the perl script. That should work.
Ok, can't do this because end-users will not like the extra step.
Another one would be to source it before calling scp:
system ("source ~/.keychain/hostname-sh; /usr/bin/scp /tmp/filename server1:/tmp/filename");
Yep, this is what I did. I have the source ~/.keychain/hostname-sh in every command now. This seems the simplest solution for the time being.
The third one (if you don't use a shell to invoke the perl and you don't want to change every "system" call) would be to parse the file inside perl and to modify the %ENV variable to include these two variables. What you want is the equivalent of:
$ENV{SSH_AUTH_SOCK} = "/tmp/ssh-XXn47DUn/agent.16721"; $ENV{SSH_AGENT_PID} = "16722";
But of course you'd have to parse the file to get the real values. Anyway, if you set that in the begining of your code, every "system" you use will have these two variables exported as well.
Yes, and for this simple one-off script probably too much trouble to go parsing the .keychain file. Its easier to just source it before each bash command.
Thanks for all your help, Filipe.
On Wed, Mar 5, 2008 at 11:03 AM, Sean Carolan scarolan@gmail.com wrote:
The variables are not exported when I call Perl. This is what I am trying to do. How do I get those variables to be available to the bash "system" commands within the script?
system("source ~/.keychain/hostname-sh; cmd");
Is this what you're looking for?
Filipe
system("source ~/.keychain/hostname-sh; cmd");
Is this what you're looking for?
Yes, this works. Is there a way to only source the file once? There are a bit over a dozen scp and ssh commands in the script. Unfortunately this is not my script, otherwise I'd have just done this all in bash.
On Wed, Mar 05, 2008 at 11:00:13AM -0600, Sean Carolan wrote:
system("source ~/.keychain/hostname-sh; cmd");
Is this what you're looking for?
Yes, this works. Is there a way to only source the file once? There are a bit over a dozen scp and ssh commands in the script. Unfortunately this is not my script, otherwise I'd have just done this all in bash.
Create a wrapper script: source ~/.keychain/hostname-sh run_annoying_perl_program.pl