- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- persistent ssh tunnel in a script
Operating System - Linux
1821638
Members
2973
Online
109633
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2009 07:26 AM
тАО05-02-2009 07:26 AM
Ok, I create a tunnel from "home" to "work" via "gate":
ssh -l myuserid -L 7777:work:22 gate
and leave this terminal session open and now I can make use of my tunnel for scp etc.
But suppose I want to create a persistent tunnel but I want to do it in a script or something? I saw one author who recommended this:
ssh -l myuserid -L 7777:work:22 gate cat -
where the author has used "cat" to keep the connection open (or other command which does not finish could be used). If no "non-finishing" command is provided, then a shell opens and then you need a controlling terminal and cannot use the ssh command in a script. Moreover, some servers have protections against running things like "cat -" and so on a well-secured server you won't easily be able to use some "non-finishing" command like that.
So how do you create a persistent tunnel in a script without having a controlling terminal? Is that possible?
Thanks, Gil
ssh -l myuserid -L 7777:work:22 gate
and leave this terminal session open and now I can make use of my tunnel for scp etc.
But suppose I want to create a persistent tunnel but I want to do it in a script or something? I saw one author who recommended this:
ssh -l myuserid -L 7777:work:22 gate cat -
where the author has used "cat" to keep the connection open (or other command which does not finish could be used). If no "non-finishing" command is provided, then a shell opens and then you need a controlling terminal and cannot use the ssh command in a script. Moreover, some servers have protections against running things like "cat -" and so on a well-secured server you won't easily be able to use some "non-finishing" command like that.
So how do you create a persistent tunnel in a script without having a controlling terminal? Is that possible?
Thanks, Gil
If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums
Solved! Go to Solution.
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-02-2009 12:39 PM
тАО05-02-2009 12:39 PM
Solution
If your SSH client is reasonably up to date, something like this might do the trick:
ssh -l myuserid -L 7777:work:22 gate -o ExitOnForwardFailure=yes -n -N &
# capture the PID of the SSH process:
TUNNELPID=$!
To remove the tunnel when you're done, use "kill $TUNNELPID".
Options:
-n removes the need for a controlling terminal.
-N says we are running SSH to get port forwardings only, remote command is not needed.
-o ExitOnForwardFailure=yes means that SSH will terminate if a tunnel cannot be set up.
You might use the "-f" option instead of "&", but then capturing the PID will be more difficult...
SSH has a lot of useful options: reading the man page helps.
MK
ssh -l myuserid -L 7777:work:22 gate -o ExitOnForwardFailure=yes -n -N &
# capture the PID of the SSH process:
TUNNELPID=$!
To remove the tunnel when you're done, use "kill $TUNNELPID".
Options:
-n removes the need for a controlling terminal.
-N says we are running SSH to get port forwardings only, remote command is not needed.
-o ExitOnForwardFailure=yes means that SSH will terminate if a tunnel cannot be set up.
You might use the "-f" option instead of "&", but then capturing the PID will be more difficult...
SSH has a lot of useful options: reading the man page helps.
MK
MK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-03-2009 05:47 AM
тАО05-03-2009 05:47 AM
Re: persistent ssh tunnel in a script
Thanks Matt
If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Learn About
News and Events
Support
© Copyright 2025 Hewlett Packard Enterprise Development LP