{"id":151,"date":"2021-09-30T08:34:55","date_gmt":"2021-09-30T08:34:55","guid":{"rendered":"http:\/\/ouyangminwei.com\/?p=151"},"modified":"2021-10-04T15:03:48","modified_gmt":"2021-10-04T15:03:48","slug":"ssh-to-a-linux-by-ngrok-and-manipulate-existing-terminal","status":"publish","type":"post","link":"https:\/\/ouyangminwei.com\/index.php\/2021\/09\/30\/ssh-to-a-linux-by-ngrok-and-manipulate-existing-terminal\/","title":{"rendered":"SSH to a Linux by ngrok and manipulate existing terminal"},"content":{"rendered":"\n<p>In <a href=\"http:\/\/ouyangminwei.com\/index.php\/2021\/09\/18\/build-a-discrod-music-bot\/\">http:\/\/ouyangminwei.com\/index.php\/2021\/09\/18\/build-a-discrod-music-bot\/<\/a>, I created a discord bot and develop on a Raspberry pi, but now there are two problems<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The network used by  is a floating IP.<\/li><li>Even if I can SSH in, I still can&#8217;t see or close the discord bot since It is on a terminal.<\/li><\/ol>\n\n\n\n<h1 class=\"has-text-color wp-block-heading\" style=\"color:#1d98cc\">Ngrok<\/h1>\n\n\n\n<p>After searching a little bit, I found that ngrok can solve the first problem perfectly.<\/p>\n\n\n\n<p>Ngrok is&nbsp;a cross-platform application that exposes local server ports to the Internet, so I can expose my raspberry pi to the internet.<\/p>\n\n\n\n<p>First, sign up and  download ngrok on this website <a href=\"https:\/\/ngrok.com\/\">https:\/\/ngrok.com\/<\/a>.<\/p>\n\n\n\n<p>Because it runs on the Raspberry Pi, so download the 32-Bit ARM&#8217;s version<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/09\/image-39.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-39.png\"\/><\/figure>\n\n\n\n<p>After you download the ngrok<strong> upzip<\/strong> it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>unzip ngrok-stable-linux-arm.zip.zip<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/09\/image-38.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-38.png\"\/><\/figure>\n\n\n\n<p>Login the ngrok, yo can get your Authtoken<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/ngrok authtoken<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/09\/image-37-1024x556.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-37-1024x556.png\"\/><\/figure>\n\n\n\n<p>And you are ready to fire it up.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/ngrok tcp 22<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.trendmicro.com\/content\/dam\/trendmicro\/global\/en\/research\/20\/i\/analysis-of-a-convoluted-attack-chain-involving-ngrok\/analysis-of-a-convoluted-attack-chain-ngorok-3.jpg\" alt=\"\u89e3\u6790Ngrok \u66f2\u6298\u7684\u653b\u64ca\u7a0b\u5e8f\u2013 \u8cc7\u5b89\u8da8\u52e2\u90e8\u843d\u683c\"\/><\/figure>\n\n\n\n<p>remember the Ip it gives  you<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tcp:\/\/2.tcp.ngrok.io:14139<\/code><\/pre>\n\n\n\n<p>Then you can SSH to it<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh username@2.tcp.ngrok.io -p14139<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/09\/image-41.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-41.png\"\/><\/figure>\n\n\n\n<h1 class=\"has-text-color wp-block-heading\" style=\"color:#1d98cc\">Screen<\/h1>\n\n\n\n<p>In order to access to running terminal, we can use &#8220;screen&#8221; to achieve it<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install screen<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/09\/image-40.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-40.png\" width=\"670\" height=\"391\"\/><\/figure>\n\n\n\n<p>This can create a new screen call &#8220;bot&#8221;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>screen -mS bot<\/code><\/pre>\n\n\n\n<p>And you can start your server, for here, I will test will python httpserver<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python -m SimpleHTTPServer<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/09\/image-42.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-42.png\"\/><\/figure>\n\n\n\n<p>Now, the server is running, CTRL+A and then CTRL+D to detach the screen.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>screen -ls<\/code><\/pre>\n\n\n\n<p>Show the screen <\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/09\/image-43.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-43.png\" width=\"613\" height=\"70\"\/><\/figure>\n\n\n\n<p>I have a &#8220;test.root&#8221; over here, Try to see if I can download this file via this python http server<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/09\/image-44.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-44.png\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>wget 127.0.0.1:8000\/test.root<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/09\/image-45-1024x183.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-45-1024x183.png\"\/><\/figure>\n\n\n\n<p>Every works well,  the server it still running.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>screen -x bot<\/code><\/pre>\n\n\n\n<p>Back to the terminal<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/09\/image-46.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-46.png\"\/><\/figure>\n\n\n\n<p>So now, I can access the Raspberry pi with ngrok and use screen to control the terimal.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/09\/image-47.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-47.png\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/ouyangminwei.com\/wp-content\/uploads\/2021\/09\/image-48-1024x157.png\" alt=\"\u9019\u5f35\u5716\u7247\u7684 alt \u5c6c\u6027\u503c\u70ba\u7a7a\uff0c\u5b83\u7684\u6a94\u6848\u540d\u7a31\u70ba image-48-1024x157.png\"\/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In http:\/\/ouyangminwei.com\/index.php\/202 &hellip; <a href=\"https:\/\/ouyangminwei.com\/index.php\/2021\/09\/30\/ssh-to-a-linux-by-ngrok-and-manipulate-existing-terminal\/\">\u95b1\u8b80\u5168\u6587 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[7,6],"post_format":[],"class_list":["post-151","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-en","tag-linux"],"_edit_lock":"1633359828:1","_edit_last":"1","_aioseo_title":"#post_title #separator_sa #site_title","_aioseo_description":"#post_excerpt","_aioseo_keywords":"","_aioseo_og_title":"","_aioseo_og_description":"","_aioseo_og_article_section":"","_aioseo_og_article_tags":"","_aioseo_twitter_title":"","_aioseo_twitter_description":"","_oembed_2544c1d0cb3503ab4c4d558c3b3c8873":"","_oembed_time_2544c1d0cb3503ab4c4d558c3b3c8873":"","_oembed_99481806ecbe6ce4ee46f8588d320993":"","_oembed_db663acf973e82e6d9d80df71945dfb8":"","_oembed_16cdfab488f57db73586f4286af2704f":"","_wp_old_slug":"","_links":{"self":[{"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/posts\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/comments?post=151"}],"version-history":[{"count":3,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":167,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/posts\/151\/revisions\/167"}],"wp:attachment":[{"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/media?parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/categories?post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/tags?post=151"},{"taxonomy":"post_format","embeddable":true,"href":"https:\/\/ouyangminwei.com\/index.php\/wp-json\/wp\/v2\/post_format?post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}