Skip to content

Instantly share code, notes, and snippets.

@cyrillbrito
Last active March 12, 2019 20:39
Show Gist options
  • Save cyrillbrito/3327c7378248a51ee270156ac1d1cba7 to your computer and use it in GitHub Desktop.
Save cyrillbrito/3327c7378248a51ee270156ac1d1cba7 to your computer and use it in GitHub Desktop.
Automatically wake-on-lan a server, remote access it, run commands, exit the console and let the commands running and run sudo commands without asking the password from the console.

Minecraft Server Starter

I have a old laptop running linux that I'm using as a server for my modded minecraft world. And because I don't want the laptop to be on all the time I'm trying to make some scripts to automate the turn on and start of the server. This scripts will run from my main Windows computer.

How to use

To use this scripts you'll need python2.7, Wake On Lan and Plink on the windows side. And on the linux server you'll need screen. You also need to enable wake on lan on the server.

  1. Download the scripts from github.
  2. Update the config.ini with the correct data
  3. Run start.py to power on the computer start the server
  4. Run stop.py to stop the server and power it off

If the server is takes more than 30 seconds to power on you might need to increase the time it waits in the start.py. The wake on lan is only configured to work if the server is on the same network as you.

Wake On Lan

Wake on lan is a feature that almost all the computer have that allows the computer to turn on when a specify packet is recived, even if the computer is off. That packet is normally called Magic Packet.

I have search a bit and found a simple comand line program that allows me to send the magic packet.

https://www.depicus.com/wake-on-lan/wake-on-lan-cmd

There is a simple example on how to use the program on a internal network. I'll use that example since a only need to wake on lan from inside my own network.

wolcmd [macaddress] 255.255.255.255 255.255.255.255 4343

Plink

PuTTY Link is kind off a command line version on putty. This is how I'll send commands to the server, the problem with this method is that Plink only sends one command instead of having a season. This is a problem because the minecraft server needs to be in a season to them be able to close the server safely.

Plink usage example:

plink [user]@[server] -pw [password] [command]

Screen

To solve the season problem I'll be using screen. I search a little and found all the commands I needed to be able to have a season open without actually having it open.

  • Run a command and detach in only one command:

    screen -S [name] -d -m [command]
    
  • Run a command on a detached screen:

    screen -S [name] [command]
    
  • Send text to the season insted of a normal command:

    screen -S [name] -X stuff [command]
    

sudo

When I was trying to automate the shutdown of the server I came upon it a little problem. Shutdown needs to be run as super user and because Plink can only send one command at a time I at to find a way to sudo shutdown now without it asking for the password. So I came up it this:

echo [password] | sudo -S [command]

This makes sudo read the password from the standard input.

ConfigParser

ConfigParser is a python class that allows loading a simple configuration file. The configuration file consists of sections, led by a [section] header and followed by name: value entries. My configuration file locks like this:

[configs]
mac_address = 20:1D:12:38:9A:92
host = 192.168.1.100
username = notch
password = apple123
startServerCommand = ./server/ServerStart.sh

Example on how to read a value from the config file:

import ConfigParser

config = ConfigParser.ConfigParser()
config.read('config.ini')

username = config.get('configs', 'username')
# This are just examples
[configs]
mac_address = 20:1D:12:38:9A:92
host = 192.168.1.100
username = notch
password = apple123
startServerCommand = ./server/ServerStart.sh
#!/usr/bin/env python
import ConfigParser
import subprocess
import time
config = ConfigParser.ConfigParser()
config.read('config.ini')
# power up computer
command = 'WolCmd ' + config.get('configs', 'mac_address') + ' 255.255.255.255 255.255.255.255 4343'
print command
subprocess.call(command.split())
# wait for power up
for i in range(30, 0, -1):
print i
time.sleep(1)
# turn on minecraft server
linuxCommand = 'screen -S minecraft -d -m ' + config.get('configs', 'startServerCommand')
command = 'plink ' + config.get('configs', 'username') + '@' + config.get('configs', 'host') + ' -pw ' + config.get('configs', 'password') + ' ' + linuxCommand
print command
subprocess.call(command.split())
#!/usr/bin/env python
import ConfigParser
import subprocess
config = ConfigParser.ConfigParser()
config.read('config.ini')
# turn off minecraft server
linuxCommand = 'screen -S minecraft -X stuff "stop^M"'
command = 'plink ' + config.get('configs', 'username') + '@' + config.get('configs', 'host') + ' -pw ' + config.get('configs', 'password') + ' ' + linuxCommand
print command
subprocess.call(command.split())
# power off computer
linuxCommand = 'echo ' + config.get('configs', 'password') + ' | sudo -S shutdown 1'
command = 'plink ' + config.get('configs', 'username') + '@' + config.get('configs', 'host') + ' -pw ' + config.get('configs', 'password') + ' ' + linuxCommand
print command
subprocess.call(command.split())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment