To better understand the tutorial, read the background behind writing this article here
How to reserve an ip for your device?
I will be explaining on reserving an ip for a device via router’s settings. My router’s model is TL-WR740N, brand TP-Link. However the sole concept and procedure is similar for all the routers. First, go to your router’s settings,
192.168.0.1 (in my case)
Now provide valid credentials for user and password. For most of the routers, the default username and password is admin
Click on the DHCP option, present in the menu in left side in case of TP-Link
Now go ahead and click on Address Reservation
Click Add New so you can reserve an ip address for a device. You will be required to enter the Mac Address of the device.
Open up terminal with the combination of keys Ctrl + Alt + T in your linux machine and enter the following command.
Since I connect via WI-FI, I look for Hwaddr under wlan0.
Go ahead and paste this mac address to the address reservation menu.
After this, enter a desired ip address, in my case 192.168.0.106. Check enable and save the settings.
Once done, you will be required to reboot the router to save these settings. That’s all for a static local ip.
Setting up openssh-server and port Forwarding
It is dead simple to set up openssh server in linux machine. Follow along,
Enter the following command to get openssh-server
sudo apt-get install openssh-server
Now, to keep the openssh running, hit the following command
sudo service ssh start
The default port for ssh is 22
Another step is to forward the incoming traffic to my router’s public ip in port 22 to the local ip of the linux device. In my case to 192.168.0.106
For this, we again go to the router’s settings. Open up browser and go to 192.168.0.1 or something similar to open the router’s settings.
Find the menu named Forwarding or something similar and click the option. Now under the sub-menu click on virtual servers.
Once done click Add New
Fill the details :
Service Port : 22
Internal Port : 22
IP Address: 192.168.0.106 (the local ip of the machine with openssh running)
Protocol: All (TCP/UDP)
Go ahead and save it.
What have we done so far? Assigned a static ip to a device. Installed openssh-server in the linux machine and run it. Forwarded the incomming traffic to port 22 of the router to the local ip of the machine.
Another step. Based on your ISP, the public ip for your internet surfing may change but is static most of the time. In my case the ip barely changes. So all I have to do is rot the public ip which is what I did.
How to access a device remotely via ssh while on Windows machine
Prerequisite: The machine running openssh-server must be powered on. However it is not necessary to login. Also openssh-server remains in running state by default when device is powered on.
Download putty.exe from here. Putty is a telnet and ssh client for windows. The size of the software is very small and you need not install it. It’s a download and run software.
Once downloaded, run putty.exe. Enter the public ip of your home network, specify port 22 and check ssh then press open. This will open a terminal. If the connection was established meaning both the home network and the network you are at is connected to internet, you will be prompted for login credentials of your remote device. Providing the correct login credentials will give you access to your device at home network via command line.
Search for a file and send an email with attachment remotely via ssh and some python
Now that you have access to your linux machine remotely. You can use ls commands to find the location of the file you are looking for.
In my case, it’s present inside /home/bhishan/lispcognitive.odt
To send this file via email, we will be using python interactive shell.
Run the command to open python shell
We will be using two standard modules in python namely smtplib and email to send email with an attachment.
The snapshot shows the codes I used to send email
Below, I’ve nicely formatted the codes in a more readable way.
import smtplib from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from email.mine.text import MIMEText from email.utils import formatdate me = "email@example.com" you = "firstname.lastname@example.org" password = "password" msg = MIMEMultipart( From = me, To = you, Date = formatdate(localtime=True), Subject = "Sending email with attachment via python" ) msg.attach(MIMEApplication(file("/home/bhishan/listcognitive.odt").read())) s = smtplib.SMTP_SSL("smtp.gmail.com") s.login(me, password) s.sendmail(me, [you], msg.as_string()) s.quit()
If you have any questions regarding the post or the codes, comment below, so we can discuss.