SSH Change directory

703 views
Skip to first unread message

Iwan Budi Kusnanto

unread,
Jun 29, 2013, 4:42:10 AM6/29/13
to golan...@googlegroups.com
Hi
How to change working directory of ssh.ClientConn?
I have tried sesssion.Run("cd somedir") but it not worked.

From my understanding, we send each command by create new session then
call Start or Run.
If we change directory by calling session.Run or session.Start it will
only affect that session, not ClientConn.
And it is useless because a session can only send one command.

Any suggestion?

--
Iwan Budi Kusnanto

Jonathan Pittman

unread,
Jun 29, 2013, 11:20:04 AM6/29/13
to Iwan Budi Kusnanto, golang-nuts
Do you need to send several subsequent commands or just one?  You can send more than one command in a string separated by a semicolon (or one of ||, && depending on what you are trying to do and your shell).  I do not recommend it for normal use.  You lose out on things like the exit codes from all but the last command in the string.

A quick search reveals a lot of examples out there (not necessarily related to Go) suggesting that one should combine the commands like "cd somedir; cmd1; cmd2".

Also, this may work.

session.Run("cd somedir && some_extra_cmd arg1 arg2")

You could keep a format string that gets used each time.

cmdFmt := "cd somedir && %s"
session.Run(fmt.Sprintf(cmdFmt, "some_extra_cmd arg1 arg2"))

Or write a wrapper function.

func runSession(sess *ssh.Session, cmd string) error {
        return sess.Run(fmt.Sprintf("cd somedir && %s", cmd))
}

Although, to answer your question, I do not see a way to set the directory for a connection.  I would imagine that each session is meant to be unique in that regard.  Or maybe more so, the sshd running on the remote host would have to support such a feature for a whole connection.




--
Iwan Budi Kusnanto

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Iwan Budi Kusnanto

unread,
Jul 1, 2013, 3:27:22 AM7/1/13
to Jonathan Pittman, golang-nuts
On Sat, Jun 29, 2013 at 10:20 PM, Jonathan Pittman
<jonathan.m...@gmail.com> wrote:
}
>
> Although, to answer your question, I do not see a way to set the directory
> for a connection. I would imagine that each session is meant to be unique
> in that regard. Or maybe more so, the sshd running on the remote host would
> have to support such a feature for a whole connection.


Hi Jonathan,

Thanks for your long answer.
I want to implement a kind of ssh client and i think sshd (from
openssh) should already support this feature because we can 'cd' using
ssh client shipped with Linux/BSD/Mac. Or maybe this ssh client do
some trick to support this?

And thanks for your multiple commands idea, i think it can become a
workaround for this.

>
>
>
> On Sat, Jun 29, 2013 at 4:42 AM, Iwan Budi Kusnanto <iwa...@gmail.com>
> wrote:
>>
>> Hi
>> How to change working directory of ssh.ClientConn?
>> I have tried sesssion.Run("cd somedir") but it not worked.
>>
>> From my understanding, we send each command by create new session then
>> call Start or Run.
>> If we change directory by calling session.Run or session.Start it will
>> only affect that session, not ClientConn.
>> And it is useless because a session can only send one command.
>>
>> Any suggestion?
>>
>> --
>> Iwan Budi Kusnanto
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to golang-nuts...@googlegroups.com.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>



--
Iwan Budi Kusnanto

Dave Cheney

unread,
Jul 1, 2013, 4:33:01 AM7/1/13
to Iwan Budi Kusnanto, Jonathan Pittman, golang-nuts
Changing directories on the remote host sounds like a symptom not the cause. Can I ask the problem you are trying to solve, ie why does your remote command need to be run in a particular directory? Is it not in the path?

You mentioned openssh, I recommend you run your command using -vv and then compare the output to what you expect. If you can show what openssh is doing that the go client is not we can fix it. Also it would be useful to review the 'shell' session command from the ssh RFC.

Cheers

Dave

shensh...@gmail.com

unread,
Jul 10, 2015, 6:50:11 AM7/10/15
to golan...@googlegroups.com
I Seen the problem!
Because one ssh session can execute command once then will be close.  
and when one session was created, The current dir is the home of current user
If you execute some command like "cd / && ls" , will list the dir /

在 2013年6月29日星期六 UTC+8下午4:42:10,Iwan Budi Kusnanto写道:
Reply all
Reply to author
Forward
0 new messages