Execute a command after ssh exits


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execute a command after ssh exits
# 1  
Old 03-10-2016
Execute a command after ssh exits

In my .tcshrc I define and run an alias to set my terminal title. The title contains the hostname. I do this because I ssh to other machines a lot and I always need to know which host I am typing in. The alias is defined and run in the .tcshrc like this...

Code:
alias tt 'echo -n "^[]0;`hostname -s`^G"'
tt
#

(where "^[" and "^G" are special control characters)

This is great when I ssh into a machine. The problem is when I exit the ssh, because the title will still show the now-old hostname. I have to manually run tt again after I exit ssh.

Is there any way this can be done automatically?
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 03-10-2016 at 11:04 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 03-10-2016
Quote:
Originally Posted by salvadorjoshua
In my .tcshrc I define and run an alias to set my terminal title. The title contains the hostname. I do this because I ssh to other machines a lot and I always need to know which host I am typing in. The alias is defined and run in the .tcshrc like this...

Code:
alias tt 'echo -n "^[]0;`hostname -s`^G"'
tt
#

(where "^[" and "^G" are special control characters)

This is great when I ssh into a machine. The problem is when I exit the ssh, because the title will still show the now-old hostname. I have to manually run tt again after I exit ssh.

Is there any way this can be done automatically?
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.
I don't use tcsh (or other csh derivatives), but with the Bourne shell and its derivatives you can set PS1 (the prompt string that is written to the terminal as a prompt to enter the next command when the last executed command completes. You could run tt in a command substitution when the prompt string is printed or (as many people do) you could print the hostname and current working directory as part of the command prompt. I assume csh and its derivatives have a similar method of setting the command prompt that is written when the shell is ready to read a command line when you are running the shell interactively.
# 3  
Old 03-11-2016
The $PS1 in sh corresponds to $prompt in tcsh.
For example, set your shell prompt to
Code:
set prompt="$USER@`hostname -s`:^[]0;`hostname -s`^G "

Then your terminal title is updated with every shell prompt.
# 4  
Old 03-11-2016
Thanks Don and Germany. This is the right train of thought.

To get it to work I had to escape the string that sets the terminal title. Like this...

Code:
set prompt="$USER@`hostname -s`:%{^[]0;`hostname -s`^G%} "

*Note: This escaped string cannot be at the end of the $prompt string. You'll notice a space character after the escaped string in the example above. If you don't want that last space character, put the escaped string in front...

Code:
set prompt="%{^[]0;`hostname -s`^G%}$USER@`hostname -s`:"

Thanks for your help, guys. Problem solved.
# 5  
Old 03-11-2016
Did you consider that for neither the hostname nor the username you need to run an external program? man tcsh:
Quote:
prompt
The string which is printed before reading each command from the terminal. prompt may include any of the following formatting sequences (+), which are replaced by the given information:
.
.
.
%M The full hostname.
%m The hostname up to the first `.'.
.
.
.
%n The user name.

Last edited by RudiC; 03-11-2016 at 01:46 PM..
# 6  
Old 03-11-2016
Good point.
Code:
set prompt="%{^[]0;%m^G%}%n@%m:"

is more efficient (and portable to non-Linux systems).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execute ssh command with additional terminal command to any remote user not working script

Hello i am having an issue with bash script and this is the code now=$(cat hosts1.txt | awk '{print $2;}') while read n ;do ssh root@$now 'useradd test1; echo -e "test1\ntest1" | passwd test1 && echo "test1 ALL=(ALL:ALL) ALL" >> /etc/sudoers' When i execute only part with cat, it... (8 Replies)
Discussion started by: tomislav91
8 Replies

2. Shell Programming and Scripting

Ssh bash script exits without remote command completion

Hi, My goal is to connect from unix server A to windows server B and call a bat file on windows. I am able to succeed in remoting to windows and executing a command, the issue i am facing is the shell scrip is exiting without making sure of bat file success. Can you please help me in... (4 Replies)
Discussion started by: pxp018
4 Replies

3. Shell Programming and Scripting

Execute command on remote host via ssh

How should i make the following code working #!/bin/bash INPUTFILE="test.txt" while read STRING; do IP=`host -t A $STRING | awk '{print $NF}'` HOSTNAME=`ssh -oPasswordAuthentication=no -oStrictHostKeyChecking=no $IP "hostname"` echo $HOSTNAME > out.txt done < $INPUTFILE At this moment while... (3 Replies)
Discussion started by: urello
3 Replies

4. Shell Programming and Scripting

Execute command using ssh server 'cmd'

Hi The command below does not work as it require to take command in the breakers But If I do so the variable values get lost ssh testserver01 'dsmc q b "${ARCHIVE_DIR}*" -sub=yes -querysummary -inactive -fromd="${BACKUP_DATE}"' Thank you. (3 Replies)
Discussion started by: zam
3 Replies

5. Shell Programming and Scripting

execute ssh command via perl

Hi I have a perl command that doesn't seem to be working correctly. It appears to be fine but even when i try and run it manually same thing. Can someone take a look at this and tell me what they think the problem could be? Here is the perl Line: system ("echo 'ssh -t -t $user\@$_ \"cd... (3 Replies)
Discussion started by: vpundit
3 Replies

6. UNIX for Dummies Questions & Answers

ssh command to execute shell script in another server

ssh -q <hostname> /opt/tcs/satish/tst.ksh ssh -q <anotherserver> /opt/tcs/satish/tst.ksh tst.ksh has "nohup <command> & " when i execute below script , its throwing error as nohup can not be found ssh -q <anotherserver> /opt/tcs/satish/tst.ksh > log & can someone let me... (5 Replies)
Discussion started by: only4satish
5 Replies

7. Shell Programming and Scripting

ssh execute command remotely

Hi all, Today I want to write a script to run the commands remotely. If I run the command as follows: ssh <user>@<ip> 'ls; pwd' it works fine. But when I want to use ssh to set view in clearcase, it will lose the response. as follows ssh <user>@<ip> 'cleartool setview <view_name>; pwd'... (1 Reply)
Discussion started by: Damon_Qu
1 Replies

8. Shell Programming and Scripting

SSH execute remote command (with jump)

Hi all, I am facing the following issue: Host A should execute a remote command (say pwd) on host B2. B2 is not directly reacheable but you have to connect from a to B1, then from B1 you can execute the command ssh user@B2 pwd. B1 and B2 are directly connected: A => B1 => B2 | ... (3 Replies)
Discussion started by: Evan
3 Replies

9. Shell Programming and Scripting

ssh to remote host and execute command

Hi, could anyone please tell me how to ssh to remote host foo and execute command on it and print the result on local host? Thanks, Paresh (1 Reply)
Discussion started by: masaniparesh
1 Replies

10. Shell Programming and Scripting

How to execute remote ssh command - Perl and CGI

Hi, I am having nightmare issue-ing remote ssh command from a CGI perl script. It just won't run on debug message: It says permission denied. Can I even do this? as the apache server running under DAEMON account probably can't execute it? Is this the case of what's going on? Here is my... (3 Replies)
Discussion started by: Dabheeruz
3 Replies
Login or Register to Ask a Question