RFE: Change directory when opening PuTTY session

Advertisement

larsen
Joined:
Posts:
25

RFE: Change directory when opening PuTTY session

Hi,

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


Lars

Reply with quote

Advertisement

purefusion
Joined:
Posts:
30
Location:
Happyworld Land

Re: RFE: Change directory when opening PuTTY session

martin wrote:

Suggestions are welcome.

Suggestions on what aspect?

Reply with quote

martin
Site Admin
martin avatar

Re: RFE: Change directory when opening PuTTY session

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.

Reply with quote

Advertisement

Gunar
Guest

Re: RFE: Change directory when opening PuTTY session

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:

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.

Reply with quote

Gunar
Guest

Re: RFE: Change directory when opening PuTTY session

Hello,

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

Added a custom remote command "SSHDir":

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

Reply with quote

Bryanhoop
Guest

My take on it

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

In WinSCP commands:

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

Reply with quote

Advertisement

puttyperson2012
Guest

c#.net workaround

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):
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:
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.

Reply with quote

martin
Site Admin
martin avatar

Re: c#.net workaround

puttyperson2012 wrote:

I wrote a little c# app to help with this, if anybody is interested.
Thanks for sharing this!

Reply with quote

Guest

I have found this page where it is solved: <removed by admin>

I enhanced it a little bit for my needs;
%ProgramFiles%\PuTTY\putty.exe -t -m "%TEMP%\putty.txt" !`cmd.exe /c echo cd "!/" ; $($SHELL) > "%TEMP%\putty.txt"`

Remark
I use PuTTY as installed program not the portable version

So what does it do?
It creates a temporary file putty.txt in you temporary directory.
This will contain the commands which are to be called on the remote server

Example:
cd "/home/user"; $($SHELL)

See PuTTY documentation:
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter3.html

Reply with quote

Advertisement

You can post new topics in this forum