Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
exporting a variable locally on ssh connection. [solved]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5589

PostPosted: Tue Oct 25, 2011 6:04 am    Post subject: exporting a variable locally on ssh connection. [solved] Reply with quote

Hello,

I have a ssh server in which I want to export a variable (lets say FOO) right after the connection (before any other scripts) so when running "export | grep FOO" I'll see the variable.

I don't want to place it in .bashrc and I need it for every connection.

I've tried using both /etc/ssh/sshrc and /etc/ssh/environment but it didn't worked.

any ideas?

Thanks.
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein


Last edited by DaggyStyle on Thu Oct 27, 2011 8:59 pm; edited 1 time in total
Back to top
View user's profile Send private message
rgawenda
n00b
n00b


Joined: 30 Mar 2005
Posts: 47
Location: Ourense, Spain

PostPosted: Tue Oct 25, 2011 9:17 am    Post subject: Reply with quote

You need the server to set the variable for incoming ssh shells only. Right?
_________________
Rafael C. Gawenda
Linuxer since 0.99pl11
Registered Linux User #93375
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5589

PostPosted: Tue Oct 25, 2011 12:12 pm    Post subject: Reply with quote

rgawenda wrote:
You need the server to set the variable for incoming ssh shells only. Right?

yes, it is a var which a script that I run upon login should act in accordance to it's value.
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
rgawenda
n00b
n00b


Joined: 30 Mar 2005
Posts: 47
Location: Ourense, Spain

PostPosted: Tue Oct 25, 2011 4:39 pm    Post subject: Reply with quote

What's the reason you don't want it in .bashrc or /etc/bash/bashrc?
Would /etc/profile / .profile serve your purposes?

I've put there the following code to test, and it worked flawlessly
Code:
[[ -n "$SSH_CONNECTION" ]] && export TEST=1

_________________
Rafael C. Gawenda
Linuxer since 0.99pl11
Registered Linux User #93375
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5589

PostPosted: Tue Oct 25, 2011 4:56 pm    Post subject: Reply with quote

rgawenda wrote:
What's the reason you don't want it in .bashrc or /etc/bash/bashrc?
Would /etc/profile / .profile serve your purposes?

I've put there the following code to test, and it worked flawlessly
Code:
[[ -n "$SSH_CONNECTION" ]] && export TEST=1


I'd rather do that only with ssh connection but it seem that your solution may help.

thanks.
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
rgawenda
n00b
n00b


Joined: 30 Mar 2005
Posts: 47
Location: Ourense, Spain

PostPosted: Tue Oct 25, 2011 4:58 pm    Post subject: Reply with quote

DaggyStyle wrote:
rgawenda wrote:
What's the reason you don't want it in .bashrc or /etc/bash/bashrc?
Would /etc/profile / .profile serve your purposes?

I've put there the following code to test, and it worked flawlessly
Code:
[[ -n "$SSH_CONNECTION" ]] && export TEST=1


I'd rather do that only with ssh connection but it seem that your solution may help.

thanks.

The test [[ -n "$SSH_CONNECTION" ]] does exactly that. No value or exported TEST var if the session is not from an incoming SSH
_________________
Rafael C. Gawenda
Linuxer since 0.99pl11
Registered Linux User #93375
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5589

PostPosted: Tue Oct 25, 2011 5:14 pm    Post subject: Reply with quote

rgawenda wrote:
DaggyStyle wrote:
rgawenda wrote:
What's the reason you don't want it in .bashrc or /etc/bash/bashrc?
Would /etc/profile / .profile serve your purposes?

I've put there the following code to test, and it worked flawlessly
Code:
[[ -n "$SSH_CONNECTION" ]] && export TEST=1


I'd rather do that only with ssh connection but it seem that your solution may help.

thanks.

The test [[ -n "$SSH_CONNECTION" ]] does exactly that. No value or exported TEST var if the session is not from an incoming SSH


ok, now I just need to test it in ssh connection.
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5589

PostPosted: Tue Oct 25, 2011 8:52 pm    Post subject: Reply with quote

well, not good, when I run a ssh with command, it doesn't works as the is no bash session open.
even running with bash -x doesn't work.
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
rgawenda
n00b
n00b


Joined: 30 Mar 2005
Posts: 47
Location: Ourense, Spain

PostPosted: Wed Oct 26, 2011 8:36 am    Post subject: Reply with quote

Just tried putting the above code in ~./.bashrc and it worked:
Code:
ais@aisl ~ $ ssh rex env
A=1
SHELL=/bin/bash
SSH_CLIENT=10.68.32.35 34270 7922
[...]
SSH_CONNECTION=10.68.32.35 34270 10.68.30.32 7922
_=/usr/bin/env
ais@aisl ~ $

Code:
ais@aisl ~ $ ssh rex cat .bashrc
#!/bin/bash
[[ -n "$SSH_CONNECTION" ]] && export A=1
ais@aisl ~ $

_________________
Rafael C. Gawenda
Linuxer since 0.99pl11
Registered Linux User #93375
Back to top
View user's profile Send private message
wcg
Guru
Guru


Joined: 06 Jan 2009
Posts: 588

PostPosted: Thu Oct 27, 2011 10:38 am    Post subject: Reply with quote

I think the OP is saying that he is not necessarily running a login shell like bash,
tcsh, ksh, etc when the ssh session logs in on the remote machine, hence standard
login environments and per user login shell configuration files are not sourced.

man sshd_config, look up PermitUserEnvironment. I do not see that there is any
other mechanism for configuring a pre-login-shell ssh session environment.

(If you are an experienced C hacker, you could look at the openssh source,
of course, follow the process of a login from sshd's point of view, and see what
all happens, with and without PermitUserEnvironment.)
_________________
TIA
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5589

PostPosted: Thu Oct 27, 2011 10:51 am    Post subject: Reply with quote

wcg wrote:
I think the OP is saying that he is not necessarily running a login shell like bash,
tcsh, ksh, etc when the ssh session logs in on the remote machine, hence standard
login environments and per user login shell configuration files are not sourced.

man sshd_config, look up PermitUserEnvironment. I do not see that there is any
other mechanism for configuring a pre-login-shell ssh session environment.

(If you are an experienced C hacker, you could look at the openssh source,
of course, follow the process of a login from sshd's point of view, and see what
all happens, with and without PermitUserEnvironment.)


I think I remember that using /etc/ssh/sshrc did worked for me when it comes to a echo to file but not when it comes to export vars.

I think tat I've read that /etc/ssh/environment should work too but it didn't worked too.

I think that I'll use the sshrc to write that data into the actual script.
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5589

PostPosted: Thu Oct 27, 2011 8:59 pm    Post subject: Reply with quote

/etc/ssh/environment didn't worked, so I've took your advice partially and I'm setting the var within the script depending on the content of SS_CONNECTION
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum