Showing posts with label System Administration. Show all posts
Showing posts with label System Administration. Show all posts

Saturday, December 17, 2011

Twitter for System Administration



How about shutting down/restarting your system via SMS? 
Is that something that is possible? Yes , With twitter SMS service that is of course possible. This can come handy for system administrators. Here I will show you how to control your system  on the move via SMS, using the services provided by the twitter.
          Initially we will create two twitter accounts , call them Master and Slave. Master account will be a normal account, on the other hand Slave will be a private account, which has connection only with Master(i.e Slave will be only following the Master and the only follower that Slave has, will be Master). This helps preventing users other than Master sending messages to the Slave.
           The next step will be a program that monitors the message inbox of the Slave for the messages from the Master. This program have to be run on the system that has to be controlled. The program should be able to log in to the account of Slave and to check out its inbox. That's where twitter API comes to rescue. For various reasons twitter prevents earlier Basic authentication. So we have to rely on OAuth . OAuth is a trickier authentication method , whose explanation is beyond the scope of this article. Using twitter api resources we will be checking out the message inbox of Slave. Remember Master is the only user in twitter who can send messages to the Slave . Also message to a twitter user can be sent via SMS .Messages are monitored by the program and depending on the message, necessary actions are taken. Below is simple program that will Shut down/restart the system depending on the message. 


Though the sample program can only be used for Shutting down/restarting the system ,System Administrators can add the functionality like auto replying to the queries issued by the Master, thus achieving a greater control over the system via SMS.
Note : Python tweepy module is a module that helps in communication with twitter. It can be installed in linux system by the command easy_install tweepy. Also the program is installed in crontab ,so that it runs every minute.

Tuesday, June 28, 2011

Automation in Linux 1.0

Automation is an important ingredient in any robust software. Linux OS provides you with the tools that  can be utilized to automate execution of scripts. Here we will discuss mainly two methods for the auto execution of scripts namely,init.d method and .bashrc method.


init.d method
               Consider that you are required to run a script automatically at the start-up itself. Then you can go for init.d method. init.d is actually a directory  in /etc folder. I will illustrate how to auto run scripts at the start-up through following steps
step1  Put the script you intend to run in to the folder /etc/init.d. You need to be root for doing this. 
Step2 Make the script executable by giving chmod +x Script_Name
Step3 Then issue command update-rc.d Script_Name defaults 99


Step3 requires further explanation. update-rc.d helps in the updation of script links. I will quote the man page here
update-rc.d updates the System V style init script links /etc/rcrunlevel.d/NNScript_Name whose target  is  the  script /etc/init.d/Script_name.   These links are run by init when it changes runlevels; they are generally used to start and stop system services such as daemons.  runlevel is one of the runlevels supported by init, namely, 0123456789S,and NN is the two-digit sequence number that determines where in the sequence init will run the scripts.
When we give defaults with update-rc.d the script will have links at every runlevel. You can see the links in the folders /etc/rc#.d , where # stands for the numbers 0 to 6 which denotes the runlevels. Care should be taken to avoid interactive programs being automated through this method


Tips :- You can make use of a single start-up script that is made to run at start-up by above methods to run
any number of other scripts. For example , in my system I've establised a startup script named My_startup. So when I need new scripts that run automatically on the start-up , all I've to do is to add the particular script to the My_starup script. So it becomes really easy to have new start-up scripts.The My_startup in my system will look something like below :-
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/user/.Myscripts
Startup_script#1 &
Startup_script#2 &
.
.
.Startup_script#n &
Note :  PATH and SHELL variables should be defined in the script. Also & at the end of each line helps in launching the next script without waiting for the previous one to finish        
.bashrc method
                       .bashrc is nothing but a shell script that is run each time you launch the terminal. You can find .bashrc file in your home folder (/home/user/) itself. You can add the scripts/commands that you want to run at the launch of terminal to .bashrc file.In my system I've used it to set variables.


The other tool that can be used for running scripts automatically is the crontab utility. It is so powerful so that we can schedule the script to run at a particular time like a day of the month, every minute,every hour,yearly,monthly.... We will have a detailed discussion on it later.

Tuesday, April 19, 2011

Changing system variables permanently

I wanted to change the the system variable $PATH in a such a way that bash executes scripts in current working directory. I can do this simply by PATH="$PATH:.". While working on this I came to know about following things....

You can change the value of system variables simply by doing sys_var_name="new_value". But such a kind of initialization is only temporary. Once you close the terminal the system variables you changed is reset to old values. So we have to initialize the system variables for our convenience every time we launch the terminal. Doing it manually is really boring. So is there any way to do it automatically?
The answer is a big ΥΕS. '.bashrc' is a shell script that's executed every time at the  launch of terminal. '.bashrc' is kept in the users home folder. You can edit .bashrc script in a such a way that it edits system variable automatically for your convenience every time you launch the terminal. Thus you can change the system variables permanently. You can even run shell scripts of your choice at the launch of terminal automatically by editing .bashrc :-)