Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Putty command line : how to execute initialization commands and keep the session opened ?

1,265 views
Skip to first unread message

PiRanha

unread,
Aug 31, 2005, 2:43:02 AM8/31/05
to
Hello,

I would like to make a batch (.bat file) that automatically opens several
Putty sessions.
No problem for the moment. But moreover I want to execute some specific
commands for each session.
I tried to use the -m option of the Putty command line to execute the
commands set in a specified file but it seems that the Putty session is
closed after the command are executed.

Is there a way to do that ?
Thank in advance.

PS : the idea is to open a Putty session for each host where I have to check
some logs, to go to the proper log directory and make a tail -f on a log
file for instance.

PiRanha


Darren Dunham

unread,
Aug 31, 2005, 1:56:22 PM8/31/05
to
PiRanha <PiRan...@yahoo.fr> wrote:
> I would like to make a batch (.bat file) that automatically opens several
> Putty sessions.
> No problem for the moment. But moreover I want to execute some specific
> commands for each session.
> I tried to use the -m option of the Putty command line to execute the
> commands set in a specified file but it seems that the Putty session is
> closed after the command are executed.

After the commands finish running, why wouldn't the window close?
Perhaps you want to finish the commands with a shell that remains
running?

> PS : the idea is to open a Putty session for each host where I have to check
> some logs, to go to the proper log directory and make a tail -f on a log
> file for instance.

Maybe instead of running a command (like tail), you want to run a shell
and have it run the command, or just invoke a new shell at the end.

--
Darren Dunham ddu...@taos.com
Senior Technical Consultant TAOS http://www.taos.com/
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >

PiRanha

unread,
Aug 31, 2005, 2:47:04 PM8/31/05
to
Darren Dunham wrote :

> After the commands finish running, why wouldn't the window close?
> Perhaps you want to finish the commands with a shell that remains
> running?
>
>> PS : the idea is to open a Putty session for each host where I have to
>> check
>> some logs, to go to the proper log directory and make a tail -f on a log
>> file for instance.
>
> Maybe instead of running a command (like tail), you want to run a shell
> and have it run the command, or just invoke a new shell at the end.

Yes, I want to open a shell with some commands automatically launched at the
shell opening.

In fact the main idea is the following : assuming I have an applicative
component hosted by 4 machines (A, B, C, D). If I have to analyse an issue
concerning that component I'd like to be able to get in one click (launching
my .bat file) one Putty session (shell) opened on each of the 4 machines,
with current directories set to the directory containing the log of the
component (using a "cd" command) and, why not, a "tail -f" on the log file.

It a question of efficiency : I'd like to avoid me wasting time to manually
open the 4 sessions, go into the right directories, view the log files.

PiRanha


PiRanha

unread,
Aug 31, 2005, 2:47:04 PM8/31/05
to
Darren Dunham wrote :

> After the commands finish running, why wouldn't the window close?
> Perhaps you want to finish the commands with a shell that remains
> running?
>
>> PS : the idea is to open a Putty session for each host where I have to
>> check
>> some logs, to go to the proper log directory and make a tail -f on a log
>> file for instance.
>
> Maybe instead of running a command (like tail), you want to run a shell
> and have it run the command, or just invoke a new shell at the end.

Yes, I want to open a shell with some commands automatically launched at the

Darren Dunham

unread,
Aug 31, 2005, 3:56:48 PM8/31/05
to
PiRanha <PiRan...@yahoo.fr> wrote:
> Darren Dunham wrote :

> Yes, I want to open a shell with some commands automatically launched at the
> shell opening.

So this isn't really a putty question, it's trying to figure out how to
get a shell to do that.

I tried some simple tacks of executing 'tail ; $SHELL', but I never got
the shell to invoke (although $SHELL ; tail ; $SHELL seemed to work...).

I don't have a simple answer. Maybe a shell script on the box that did
the tail, then ran a shell. Run that script from putty...

PiRanha

unread,
Sep 1, 2005, 2:18:29 AM9/1/05
to
"Darren Dunham" <ddu...@redwood.taos.com> wrote :

> PiRanha <PiRan...@yahoo.fr> wrote:
>> Darren Dunham wrote :
>> Yes, I want to open a shell with some commands automatically launched at
>> the
>> shell opening.
>
> So this isn't really a putty question, it's trying to figure out how to
> get a shell to do that.
>
> I tried some simple tacks of executing 'tail ; $SHELL', but I never got
> the shell to invoke (although $SHELL ; tail ; $SHELL seemed to work...).
>
> I don't have a simple answer. Maybe a shell script on the box that did
> the tail, then ran a shell. Run that script from putty...

May be it is not possible with the current version of Putty :(
However, if it is the case, I think it would be a nice feature.

PiRanha


Newsgroup User 69

unread,
Sep 1, 2005, 3:55:00 AM9/1/05
to PiRanha
Kick off a saved putty session with:
'putty -l <username_with_rights_to_log> -load "<sessionname>"'
(double-quotes seem to be required around the sessionname) Have that
session set to execute:
'/usr/bin/tail -f <path/to/logfile.log>'
(from the main SSH options screen)
You'll be prompted for your password unless you're using keys.

That should work just fine, but as always, YMMV.

If you want a batch file to kick off multiple sessions then you would
basically create your file with the following lines:

----
putty -l <username_with_rights_to_log> -load "<sessionname0>"
putty -l <username_with_rights_to_log> -load "<sessionname1>"
putty -l <username_with_rights_to_log> -load "<sessionname2>"
etc...

Darren Dunham

unread,
Sep 1, 2005, 1:14:19 PM9/1/05
to
Newsgroup User 69 <ngus...@comcast.net> wrote:
> Kick off a saved putty session with:
> 'putty -l <username_with_rights_to_log> -load "<sessionname>"'
> (double-quotes seem to be required around the sessionname) Have that
> session set to execute:
> '/usr/bin/tail -f <path/to/logfile.log>'
> (from the main SSH options screen)
> You'll be prompted for your password unless you're using keys.

> That should work just fine, but as always, YMMV.

And after the tail exits you get a shell prompt in the window? That's
what the OP wanted and what I couldn't recreate easily.

Getting the tail to run by itself (with putty exiting after the tail
does) is trivial.

PiRanha

unread,
Sep 1, 2005, 1:14:21 PM9/1/05
to
"Newsgroup User 69" wrote

> Kick off a saved putty session with:
> 'putty -l <username_with_rights_to_log> -load "<sessionname>"'
> (double-quotes seem to be required around the sessionname) Have that
> session set to execute:
> '/usr/bin/tail -f <path/to/logfile.log>'
> (from the main SSH options screen)
> You'll be prompted for your password unless you're using keys.
>
> That should work just fine, but as always, YMMV.
>
> If you want a batch file to kick off multiple sessions then you would
> basically create your file with the following lines:
>
> ----
> putty -l <username_with_rights_to_log> -load "<sessionname0>"
> putty -l <username_with_rights_to_log> -load "<sessionname1>"
> putty -l <username_with_rights_to_log> -load "<sessionname2>"
> etc...

Indeed, by doing this, the "tail -f" is well done on my log file but as soon
as I make CTRL-C to get the prompt back the window is closed. I'd like to
keep the session opened to be able to manually type other commands.

PiRanha


Newsgroup User 69

unread,
Sep 1, 2005, 1:48:25 PM9/1/05
to PiRanha
OK - I did not realize you wanted to work in that window.
I have Cygwin-X installed and typically just display back my tail(s)
into an xterm locally on my Windows PC.

What I do is have a script or alias on each of my maintained hosts, I
login to those with the -X option or have it enabled in the PuTTY
session and execute the local command which displays an xterm back
to my PC with my logfiles.

Command to tunnel through SSH
xterm -geom 132x34 -title root@<host> -name root@<host> -e tailnet &

Command to run direct to local PC:
/usr/bin/xterm -display your.pc.local:0.0 -geom 132x34 -title
root@<host> -name root@<host> -e tailnet &
(i usually prepend nohup to the second so I can close out the session
and have the xterm still running)

---
tailnet
---
#!/bin/sh
cd /var/log
/usr/bin/tail -f maillog mysqld.log radius/radius.log &
cd /var/log/httpd
/usr/bin/tail -f site1.com-access_log site1.com-error_log &
exit

---

Newsgroup User 69

unread,
Sep 1, 2005, 2:05:51 PM9/1/05
to
Sorry to respond to my own post but I was messing around and here's a
simple solution if you want the tail to spawn in the same window you're
working in.

create a script on the local machine and make it user-executable

---Test---
#!/bin/sh
/usr/bin/tail -f /var/log/messages /var/log/syslog &
/bin/bash
/usr/bin/killall tail
...
---

have PuTTY Execute the script when connecting the same way we did the
tail previously (just put the /path/to/script in the remote command
field). This will execute the tail, background it and spawn a shell.
The last line, which will execute when you exit the shell, is a very
crude cleanup which would be better done with some logic but you can
extrapolate as needed.

hope one of those two solutions helps....

0 new messages