Thursday, February 2, 2017

How we will block USB on linux machine.

Hello Friends,

We can block the usb drive on linux system very easily. There are several ways using this we can block or open the USB drive on machine.

Here, we will block USB using script. This script is used in any version on Linux ( Redhat, Centos).
You can create a any .sh file on your linux machine and paste this below script content.

##############################################################################

#!/bin/bash
clear ;
yum -y install sshpass &> /tmp/temp ;
apt-get install sshpass -y &> /tmp/tmp ;
echo  " Welcome $USER , *** WARNING: THIS PROGRAM WILL RESTART THE REMOTE MACHINE *** "
echo ""
echo  " 1. Block USB ports of Machine "
echo  " 2. Open USB ports of Machine "
echo ""
echo -ne " Please Select your choice : "
read choice ;
echo -ne " Please Enter the IP address of Machine : "
read ip ;
echo -ne " please Enter the password for $ip : "
read -s pass ;
case "$choice" in

1)      echo -e "\n";
        echo -ne " You are going to Block the USB ports of $ip ,Are you sure you want to continue connecting (Yes/no)? "
    read choice1 ;
        if [ "$choice1" == "no" ];
        then
                exit ;
        else
                sshpass -p $pass ssh $ip  "echo 'blacklist usb_storage' >>  /etc/modprobe.d/blacklist.conf ; echo 'modprobe -r usb_storage' >> /etc/rc.local ; "
        fi

        echo -e "\n"
        echo -ne " $ip needs a reboot, So we are going to reboot $ip machine. Press 0 to EXIT or Press Enter to reboot the Machine."

        read choice2 ;
        if [ "$choice2" == "0" ]
        then
                exit ;
        else
                 sshpass -p $pass ssh $ip reboot ;
        fi

    ;;
2)      echo -e "\n" ;
        echo -ne " You are going to Open the USB ports of $ip ,Are you sure you want to continue connecting (Yes/no)? "
        read choice3 ;
        if [ "$choice3" == "no" ]
        then
                exit ;
        else
                 sshpass -p $pass ssh $ip "sed -i 's/"blacklist usb_storage"/""/g' /etc/modprobe.d/blacklist.conf ; sed -i 's/"modprobe -r usb_storage"/""/g' /etc/rc.local ;"
        fi
    ;;
*) echo "you have not selected the right option, Try Again. Bye"
   ;;
esac

##############################################################################

When you run the script, it is asking for 2 options, first option is used for blocking the USB drive and second option is for open the block usb drive.

Please read the script carefully and please let me know if you have any doubt in your mind. I will try to resolve your query as earliest.

No comments:

Post a Comment