<body id=bd><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/platform.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d7145547\x26blogName\x3dESZhang+blog\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dSILVER\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttps://zenac.blogspot.com/search\x26blogLocale\x3den_US\x26v\x3d2\x26homepageUrl\x3dhttp://zenac.blogspot.com/\x26vt\x3d3767548654419712195', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>

 ESZhang blog

บันทึก ความทรงจำ บ่น ระบาย เหตุบ้านการเมือง ไปๆมาๆ กล้องกับการถ่ายรูป กับงานสุดรัก คอมพิวเตอร์งัย

AutoSSH

ได้รับคำขอร้อง(จริงๆสั่งให้ทำ) ว่าให้หาช่องทางในการ Remote เข้าไปทำงานบน Network ของลูกค้า
โจทย์ไม่น่าจะยากเนอะ เหอะๆปัญหามีอยู่ว่า Network วงเนี้ยไม่มี Internet
สายเช่าก็แพงใช้ได้ จะต่อ ADSL เข้าไปก็มีปัญหาเรื่องสิทธิการใช้(พอติดต่อในนามบริษัทค่า ADSL พุ่งพรวด)
เฮือก... ทำไงดี ต่อผ่าน Mobile สิครับก็ไปได้ GPRS Router modem มาตัว 1
ถูกว่าค่าติดตั้ง สายเช่ากะ ADSL อีก ค่าเช่าสัญญาณก็ถูกว่า ติดตั้งก็ง่าย
แต่ว่ามัน SSH เข้าผ่าน GPRS ไม่ได้อีก เฮือก...
ไม่เป็นไร SSH มันเปิด Remote Forward port ได้ ก็นี่เลย
ssh -R 50022:127.0.0.1:22 Destination-IP-at-HQ
อ่ะเรียบร้อย... แต่เดี๋ยวก่อนถ้ามันหลุดล่ะ จะทำไง (โจทย์เยอะจังวุ้ย)

จำไว้ครับเราไม่ใช่คนแรก... Google ช่วยท่านได้
ผมไปนั่งค้นๆดูสักพักก็เจอ rstunnel ( Reliable SSH Tunnel ) อืมๆท่าทางจะใช้ได้
แต่พอลองหาเพิ่มไปอีกหน่อย อะนี่เลย Inspire by rstunnel AutoSSH พร้อมใช้ติดตั้งง่าย Function ครบครัน
มาลองดูกัน

AUTOSSH_POLL=600 #ให้ตรวจ tunnel ทุกๆ 5 นาที
AUTOSSH_PIDFILE=/var/run/autossh #File ที่เอาไว้เก็บ PID ของเจ้า AutoSSH มัน
export AUTOSSH_PIDFILE AUTOSSH_POLL #export ซะหน่อย
autossh -M 50000:50001 -f ssh -i private_key -R 50022:127.0.0.1:22 -l remote_user -N Destination-IP-at-HQ
# -M บอกว่า port ที่เอาไว้ให้ autossh ตรวจ tunnel
# -f สั่งให้ทำงานเป็น Daemon
# ssh -i private_key เป็น private key file ที่ไม่มี passphase สำหรับ remote_user
# -R 50022:127.0.0.1:22 ไปเปิด Remote port forward ที่ Head Quoter Office
# -l remote_user บอกให้ ssh ใช้ user remote_user (ผู้ใช้คนนี้มี shell=/bin/false)
# -N บอก ssh ให้ไม่ต้องรัน Shell เปิด tunnel อย่างเดียวพอ


ถ้าต้องการ restart ssh tunnel สั่ง
kill -s SIGUSR1 `cat /var/run/autossh`
ถ้าจะเลิกการทำงานของ autossh สั่ง
kill -s SIGTERM `cat /var/run/autossh`
ทีนี้ถ้า tunnel หลุดแต่ ssh session ยังอยู่ก็ไปสั่งคำสั่งนี้ที่ฝั่ง HQ
pkill -u remote_user sshd
กำลังลองใช้งานอยู่ครับ ทำงานได้ดีทีเดียว

Labels: , , , , ,

Thursday, May 01, 2008 at 12:10

My Profile

 My name: Zena
My Home: Klongsamwa, Bangkok, Thailand

View my complete profile


Copyright © 2005 for Blogger by Blogger Templates Inc. All rights reserved.