LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-01-2019, 01:34 PM   #1
bsmile
Member
 
Registered: Oct 2017
Posts: 98

Rep: Reputation: Disabled
Angry how to go through each terminal and execute commands on a remote server?


Internet is an issue for me, so I try to disconnect it when not needed. But I from time to time need to open multiple terminals and connect to a remote server and run commands there. As the internet is disconnected, the remote connections to the server are also lost. I need to manually establish all the remote connections and cd to the right directories and execute the last command to keep up with my work. So, my question is, is there a way to write a script which goes through each terminal, execute commands on the remote server to print out the default directories and the last command executed on that terminal? Or, do you have other suggestions to help me control myself on internet?
 
Old 03-01-2019, 01:40 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,289
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
You might consider running a terminal multiplexer like tmux on your remote machines. Then when you re-attach you will re-join your session in progress.

Code:
ssh-add ~/.ssh/some.key.ed25519;
while ! ssh -t -i ~/.ssh/some.key.ed25519 -l bsmile server01.example.com 'tmux a || tmux'; do
        sleep 5;
done;
There are other possibilities too. You can load the SSH command, along with all the options, into .desktop files and then launch everything with a click.

Can you give more detais about your work flow?
 
1 members found this post helpful.
Old 03-01-2019, 02:26 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,879
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Sounds like instead of disabling your network connection, you should cause your router to stop allowing internet access. Then you won't have this problem of attaching to your server.

This is all presuming that the server is located in the same local network, and also that this is a network which you own.
 
Old 03-01-2019, 02:38 PM   #4
bsmile
Member
 
Registered: Oct 2017
Posts: 98

Original Poster
Rep: Reputation: Disabled
to Turbocapitalist: that's a great idea, I just checked tmux was not installed on the remote server. If to use tmux to achieve the goal, does it need admin's privilege or can it be my user installation?

to rtmistler: unfortunately, it is not within the same local network.
 
Old 03-01-2019, 02:50 PM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
i heard that for sketchy internet connections, mosh can be a good replacement for ssh.
always meant to try it, never did, now i don't need it anymore...
 
Old 03-01-2019, 02:51 PM   #6
bsmile
Member
 
Registered: Oct 2017
Posts: 98

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
There are other possibilities too. You can load the SSH command, along with all the options, into .desktop files and then launch everything with a click.

Can you give more detais about your work flow?
I might work on different tasks and open different number of terminals each time for an extended number of days. Whenever internet gets interrupted, I need to reset the internet and reopen all the terminals, ssh to the intended remote servers and then go to the former directories and run the latest command there.

The intended operation in my mind is to run a script to collect all the directories and latest command on each terminal on a specific remote server, rewrite these information into a script and save it. Next time, I run the script once and all the sessions are restored in one click/return.
 
Old 03-01-2019, 03:10 PM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by ondoho View Post
i heard that for sketchy internet connections, mosh can be a good replacement for ssh.
forgot to add:
it survives connections that get interrupted.
 
Old 03-02-2019, 12:03 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,289
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
Quote:
Originally Posted by bsmile View Post
If to use tmux to achieve the goal, does it need admin's privilege or can it be my user installation?
Your system administrator can install tmux on the remote machine and then any account there on the machine can use it.

If you need shortcuts, the while loop above can be made into a shell function and then invoked with just a single command.

Code:
function server01() { while ! ssh -t server01 "tmux a || tmux"; do sleep 5; done; }
That example could then be invoke with just entering "server01" into the shell. See "man bash" and scroll way, way, way down to the section "Shell Function Definitions"

Quote:
Originally Posted by bsmile
I need to reset the internet and reopen all the terminals
Or do the terminal programs on the machine you are connecting from get closed too? Those would be xterm, xfce4-terminal, guake, gnome-terminal, terminator, konsole, or any of the others. If those are closing too, then the script can still open them and launch an SSH session to use tmux on the remote machine. For that you could have a loop and inside that a loop to check for network connectivity and then after that loop it would fire up all the terminals using using the --title and --execute options.
 
1 members found this post helpful.
Old 03-02-2019, 04:24 PM   #9
bsmile
Member
 
Registered: Oct 2017
Posts: 98

Original Poster
Rep: Reputation: Disabled
Actually, the administrator disallowed installing tmux, thus I might not have access to the convenience of this software. Can you introduce script based method?
 
Old 03-02-2019, 11:28 PM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,289
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
If the server is properly set up it will have a termnal multiplexor. Maybe screen is there instead of tmux.

Code:
ssh-add ~/.ssh/some.key.ed25519;
while ! ssh -t -i ~/.ssh/some.key.ed25519 -l bsmile server01.example.com \
    'screen -dR'; do
        sleep 5;
done;
However, tmux is part of the set of basic packages in many server distros and even some other operating systems. For example it is part of OpenBSD's base, if that means anything to you.

The system administrator's job is to help you do your job so I would try again and find out what impediment needs to be cleared to get it in place. Obviously they would not let you install it but they definitely should install it. Or are you dealing with M$ resellers posing as IT people? I've run into those a lot and they'll argue in not so many words that they don't know how to install software. Whatever. Be diplomatic. Maybe

As for wasting time and effort on trying to achieve something similar from the client side, it could go a little like this:

Code:
ssh-add ~/.ssh/some.key.ed25519;
while ! ssh -t -i ~/.ssh/some.key.ed25519 -l bsmile server01.example.com \
    'cmnd1; cmnd2; cmnd3; ...; exec bash;'; do
        sleep 5;
done;
However, that will not rejoin your session in progress. That will simply make a new connection and re-run all the commands each time the network connection is re-established.
 
Old 03-03-2019, 01:22 AM   #11
bsmile
Member
 
Registered: Oct 2017
Posts: 98

Original Poster
Rep: Reputation: Disabled
@Turbocapitalist Thanks for your help, I think the admin simply doesn't want too many dead/unattended sessions to hang around on the head node. The cluster is mainly for computation purpose, thus rapid head-node response is required. Also security is their priority concerns.

Sending command to serve via ssh while establishing new connection seems to work fine, but how about running a script on one terminal to access other terminals which already have remote connections established to print out the current directories and latest commands executed there?
 
Old 03-03-2019, 01:31 AM   #12
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,289
Blog Entries: 3

Rep: Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718Reputation: 3718
Quote:
Originally Posted by bsmile View Post
... but how about running a script on one terminal to access other terminals which already have remote connections established to print out the current directories and latest commands executed there?
That sounds a bit more complicated to solve. Do you mean that you wish to go back and capture the output of commands that have already run? It might be easier to capture the session before running the commands and then filter out what you don't need. Can you explain the task a little more?
 
1 members found this post helpful.
Old 03-03-2019, 10:42 AM   #13
bsmile
Member
 
Registered: Oct 2017
Posts: 98

Original Poster
Rep: Reputation: Disabled
I have opened a sequence of terminals to work on. I am then afraid of unexpected interruption in internet connection, so I would like to save a copy of the current status of all the terminals and save them into a script file so that if an interruption did happen, I can restore my working status by running the script in a terminal of my local machine. This could save me efforts to open multiple terminals again, ssh to the remote machine and go to the intended directories.
 
Old 03-03-2019, 01:46 PM   #14
Pearlseattle
Member
 
Registered: Aug 2007
Location: Zurich, Switzerland
Distribution: Gentoo
Posts: 999

Rep: Reputation: 142Reputation: 142
I agree so far with Turbocapitalist's recommendations.

Just to be 100% sure: you're opening "many" ssh sessions to run multiple commands/programs on only 1 host (not to run the same program on many hosts), right?
If this is correct then I cannot think of anything else => maybe it would be worth trying to get management permission to install "tmux" or a similar tool by stating how many hours of work you would save by having it... .
 
Old 03-03-2019, 01:56 PM   #15
bsmile
Member
 
Registered: Oct 2017
Posts: 98

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Pearlseattle View Post
Just to be 100% sure: you're opening "many" ssh sessions to run multiple commands/programs on only 1 host (not to run the same program on many hosts), right?
Yes, that's the way I am doing now. Since Turbocapitalist and you have strongly recommended tmux, I have written an email specifically requesting installing it there. Let's see what is the admin's response.
 
  


Reply

Tags
remote login, scripting, terminal


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sudo connect to a remote server and execute scripts in remote server masubram Linux - Newbie 6 07-31-2016 09:35 AM
[SOLVED] execute remote commands using plink -m commands jcolinbx Linux - General 3 12-04-2014 11:36 AM
How to execute multiple commands on remote server AshwiniIngale Programming 14 01-26-2013 12:54 PM
Execute script on local server as normal user to run commands on remote server ALInux Linux - Software 1 01-01-2010 06:30 AM
Execute commands or scripts on a remote server gurl4sh25 Linux - Software 3 06-27-2007 09:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration