Topic "RFE: Change directory when opening PuTTY session"

Author Message
larsen
[View user's profile]

Joined: 2007-02-02
Posts: 24
Hi,

would it be possible to set the current directory in PuTTY to that one opened in WinSCP?


Lars
Advertisements
purefusion
[View user's profile]

Joined: 2009-04-08
Posts: 30
Location: Happyworld Land
I have also been wanting this behavior...
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24553
Location: Prague, Czechia
Thanks for your post. This issue is being tracked already. Suggestions are welcome.
_________________
Martin Prikryl
purefusion
[View user's profile]

Joined: 2009-04-08
Posts: 30
Location: Happyworld Land
prikryl wrote:
Suggestions are welcome.


Suggestions on what aspect?
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24553
Location: Prague, Czechia
purefusion wrote:
Suggestions on what aspect?

How to implement that technically. I'm not aware of any reliable solution. See discussions in threads linked from tracker.
_________________
Martin Prikryl
Gunar

Guest


Hello, thanks for entertaining this option. I would be also very interested in automatically opening a putty session in the same directory as it is currently viewed in WinScp.

How about using a file to transfer the target directory to the target machine?

Example:
~/.winscptargetdir

The user has to append the own .cshrc (.bashrc ...) on the target machine with:

Code:
set WINSCP_FNAME=$HOME/.winscptargetdir
if ( -f $WINSCP_FNAME) then
  cd `cat $WINSCP_FNAME`
  rm -f $WINSCP_FNAME
endif


Hence, before launching putty, WinScp creates the file in the user's home dir (it knows home dir already) with content of the target dir. Then, when the ssh session is established and initializes through .cshrc, it picks up the directory and deletes the file.

On regular login's the file will not be present and default behavior is not altered.

What can go wrong? Worst case if ssh session does not work out, the user would be redirected once to an old directory next time the user logs in normally (acceptable risk to me).

Thanks
Gunar

PS: Shell code to put into user ~/.*rc file depends on user shell.
Gunar

Guest


Hello,

I tried my own suggestion above using the custom commands and it works nicely!

Added a custom remote command "SSHDir":

Code:
echo !/ > ~/.winscptargetdir


Now, I just trigger the "SSHDir" command first (from toolbar), and then click on Putty button.

These two clicks get me into a putty ssh shell in the target directory -- NICE.

Certainly, an integration into WinSCP would be nicer.

Thanks in advance,
Gunar
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24553
Location: Prague, Czechia
Thanks for your suggestion!
_________________
Martin Prikryl
Bryanhoop

Guest


In .bashrc:
Code:
if [ -f $HOME/pwdputtyscript.sh ]
then
source $HOME/pwdputtyscript.sh
cd $WINSCP
rm -f $HOME/pwdputtyscript.sh
fi


In WinSCP commands:

Code:
echo WINSCP=!/ > ~/pwdputtyscript.sh
[/quote]
puttyperson2012

Guest


I wrote a little c# app to help with this, if anybody is interested. The steps were:

1. modify your .bashrc as mentioned above (i changed the variable name):
Code:
if [ -f $HOME/pwdputtyscript.sh ]
then
  source $HOME/pwdputtyscript.sh
  cd $PUTTYSTARTDIR
  rm -f $HOME/pwdputtyscript.sh
fi


2. Add a custom command in WinSCP to say: "C:\mydir\WinSCPPutty.exe" !U !P !@ !/
and give it a shortcut like Shift+Ctrl+Alt+P

3. Make a c# console app with the following in Program.cs:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace WinSCPPutty
{
    class Program
    {
        static void Main(string[] args)
        {
            //args = [user pass host dir]
            string user, pass, host, dir;
            if (args.Length < 3)
                return;

            if (args.Length < 4)
                dir = "~";

            user = args[0];
            pass = args[1];
            host = args[2];
            dir = args[3];

            try
            {

                string puttyexe = @"C:\Program Files (x86)\PuTTY\putty.exe"; //you'll need to edit the putty location as needed
                string filename = System.IO.Path.GetTempPath() + "cdputty.txt";

                string txt = "echo PUTTYSTARTDIR=" + dir + " > ~/pwdputtyscript.sh";

                string logintxt = user + "@" + host + " -pw " + pass;
                File.WriteAllText(filename, txt);

                ProcessStartInfo ps = new ProcessStartInfo(puttyexe, logintxt + " -m \"" + filename + "\"");
                ps.WindowStyle = ProcessWindowStyle.Hidden;
                Process p = Process.Start(ps);
                p.WaitForExit();

                File.Delete(filename);

                Process.Start(puttyexe, logintxt);
            }
            catch (Exception)
            {
            }
        }
    }
}


note: after creating the project, I noticed that setting project properties to output a "windows application" instead of console kept it from popping up unnecessary windows.



Now when you click the shortcut, it calls that new EXE and feeds it your current username, password, host, and current dir. The EXE then launches putty once (invisibly) to create the ~/pwdputtyscript.sh on the remote computer. It then launches putty, logs you in, and the .bashrc stuff brings you to the correct directory and cleans everything up. Importantly, no information about your username or password are ever saved in any file... they're all being transmitted between applications as arguments.

It's a hack to be sure, but it does the job.
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24553
Location: Prague, Czechia
puttyperson2012 wrote:
I wrote a little c# app to help with this, if anybody is interested.

Thanks for sharing this!
_________________
Martin Prikryl
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License