Перейти к содержимому

How to run a remote command in PuTTY after login & keep the shell running?

32

What I'm trying to do: start a PuTTY session from the command line, login to remote machine and cd to provided directory.

putty.exe -agent -ssh some.host 

That will open a session & login with my default login name & private key.

echo cd /some/remote/path/ > c:/stuff/cmd.txt putty.exe -agent -ssh some.host -m 'c:/stuff/cmd.txt' 

That will open a session, login, execute a command (cd in this case) and exit.

How do I open a session, login, cd and keep the session open?

Background: I use emacs under windows and often edit files on remote Unix machines using tramp & plink. I want to make a hotkey that opens a PuTTY session for that remote machine and chdirs to the directory of that file. Not a big deal on emacs side, but I'm stuck with PuTTY.

132 511 просмотров
Artyom V. Kireev спросил 13 лет назад
A 263

2 ответа

30
Принятый ответ

What the -m does is, that it makes PuTTY instruct the SSH server to start that command(s) INSTEAD of a shell. So once your command finishes, so does the session.

If you want to run the shell after the cd command, you need to add it explicitly to your cmd.txt, like:

cd /my/path ; /bin/bash 

Also the -m implies "nopty"/non-interactive mode. To use an interactive shell you need to override that using the -t switch.

putty.exe -ssh example.com -m "c:\path\cmd.txt" -t 

Alternatively use KiTTY with its -cmd switch, that does what you want (and does not need a temporary file).

Я попробовал это (добавив / bin / bash в cmd.txt), и это не работает. Насколько я понимаю, bash запускается в неинтерактивном режиме и, не имея кода для выполнения, завершается. Есть ли способ запустить интерактивную оболочку таким образом?

Artyom V. Kireev · 13 лет назад · 0

Тем временем я пытался запустить mc. :-) Это не так, и предоставил некоторый вывод: Невозможно получить настройки терминала: неверный аргумент (22) Требуется установить переменную среды TERM.

Artyom V. Kireev · 13 лет назад · 0

Спасибо за рекомендацию здесь, но у меня, кажется, есть проблема, пытаясь использовать putty -m" myfile.sh "-t для подключения к tinycorelinux vm (/ bin / ash). Я получу следующую ошибку: http://i.imgur.com/QN5oRXC.png (мой скрипт начинается с echo" .. ", он всегда будет показывать первую букву в моем скрипте после недопустимых символов

Vincent De Smet · 11 лет назад · 0

если я использую настроить удаленную команду в профиле замазки, она работает

Vincent De Smet · 11 лет назад · 0
Martin Prikryl ответил 13 лет назад
M 10 692
-1

The tramp session you're starting in emacs and the shell session you run via putty are completely separate processes.

You're probably better off checking configuration options for tramp to see if there's a way to set a default directory per host within your emacs configuration. Maybe start with the tramp-remote-path configuration option as documented here.

If you need to use putty to establish the connection, you probably want to set up an ssh tunnel and have emacs/tramp connect through that.

Emacs и tramp здесь не имеют значения, я добавил это только для фона. Дело не в редактировании файла в emacs, а в открытии оболочки для выполнения чего-то еще с этим файлом (например, запуска скрипта). Я устал от «запуска шпаклевки, выберите хост, затем перейдите к нужному пути» и хочу автоматизировать это.

Artyom V. Kireev · 13 лет назад · 0

Хорошо, спасибо за разъяснения.

Doug Harris · 13 лет назад · 0
Doug Harris ответил 13 лет назад
D 20 102