Retaining SSH session in bash script The 2019 Stack Overflow Developer Survey Results Are InHow to determine if a bash variable is empty?Need help with executing and deleting remote bash script via a local bash scriptSSH: Access local file (redirect local file contents) via remote SSH consoleSSH not seeming to run in bash script called via web serverSSH - set env vairables by every connection - godaddy shared hostTerminate remote process when backgrounded ssh session is terminatedRemote bash script execution via ssh causes local script to stopPause BASH script to allow user to enter SSH passwordHow to exit a SSH connection in a bash scriptUse bash script to send commands to SSH client?
Do these rules for Critical Successes and Critical Failures seem Fair?
How to type this arrow in math mode?
What did it mean to "align" a radio?
Falsification in Math vs Science
Shouldn't "much" here be used instead of "more"?
Can one be advised by a professor who is very far away?
What is the meaning of Triage in Cybersec world?
Can you compress metal and what would be the consequences?
What is the motivation for a law requiring 2 parties to consent for recording a conversation
Write faster on AT24C32
Output the Arecibo Message
Which Sci-Fi work first showed weapon of galactic-scale mass destruction?
Is an up-to-date browser secure on an out-of-date OS?
Can someone be penalized for an "unlawful" act if no penalty is specified?
"as much details as you can remember"
How technical should a Scrum Master be to effectively remove impediments?
FPGA - DIY Programming
Does a dangling wire really electrocute me if I'm standing in water?
Where to refill my bottle in India?
What to do when moving next to a bird sanctuary with a loosely-domesticated cat?
Why do we hear so much about the Trump administration deciding to impose and then remove tariffs?
Can a rogue use sneak attack with weapons that have the thrown property even if they are not thrown?
Why was M87 targetted for the Event Horizon Telescope instead of Sagittarius A*?
How can I autofill dates in Excel excluding Sunday?
Retaining SSH session in bash script
The 2019 Stack Overflow Developer Survey Results Are InHow to determine if a bash variable is empty?Need help with executing and deleting remote bash script via a local bash scriptSSH: Access local file (redirect local file contents) via remote SSH consoleSSH not seeming to run in bash script called via web serverSSH - set env vairables by every connection - godaddy shared hostTerminate remote process when backgrounded ssh session is terminatedRemote bash script execution via ssh causes local script to stopPause BASH script to allow user to enter SSH passwordHow to exit a SSH connection in a bash scriptUse bash script to send commands to SSH client?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a bash script which loops over a server list, runs a couple of remote commands via SSH, and writes the output of those commands to a local file.
For every command I run on the remote server I open a new SSH session.
Is there a way that only one SSH session remains open to a server, and that I can run multiple commands which the output I can redirect to a local file?
linux ssh bash scripting
add a comment |
I have a bash script which loops over a server list, runs a couple of remote commands via SSH, and writes the output of those commands to a local file.
For every command I run on the remote server I open a new SSH session.
Is there a way that only one SSH session remains open to a server, and that I can run multiple commands which the output I can redirect to a local file?
linux ssh bash scripting
add a comment |
I have a bash script which loops over a server list, runs a couple of remote commands via SSH, and writes the output of those commands to a local file.
For every command I run on the remote server I open a new SSH session.
Is there a way that only one SSH session remains open to a server, and that I can run multiple commands which the output I can redirect to a local file?
linux ssh bash scripting
I have a bash script which loops over a server list, runs a couple of remote commands via SSH, and writes the output of those commands to a local file.
For every command I run on the remote server I open a new SSH session.
Is there a way that only one SSH session remains open to a server, and that I can run multiple commands which the output I can redirect to a local file?
linux ssh bash scripting
linux ssh bash scripting
asked Oct 27 '09 at 10:51
Tom_13Tom_13
3615
3615
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
You want to look into the ControlMaster setting for SSH. That allows you to create a single SSH connection and then have all the other SSH connections to the same server multiplex over that one connection. Very handy.
Would this not add a lot of complexity in your script to start the master and kill it again after.
– David Pashley
Oct 27 '09 at 11:10
TANSTAAFL. Virtual memory adds a lot of complexity to your OS kernel, but we do it anyway because the benefits outweigh the disadvantages.
– womble♦
Oct 27 '09 at 12:15
No there's not a significant increase in complexity because ControlMaster can be made automatic, and then ssh handles creating and removing the master (unix-domain) socket.
– Jim Zajkowski
Oct 29 '09 at 21:51
add a comment |
Note: When you say local file, I am not sure if you mean local the machine you just ssh'd into, or if you mean local to the machine the starts the ssh session.
Push vs Pull:
It sounds like this might not be the right tool, maybe you just want to use cron on each local machine? If information is needed from the master, you can have the cron job fetch a file from the server. This would be more of a pull method, the ssh is what I would call push method. I can't say which is better for your situation, but this may be something to think about.
Run Multiple Commands:
I don't understand if you mean keep the session open for each itteration of the multiple commands, or just have it sort of always open. If it is the former, you can just use semi-colons between commands, or && to make so the next command will only be executed if the first works. For example:
#bar echoed only if foo exists and can be read
ssh myServer 'cat foo && echo bar'
ssh myServer 'cat foo; echo bar'
You can also do stuff like the following if you want to specify use of things in a shell:
ssh myServer "bash -c 'cat foo && echo bar'"
Lastly, you could always just put the script on each server, and just run that.
add a comment |
The "expect" tool could also do what you want, but involves writing tcl.
add a comment |
To expand on the ControlMaster use, here's a slightly more concrete example:
CONTROL=/tmp/ssh-control-`date +%s`-$RANDOM
ssh -o BatchMode=yes -NfM -S $CONTROL &
MASTERPID=$?
ssh -o BatchMode=yes -S $CONTROL user@host command1 > outfile1
ssh -o BatchMode=yes -S $CONTROL user@host command2 > outfile2
kill $MASTERPID
This is largely untested, but it should give you the right general idea.
add a comment |
You can run several commmands in sequence by starting a shell on the remote system and passing it the commands:
ssh server "echo this is; all in one session"
will automatically pass the quoted string through a remote shell. Beware of shell quoting rules, though!
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "2"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f78630%2fretaining-ssh-session-in-bash-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You want to look into the ControlMaster setting for SSH. That allows you to create a single SSH connection and then have all the other SSH connections to the same server multiplex over that one connection. Very handy.
Would this not add a lot of complexity in your script to start the master and kill it again after.
– David Pashley
Oct 27 '09 at 11:10
TANSTAAFL. Virtual memory adds a lot of complexity to your OS kernel, but we do it anyway because the benefits outweigh the disadvantages.
– womble♦
Oct 27 '09 at 12:15
No there's not a significant increase in complexity because ControlMaster can be made automatic, and then ssh handles creating and removing the master (unix-domain) socket.
– Jim Zajkowski
Oct 29 '09 at 21:51
add a comment |
You want to look into the ControlMaster setting for SSH. That allows you to create a single SSH connection and then have all the other SSH connections to the same server multiplex over that one connection. Very handy.
Would this not add a lot of complexity in your script to start the master and kill it again after.
– David Pashley
Oct 27 '09 at 11:10
TANSTAAFL. Virtual memory adds a lot of complexity to your OS kernel, but we do it anyway because the benefits outweigh the disadvantages.
– womble♦
Oct 27 '09 at 12:15
No there's not a significant increase in complexity because ControlMaster can be made automatic, and then ssh handles creating and removing the master (unix-domain) socket.
– Jim Zajkowski
Oct 29 '09 at 21:51
add a comment |
You want to look into the ControlMaster setting for SSH. That allows you to create a single SSH connection and then have all the other SSH connections to the same server multiplex over that one connection. Very handy.
You want to look into the ControlMaster setting for SSH. That allows you to create a single SSH connection and then have all the other SSH connections to the same server multiplex over that one connection. Very handy.
answered Oct 27 '09 at 10:59
womble♦womble
85.7k18144204
85.7k18144204
Would this not add a lot of complexity in your script to start the master and kill it again after.
– David Pashley
Oct 27 '09 at 11:10
TANSTAAFL. Virtual memory adds a lot of complexity to your OS kernel, but we do it anyway because the benefits outweigh the disadvantages.
– womble♦
Oct 27 '09 at 12:15
No there's not a significant increase in complexity because ControlMaster can be made automatic, and then ssh handles creating and removing the master (unix-domain) socket.
– Jim Zajkowski
Oct 29 '09 at 21:51
add a comment |
Would this not add a lot of complexity in your script to start the master and kill it again after.
– David Pashley
Oct 27 '09 at 11:10
TANSTAAFL. Virtual memory adds a lot of complexity to your OS kernel, but we do it anyway because the benefits outweigh the disadvantages.
– womble♦
Oct 27 '09 at 12:15
No there's not a significant increase in complexity because ControlMaster can be made automatic, and then ssh handles creating and removing the master (unix-domain) socket.
– Jim Zajkowski
Oct 29 '09 at 21:51
Would this not add a lot of complexity in your script to start the master and kill it again after.
– David Pashley
Oct 27 '09 at 11:10
Would this not add a lot of complexity in your script to start the master and kill it again after.
– David Pashley
Oct 27 '09 at 11:10
TANSTAAFL. Virtual memory adds a lot of complexity to your OS kernel, but we do it anyway because the benefits outweigh the disadvantages.
– womble♦
Oct 27 '09 at 12:15
TANSTAAFL. Virtual memory adds a lot of complexity to your OS kernel, but we do it anyway because the benefits outweigh the disadvantages.
– womble♦
Oct 27 '09 at 12:15
No there's not a significant increase in complexity because ControlMaster can be made automatic, and then ssh handles creating and removing the master (unix-domain) socket.
– Jim Zajkowski
Oct 29 '09 at 21:51
No there's not a significant increase in complexity because ControlMaster can be made automatic, and then ssh handles creating and removing the master (unix-domain) socket.
– Jim Zajkowski
Oct 29 '09 at 21:51
add a comment |
Note: When you say local file, I am not sure if you mean local the machine you just ssh'd into, or if you mean local to the machine the starts the ssh session.
Push vs Pull:
It sounds like this might not be the right tool, maybe you just want to use cron on each local machine? If information is needed from the master, you can have the cron job fetch a file from the server. This would be more of a pull method, the ssh is what I would call push method. I can't say which is better for your situation, but this may be something to think about.
Run Multiple Commands:
I don't understand if you mean keep the session open for each itteration of the multiple commands, or just have it sort of always open. If it is the former, you can just use semi-colons between commands, or && to make so the next command will only be executed if the first works. For example:
#bar echoed only if foo exists and can be read
ssh myServer 'cat foo && echo bar'
ssh myServer 'cat foo; echo bar'
You can also do stuff like the following if you want to specify use of things in a shell:
ssh myServer "bash -c 'cat foo && echo bar'"
Lastly, you could always just put the script on each server, and just run that.
add a comment |
Note: When you say local file, I am not sure if you mean local the machine you just ssh'd into, or if you mean local to the machine the starts the ssh session.
Push vs Pull:
It sounds like this might not be the right tool, maybe you just want to use cron on each local machine? If information is needed from the master, you can have the cron job fetch a file from the server. This would be more of a pull method, the ssh is what I would call push method. I can't say which is better for your situation, but this may be something to think about.
Run Multiple Commands:
I don't understand if you mean keep the session open for each itteration of the multiple commands, or just have it sort of always open. If it is the former, you can just use semi-colons between commands, or && to make so the next command will only be executed if the first works. For example:
#bar echoed only if foo exists and can be read
ssh myServer 'cat foo && echo bar'
ssh myServer 'cat foo; echo bar'
You can also do stuff like the following if you want to specify use of things in a shell:
ssh myServer "bash -c 'cat foo && echo bar'"
Lastly, you could always just put the script on each server, and just run that.
add a comment |
Note: When you say local file, I am not sure if you mean local the machine you just ssh'd into, or if you mean local to the machine the starts the ssh session.
Push vs Pull:
It sounds like this might not be the right tool, maybe you just want to use cron on each local machine? If information is needed from the master, you can have the cron job fetch a file from the server. This would be more of a pull method, the ssh is what I would call push method. I can't say which is better for your situation, but this may be something to think about.
Run Multiple Commands:
I don't understand if you mean keep the session open for each itteration of the multiple commands, or just have it sort of always open. If it is the former, you can just use semi-colons between commands, or && to make so the next command will only be executed if the first works. For example:
#bar echoed only if foo exists and can be read
ssh myServer 'cat foo && echo bar'
ssh myServer 'cat foo; echo bar'
You can also do stuff like the following if you want to specify use of things in a shell:
ssh myServer "bash -c 'cat foo && echo bar'"
Lastly, you could always just put the script on each server, and just run that.
Note: When you say local file, I am not sure if you mean local the machine you just ssh'd into, or if you mean local to the machine the starts the ssh session.
Push vs Pull:
It sounds like this might not be the right tool, maybe you just want to use cron on each local machine? If information is needed from the master, you can have the cron job fetch a file from the server. This would be more of a pull method, the ssh is what I would call push method. I can't say which is better for your situation, but this may be something to think about.
Run Multiple Commands:
I don't understand if you mean keep the session open for each itteration of the multiple commands, or just have it sort of always open. If it is the former, you can just use semi-colons between commands, or && to make so the next command will only be executed if the first works. For example:
#bar echoed only if foo exists and can be read
ssh myServer 'cat foo && echo bar'
ssh myServer 'cat foo; echo bar'
You can also do stuff like the following if you want to specify use of things in a shell:
ssh myServer "bash -c 'cat foo && echo bar'"
Lastly, you could always just put the script on each server, and just run that.
edited Oct 27 '09 at 11:48
answered Oct 27 '09 at 11:38
Kyle Brandt♦Kyle Brandt
66.4k61263414
66.4k61263414
add a comment |
add a comment |
The "expect" tool could also do what you want, but involves writing tcl.
add a comment |
The "expect" tool could also do what you want, but involves writing tcl.
add a comment |
The "expect" tool could also do what you want, but involves writing tcl.
The "expect" tool could also do what you want, but involves writing tcl.
answered Oct 27 '09 at 12:16
pjc50pjc50
1,7001012
1,7001012
add a comment |
add a comment |
To expand on the ControlMaster use, here's a slightly more concrete example:
CONTROL=/tmp/ssh-control-`date +%s`-$RANDOM
ssh -o BatchMode=yes -NfM -S $CONTROL &
MASTERPID=$?
ssh -o BatchMode=yes -S $CONTROL user@host command1 > outfile1
ssh -o BatchMode=yes -S $CONTROL user@host command2 > outfile2
kill $MASTERPID
This is largely untested, but it should give you the right general idea.
add a comment |
To expand on the ControlMaster use, here's a slightly more concrete example:
CONTROL=/tmp/ssh-control-`date +%s`-$RANDOM
ssh -o BatchMode=yes -NfM -S $CONTROL &
MASTERPID=$?
ssh -o BatchMode=yes -S $CONTROL user@host command1 > outfile1
ssh -o BatchMode=yes -S $CONTROL user@host command2 > outfile2
kill $MASTERPID
This is largely untested, but it should give you the right general idea.
add a comment |
To expand on the ControlMaster use, here's a slightly more concrete example:
CONTROL=/tmp/ssh-control-`date +%s`-$RANDOM
ssh -o BatchMode=yes -NfM -S $CONTROL &
MASTERPID=$?
ssh -o BatchMode=yes -S $CONTROL user@host command1 > outfile1
ssh -o BatchMode=yes -S $CONTROL user@host command2 > outfile2
kill $MASTERPID
This is largely untested, but it should give you the right general idea.
To expand on the ControlMaster use, here's a slightly more concrete example:
CONTROL=/tmp/ssh-control-`date +%s`-$RANDOM
ssh -o BatchMode=yes -NfM -S $CONTROL &
MASTERPID=$?
ssh -o BatchMode=yes -S $CONTROL user@host command1 > outfile1
ssh -o BatchMode=yes -S $CONTROL user@host command2 > outfile2
kill $MASTERPID
This is largely untested, but it should give you the right general idea.
edited 13 hours ago
V1pr
52
52
answered Oct 27 '09 at 13:18
Walter MundtWalter Mundt
35414
35414
add a comment |
add a comment |
You can run several commmands in sequence by starting a shell on the remote system and passing it the commands:
ssh server "echo this is; all in one session"
will automatically pass the quoted string through a remote shell. Beware of shell quoting rules, though!
add a comment |
You can run several commmands in sequence by starting a shell on the remote system and passing it the commands:
ssh server "echo this is; all in one session"
will automatically pass the quoted string through a remote shell. Beware of shell quoting rules, though!
add a comment |
You can run several commmands in sequence by starting a shell on the remote system and passing it the commands:
ssh server "echo this is; all in one session"
will automatically pass the quoted string through a remote shell. Beware of shell quoting rules, though!
You can run several commmands in sequence by starting a shell on the remote system and passing it the commands:
ssh server "echo this is; all in one session"
will automatically pass the quoted string through a remote shell. Beware of shell quoting rules, though!
answered Oct 27 '09 at 11:39
sleskesleske
8,45732440
8,45732440
add a comment |
add a comment |
Thanks for contributing an answer to Server Fault!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f78630%2fretaining-ssh-session-in-bash-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown