Showing posts with label Shell Scripts. Show all posts
Showing posts with label Shell Scripts. Show all posts

/bin/sh^M: bad interpreter: No such file or directory



The error  "/bin/sh^M: bad interpreter: No such file or directory" occurs when you simply do copy/paste from your windows machine to linux shell prompt. It appends ^M character at end of line. So the shell script execution gives error as mentioned below.

[root@localhost ~]# /usr/local/nagios/libexec/check_dirsize_perf.sh -d /tmp/ -w 100 -c 200
-bash: /usr/local/nagios/libexec/check_dirsize_perf.sh: /bin/sh^M: bad interpreter: No such file or directory

Solution:

You can use dos2unix command to make file usable(remove ^M). Use below command.

[root@localhost ~]# dos2unix -n /usr/local/nagios/libexec/check_dirsize_perf.sh /usr/local/nagios/libexec/check_dirsize_perf_1.sh
dos2unix: converting file /usr/local/nagios/libexec/check_dirsize_perf.sh to file /usr/local/nagios/libexec/check_dirsize_perf_1.sh in UNIX format ...

[root@localhost ~]# mv /usr/local/nagios/libexec/check_dirsize_perf_1.sh /usr/local/nagios/libexec/check_dirsize_perf.sh
mv: overwrite `/usr/local/nagios/libexec/check_dirsize_perf.sh'? y
[root@localhost ~]# /usr/local/nagios/libexec/check_dirsize_perf.sh -d /tmp -w 100 -c 200 -u mb
293  mb -  critical

Hope this will help.





Offsite LDAP server setup

To configure your offsite running LDAP server, you should have below prerequisites.
- Both Master and slave server running with the same slapd.conf file.
- Password less authentication configured to log in from Master server to slave server.

Slave server's IP address: 192.168.19.20

Run below shell script on Master server.

#!/bin/sh

#Below command will export from Master server
/usr/sbin/slapcat -f /etc/openldap/slapd.conf > /ldapbackup/ldap-` date +%d-%m-%Y`.ldif

###############Backup Restoration on LDAP Backup/slave server#########################

#Below command will stop LDAP service on Slave server
ssh root@192.168.19.20 "/etc/init.d/ldap stop"

#This command will remove LDAP schema directory from the server
ssh root@192.168.19.20 "rm -rf /var/lib/ldap/domain.com/"

#This command will create schema directory on Slave server
ssh root@192.168.19.20 "mkdir -p /var/lib/ldap/domain.com/"

#This command will set ldap as owner of Schema directory
ssh root@192.168.19.20 "chown -R ldap.ldap /var/lib/ldap/domain.com/"

#This command will copy backup from Master server to Slave server on /root/ldapback_letest.ldif location
scp /ldapbackup/ldap-` date +%d-%m-%Y`.ldif root@192.168.19.20:/root/ldapback_letest.ldif

#This command will start LDAP service on Slave server
ssh root@192.168.19.20 "/etc/init.d/ldap start"

#This command will stop LDAP service on Slave server
ssh root@192.168.19.20 "/etc/init.d/ldap stop"

#This command will restore LDAP backfile on Slave server
ssh root@192.168.19.20 "slapadd -v -c -l /root/ldapback_letest.ldif -f /etc/openldap/slapd.conf"

#This command will start LDAP on Slave server
ssh root@192.168.19.20 "/etc/init.d/ldap start"

Shell Script to create samba users in bulks

If you want to crate so many numbers of users at once, then you can do that by running the given shell script.

*********************************
#!/bin/bash
#
# Ensure that root is running the script.
##
WHOAMI=`/usr/bin/whoami`
if [ $WHOAMI != "root" ]; then
echo "You must be root to add news users!"
exit 1
fi
#
clear
NEW_USERS="/root/names.txt"
echo "Enter password you want to set for all users:"
read SMBPASS
cat $NEW_USERS | while read USER; do
#adduser $USER `echo $SMBPASS`; `echo $SMBPASS` | passwd --stdin $USER > /dev/null
adduser $USER -p $SMBPASS
echo Added user $USER
smbpasswd -e $USER -w $SMBPASS > /dev/null
(echo $SMBPASS; echo $SMBPASS) | smbpasswd -as $USER
echo -e "$USER = $USER" >> /etc/samba/smbusers
done
*********************************

Here in this script, you will have to give name of the users in /root/names.txt file line by line.
For Ex:
********
alpesh
keyur
ashish
********

Once you run this script, it will ask you for the password which you want to assign for each users.

I hope this will help you.