Tuncay Sahin

ICT Engineer | Docent ICT & Trainer

If you have too much Linux systems to manage them manually, this document will provide you the centralized solution to manage them remotely and in bulk.

I’ve used a Windows command line tool, named plink, to connect to remote Linux systems by SSH and run some commands on that system. Around it I’ve write a batch-script to automatically repeat this step for each defined Linux system in a separate file.

The script gets the remote Linux hostnames from a text file and pass this into the windows command line tool to run some commands on the remote system. These commands are also defined in a separate file. You only have to find out which commands to be run without any user input is required.

Explanation with an example to modify DNS settings remotely

This remote management solution consists the following files to do centralized management:

File Description
  • Run.bat
Main script to start the execution of the remote commands.
  • Remote_systems.txt
List of all the remote systems.
  • sub_run.bat
Sub script to call plink.exe.
  • Remote_commands.txt
Linux commands to run on remote system.
  • plink.exe
Windows command line tool to connect to remote system.
  • logging.txt
Output logging.

Run.bat

for /F “tokens=1” %%a in (remote_systems.txt) do call sub_run.bat %1 %2 %%a

This is the main file to be run to start the execution of the remote commands.

Open a new Command Prompt and switch to the folder containing the script files and use the command below (replace username and password with yours. This account is used for all the remote systems.):

Run.bat <username> <password>

For every remote system defined in remote_systems.txt file the sub_run.bat file is called.

The provided username (%1), password (%2) and the system name  (%aa) are be passed to the sub_run.bat file.

remote_systems.txt

hostname1
hostname2
192.168.1.25
192.168.1.73

In this file you have to list all your systems you want to manage remotely. It may be the hostname self or IP Address.

sub_run.bat

echo yes | plink.exe -ssh -pw %2 %1@%3 -m remote_commands.txt >> logging.txt

This file catches the argument for username, password and the system name and pass them to the command line tool plink.exe to use for the remote connection. The commands to be run remotely are read from the remote_commands.txt file.

“echo yes | “ is used to answer the “Store key in cache?” with yes. Otherwise you have to answer this question for every remote system.

remote_commands.txt

In the example below I will show you how to modify the DNS settings for remote Linux systems. Just replace the content of this file to do other task on the remote systems. It’s important to provide a well-targeted command so no user input is required. Just find out on a reference system the right commands to achieve your goals.

#VERIFY SYSTEMS’ IP ADDRESS TO COULD DEFINE THE NEW DNS SETTING

IP=`ifconfig eth0 2>/dev/null|awk ‘/inet addr:/ {print $2}’|sed ‘s/addr://’`

#NEW DNS SETTINGS

#AMSTERDAM SYSTEMS

if [[ $IP == *172.168.* ]]

then

DOMAIN=amsterdam.com

DNS1=172.168.1.1

DNS2=172.128.1.1

fi

#LONDON SYSTEMS

if [[ $IP == *172.128.* ]]

then

DOMAIN=london.com

DNS1=172.128.1.1

DNS2=172.168.1.1

fi

# LOG CURRENT DNS SETTINGS

echo DNS settings BEFORE

cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep ‘DOMAIN\|DNS’

# CLEAR PREVIOUS DNS SETTINGS

sed -i ‘/^DNS.*/d;/^DOMAIN.*/d’ /etc/sysconfig/network-scripts/ifcfg-eth0

rm -f /etc/resolv.conf

# SET NEW DNS SETTINGS

echo -e $”DOMAIN=$DOMAIN\nDNS1=$DNS1\nDNS2=$DNS2″ >> /etc/sysconfig/network-scripts/ifcfg-eth0

# LOG NEW DNS SETTINGS

echo DNS settings AFTER

cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep ‘DOMAIN\|DNS’

plink.exe

This the Windows command-line tool to connect to the remote systems and execute the commands on it. You can download it from http://www.putty.org.

Logging.txt

Output will be logged into the logging.txt file. This file will be created automatically.

 

Meer informatie

Voor meer informatie of voor een persoonlijk adviesgesprek kunt u altijd vrijblijvend contact met mij opnemen.

E-Mail

info@tuncaysahin.nl