Thursday 31 August 2017

Remotely shutdown Linux & windows machine using shell script

Please find the below script for shutdown the Unix & Windows server remotely.

#!/bin/bash

 SSH=/usr/bin/ssh
 POWERCMD="/sbin/shutdown -h now"
 HOSTS="server1 server2 server3"

 # Shutdown remote machines
 for i in $HOSTS;
 do
   $SSH -l root $i $POWERCMD;
 done

  telnet=/usr/bin/telnet
  POWER="C:\Windows\System32\shutdown.exe \s"
  IP="server1 server2 server3"

  # Shutdown window machine
  for i in $IP;
  do
        $telnet -l administrator $i $POWER;
  done

 # Shutdown ourself
 /sbin/shutdown -h now

ifconfig command not found in centos 7 minimal installation

By default in Centos 7 minimal installation , ifconfig command is not found, so for obtaining this command , first you need to check which packages yum provide for this.

[root@localhost]# yum provides ifconfig

Loaded plugins: fastestmirror
Repository 'Packages' is missing name in configuration, using id
Loading mirror speeds from cached hostfile
Packages/filelists_db                                                                                                                              
No matches found

Note: here no packages is installed on the system.

So, using yum we will install the net-tool rpm which is required for ifconfig command

[root@localhost]# yum install net-tools*

Loaded plugins: fastestmirror
Repository 'Packages' is missing name in configuration, using id
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.17.20131004git.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

 Package                  Arch           Version                                   Repository                    Size

Installing:
 net-tools                x86_64         2.0-0.17.20131004git.el7         Packages                        304 k

Transaction Summary
========================================================================
Install  1 Package

Total download size: 304 k
Installed size: 917 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : net-tools-2.0-0.17.20131004git.el7.x86_64                                                                     1/1
Verifying  : net-tools-2.0-0.17.20131004git.el7.x86_64                                                                    1/1

Installed:
net-tools.x86_64 0:2.0-0.17.20131004git.el7

Complete!

Now run the ifconfig command after successfully installed the packages.

[root@localhost]# ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.26.12.1  netmask 255.255.0.0  broadcast 192.26.255.255
        inet6 fe80::250:56ff:febf:f6  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:bf:00:f6  txqueuelen 1000  (Ethernet)
        RX packets 18939  bytes 1251073 (1.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 433  bytes 63109 (61.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost]# yum provides ifconfig

Loaded plugins: fastestmirror
Repository 'Packages' is missing name in configuration, using id
Loading mirror speeds from cached hostfile
net-tools-2.0-0.17.20131004git.el7.x86_64 : Basic networking tools
Repo: @Packages
Matched from:
Filename: /usr/sbin/ifconfig