How to automate opening multiple ssh servers with distinct session name in iterm2? - ssh

I am automating my developing setup and found an appplescript that opens multiple terminals and ssh into different servers. It works great but I want to change the tab names so that I can distinguish between tabs.

First make sure that the profile that you are using has the following "Terminal" option set:
Now you are set the tab and/or window title:
tell current session
write text "echo -ne \"\\033]1; A Tab Title \\007\""
write text "echo -ne \"\\033]2; A Window Title \\007\""
end tell

Related

Change Integrated Terminal title in vscode

We can open command prompt in vscode by using the Integrated Terminal feature in View menu.
We can even open multiple terminals as shown below:
Is there any way I can change the title of the terminal ?
1: cmd.exe will be build terminal
2: cmd.exe will be watch terminal
I have gone through the integrated terminal documentation but I didn't find a way to do that.
Press in windows Ctrl + Shift + P and after type: Terminal: Rename, there you can change the terminal name/title
In v1.61 there is the ability to set the terminal names using variables. See terminal custom titles in release notes.
Terminal names are traditonally the name of the process they're
associated with. Thus, it can be difficult to distinguish between
them.
We now support configuring both title and description to help with
this using variables described in terminal.integrated.tabs.title and
terminal.integrated.tabs.description settings.
The current default values are:
{
"terminal.integrated.tabs.title": "${process}",
"terminal.integrated.tabs.description": "${task}${separator}${local}${separator}${cwdFolder}"
}
Variables available are:
${cwd} - The terminal's current working directory
${cwdFolder} - The terminal's current working directory.
${workspaceFolder} - The workspace in which the terminal was launched.
${local} - Indicates a local terminal in a remote workspace.
${process} - The name of the terminal process.
${separator} - A conditional separator (" - ") that only shows when surrounded by variables with values or static text.
${sequence} - The name provided to xterm.js by the process.
${task} - Indicates this terminal is associated with a task.
It looks like the ${task} variable is what you are looking for.
Sometimes, plugins will remove default keyboard shortcut bindings.
Look for "terminal.rename" in keyboard shortcuts then edit the keyboard shortcut to your preferred shortcut.
To apply your shortcut, make sure your cursor is focus in edit part of the window before you key in. Not at the terminal part.
In v1.41 there is a new command which can be used like so:
{
"key": "ctrl+t",
"command": "workbench.action.terminal.renameWithArg",
"args": {
"name": "remote"
}
}
if you have some frequently used name, like "remote" or "build" that you use often.
With VSCode 1.63 (Nov. 2021), entering "<blank>" (ie., empty string) as name will restore the default name for that terminal.
See issue 134020
For instance:
If my setting is ${cwd}${separator}${process} and I name a terminal "foo".
How do I reset "foo" back to the value of ${cwd}${separator}${process}?
My suggestion was that if you attempted to submit a blank name that would automatically reset to the value of your setting.

Renaming a Yakuake session from commandline

Yakuake provides a hotkey and a GUI way to rename commandline tabs/sessions.
I'd like to do the same via the command line, so I can script it and use it in an alias. (My goal is that if I use an alias which does an SSH to some server, then the tab is renamed according to this servers name...)
I tried the suggestions shown here Renaming a Konsole session from commandline after ssh so far no luck.
Since KDE4, one should use qdbus to control KDE apps (instead of deprecated and deleted DCOP).
For example, to change a title of the first session one may use:
qdbus org.kde.yakuake /Sessions/1 org.kde.konsole.Session.setTitle 1 "New title"
To explore available interfaces, methods and properties one may use qdbusviewer.
As a homework try to get a list of active sessions (before you going to change smth).
Like #fgysin pointed out, his command also works for me. BUT it needs the ` character and not " for the subcommand :
qdbus org.kde.yakuake /yakuake/sessions org.kde.yakuake.activeSessionId
It gives :
qdbus org.kde.yakuake /yakuake/tabs org.kde.yakuake.setTabTitle `qdbus org.kde.yakuake /yakuake/sessions org.kde.yakuake.activeSessionId` "NEW TAB TITLE";

Create a file with content through ssh without a text editor

I am aware that it is possible to create a file in ssh through the "touch" command then editing it using text editors such as vi or pico to edit them and add content.
I was wondering if it was possible to create a file and add it's contents in one command line?
Something like:
[create file command] [filename.txt] ["this is the contents of filename.txt"]
The reason I'm asking if this was possible is because I have an ssh client in Go where a session only accepts one call to run and I plan to use this for an app that is automated with no user inputs so I want to avoid using text editors.
Here's one solution:
ssh [user#]hostname 'echo "this is the contents of filename.txt" > <path/filename>'
The echo with ">" overwrites an existing file, or creates a new file if it doesn't exist.
Replace ">" with ">>" to append the text to an existing file.
Cheers

running parts of shell script in background

I have a shell script which asks for user input and depending on the input opens db connection using sqlplus and run some sql querys like drop table /create table/select/update. Is it possible that the sql part be run as background job,so that even if i lose vpn connectivity to the network,all the sql queries gets executed.
Also ,when the sql parts gets completed and user is prompted with another input the shell script comes to foreground and after getting the input again goes to background?
I have found some questions which tell us how to run the script in background,but i want to run ONLY some parts of the same script in background if possible(and come to foreground for user input).Though i can make multiple scripts too handle it(dividing the scripts in parts which needs to be called in background and calling them though another script),i would rather do it in a single script if possible.
You can break your main script up into functions / smaller scripts to achieve the desired behavior of a mix of background processes and foreground processes.
For example, in your main script:
#!/bin/sh
echo "Starting script..."
# do so more stuff here, maybe ask user for input
./run_background_process_1 &
# ask the user for some more input
./run_background_process_2 &
...
Use the & symbol at the end of script calls to denote that they should be run in the background.
(Updated) If you'd like to keep everything in 1 script, use functions to break up / encapsulate the parts of logic that you would like to run in the background. Call these functions by suffixing the call with &, same as above.
You can try the following example to see that it works:
#!/bin/sh
hello() {
condition="yes"
while [[ $condition== "yes" ]]
do
echo "."
sleep 1
done
}
# Script main starts here
echo "Start"
hello &
echo "Finish"
Remove the & after hello and you'll see that it behaves differently.
There are tools which allow you to keep scripts running despite loss of connection. For example, check out http://www.gnu.org/software/screen/ - one of its features is Programs continue to run when their window is currently not visible and even when the whole screen session is detached from the users terminal.
After search on internet i found out i can use three methods to make the script background :
1) using bg: How do I put an already-running process under nohup? .but unfortunately ,this didnt worked for me in ksh shell.
2) using coprocesses
3) using nohup
I decided to go with nohup as it was easier to implement. The sqlplus part which needed to be run in background ,i made another script of it and called it from the main script using nohup
nohup script-name.ksh ${parameter1} ${paramter2} &
This worked for me.

listing current working directory when seeing all open shells in a screen session

I use screen and start a couple of shells within it. Now, if I list all shells using Ctrl-a " , then, all the windows are listed with a generic "bash" label. How can I change this, for e.g. so that they also list the current working directory of that window also.
Never mind, I figured it out. What is displayed via the Ctrl-A " sequence is the xterm window name, so you have to set that to something else.
So, for bash, put this in the .bashrc
PROMPT_COMMAND='
if [ $TERM = "screen" ]; then
echo -n -e "\033k$MYPWD\033\\"
fi
'