Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
CDing to a certain directory in ssh
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
vuakko
Tux's lil' helper
Tux's lil' helper


Joined: 09 May 2007
Posts: 138
Location: Helsinki, Finland

PostPosted: Wed Nov 21, 2007 8:49 am    Post subject: CDing to a certain directory in ssh Reply with quote

I'd want to do something like ssh <machine>:dir in scp syntax, but if I just use
ssh <machine> cd $dir then the connection of course logs me out immediately.

Does anyone know a way to ssh, do a command and then continue in the shell?
I would like my pwd to stay constant when I ssh to another machine
in the network (they share the same fs tree).
Back to top
View user's profile Send private message
JeliJami
Veteran
Veteran


Joined: 17 Jan 2006
Posts: 1086
Location: Belgium

PostPosted: Wed Nov 21, 2007 11:57 am    Post subject: Reply with quote

you could place a bash script in your homedir on the remote server, workdir.sh:
Code:
#!/bin/bash

cd $1
/bin/bash


and call it in your ssh call:
Code:
local $ ssh -t remote /bin/bash workdir.sh $PWD


and you start out in the same directory on the remote server
_________________
Unanswered Post Initiative | Search | FAQ
Former username: davjel
Back to top
View user's profile Send private message
Akkara
Bodhisattva
Bodhisattva


Joined: 28 Mar 2006
Posts: 6702
Location: &akkara

PostPosted: Wed Nov 21, 2007 12:35 pm    Post subject: Reply with quote

From the ssh man page:
Quote:
Additionally, ssh reads ~/.ssh/environment, and adds lines of the format ``VARNAME=value'' to the environment if the file exists and users are allowed to change their environment. For more information, see the PermitUserEnvironment option in sshd_config(5).


I don't know whether adding the line HOME=/your/choice will do what you want, but it might be worth a look.

Edit: Hm, on second thought, even if it does start you out where you want, you probably wouldn't want your home to be set to that.
Back to top
View user's profile Send private message
vuakko
Tux's lil' helper
Tux's lil' helper


Joined: 09 May 2007
Posts: 138
Location: Helsinki, Finland

PostPosted: Wed Nov 21, 2007 2:03 pm    Post subject: Reply with quote

That works only half way (davjel's suggestion). It goes to the correct directory, but I don't get any shell prompt
(that is <blah blah>$) so its really not so useful still, because I never know where I am without
using pwd and I never know when a command has run through. Also tab-completion doesn't work and I
would also have to source my bashrc and so on in the same script manually.

So thanks for the offer, but it doesn't help yet.

Btw, as I said, the machine share the whole fs tree so the homedir is the same on every machine.
Back to top
View user's profile Send private message
Akkara
Bodhisattva
Bodhisattva


Joined: 28 Mar 2006
Posts: 6702
Location: &akkara

PostPosted: Wed Nov 21, 2007 2:42 pm    Post subject: Reply with quote

Might there be a way of aliasing ssh to do the following:

- append a line to your .bash_profile to go where you are now:
Code:
echo "cd "`pwd` >>~/.bash_profile


- ssh to where you're going.

And previously having add a small script to .bash_logout to remove the last line in .bash_profile, if it starts with a "cd".
Back to top
View user's profile Send private message
JeliJami
Veteran
Veteran


Joined: 17 Jan 2006
Posts: 1086
Location: Belgium

PostPosted: Wed Nov 21, 2007 3:09 pm    Post subject: Reply with quote

ntvuok wrote:
That works only half way (davjel's suggestion). It goes to the correct directory, but I don't get any shell prompt
(that is <blah blah>$) so its really not so useful still, because I never know where I am without
using pwd and I never know when a command has run through. Also tab-completion doesn't work and I
would also have to source my bashrc and so on in the same script manually.

So thanks for the offer, but it doesn't help yet.

Btw, as I said, the machine share the whole fs tree so the homedir is the same on every machine.


I don't get it. For me it works:
Code:
david@david ~ $ cd /usr/bin
david@david /usr/bin $ ssh -t octopus-dev /bin/bash workdir.sh $PWD
david@octopus-dev:/usr/bin$


AH, I see: you forget the '-t' option for ssh!!
_________________
Unanswered Post Initiative | Search | FAQ
Former username: davjel
Back to top
View user's profile Send private message
vuakko
Tux's lil' helper
Tux's lil' helper


Joined: 09 May 2007
Posts: 138
Location: Helsinki, Finland

PostPosted: Wed Nov 21, 2007 4:52 pm    Post subject: Reply with quote

OK, now I've got it working.

pwd.sh:
Code:

#!/bin/bash

cd "$*"
/bin/bash

.bashrc:
Code:

...
function bestcl {
   ...
   ssh -t machine-$ind /bin/bash pwd.sh `pwd`
}
...

Thanks!

I tried all kinds of $1, $@ and $* confs with or without parentheses, finally
"$*" seemed to work.

Reason for this all: Our lab has a bunch of older cluster machines you use over ssh.
Function bestcl first calculates the cluster that has the lowest load (takes only 0.1 secs) and
then goes ssh there. It started to really grind me, when I always had to cd to the directory
I just was in.
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