The output of $ip coming blank
#!/bin/bash
for i in illinrcmsg{002..003}
do
echo " ####################### $i #################### "
ssh -o StrictHostKeyChecking=no $i "
echo $i
cp /etc/hosts /etc/hosts_bak
ip=`ifconfig | head -n 2 | tail -n 1 | awk '{print $2}' | cut -d':' -f2`
echo $ip
echo "$ip $i.corp.amdocs.com $i" >> /etc/hosts
"
done
I am trying to fix /etc/hosts entry on multiple servers with a bash script, but it is dumping on hostname values on the file.
This worked:
#!/bin/bash
for i in illinrcmsg{002..003}
do
echo " ####################### $i #################### "
ssh -o StrictHostKeyChecking=no $i "
echo $i
cp /etc/hosts /etc/hosts_bak
#ip=`ifconfig | head -n 2 | tail -n 1 | awk '{print $2}' | cut -d':' -f2`
#echo $ip
echo "`nslookup illinrcmsg002 | tail -n 2 | head -n 1|awk '{print $2}'`
$i.corp.amdocs.com $i" >> /etc/hosts
"
done
Related
I created a script that recovers info from a remote VM and generates a second script that creates a new ifcfg-eth* and replaces it in a local copy of the same VM we are migrating (basically the remote VM is in DHCP and we are migrating them in a new location with a static IP and info that we recover from the original VM):
#! /bin/bash
# Version 1.0
# Last review 27/01/2022
# Author Alessandro
# Define variables
RED='\033[0;31m'
NC='\033[0m' # No Color
NCC="\e[0m"
CYAN='\e[96m'
USER='user'
CWD=$(pwd)
# Checks if the IP exists after the script name
if [ -z "$1" ]; then echo -e "${RED}Where is the IP?${NC}" && exit 1; fi
echo -e "${CYAN}Starting...${NCC}"
# Stores the output of of 'script' in the variable $RAW_INPUT
RAW_INPUT=$(ssh -o StrictHostKeyChecking=no $USER#"$1" <<'SCRIPT'
# Script
HOSTNAMEVM=$(hostname -s)
GW=$(/sbin/ip route | awk '/default/ { print $3 }')
INTERFACE1=$(ip link | awk -F: ' $0 !~"lo|vir|wl|^[^1-2]" {print $2;getline}' | awk '{ gsub (" ", "", $0); print}')
IP1=$(ifconfig "${INTERFACE1}" 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://')
SUBNET1=$(/sbin/ifconfig "${INTERFACE1}" | grep Mask | cut -d":" -f4)
INTERFACE2=$(ip link | awk -F: ' $0 !~"lo|vir|wl|^[^3-4]" {print $2;getline}' | awk '{ gsub (" ", "", $0); print}')
IP2=$(ifconfig "${INTERFACE2}" 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://')
SUBNET2=$(/sbin/ifconfig "${INTERFACE2}" | grep Mask | cut -d":" -f4)
echo "Hostname:${HOSTNAMEVM}"
echo "GW:${GW}"
echo "Network-card1:${INTERFACE1}"
echo "${INTERFACE1}_IP1:${IP1}"
echo "SUBNET_1:${SUBNET1}"
echo "Network-card2:${INTERFACE2}"
echo "${INTERFACE2}_IP2:${IP2}"
echo "SUBNET_2:${SUBNET2}"
echo "ENDECA:${ENDECA}"
SCRIPT
)
# Generate the variables for the pre_failover_script parsing the RAW_INPUT variable
VM_NAME=$( echo "${RAW_INPUT}" | awk -F"Hostname:" '/Hostname:/{print $2}')
# Verifies if the executed pre failover script already exists
if [ -f "${CWD}"/executed_pre_"${VM_NAME}".sh ]; then echo -e "${RED}WARNING${NC}:The pre failover script for${NC} ${CYAN}${VM_NAME}${NC} has been already executed!" && exit 1; fi
JOB_ID=$("${CWD}"/list_jobs.sh | grep "${VM_NAME}" | awk -F"mnt/" '/mnt/{print $2}')
# Checks if a job exists for the vm name
if [ -z "${JOB_ID}" ]; then
echo -e "${RED}The job ID for${NC} ${VM_NAME} ${RED}doesn't exist.${NC}";
exit 1;
fi
IP_GATEWAY=$(echo "${RAW_INPUT}" | awk -F"GW:" '/GW:/{print $2}')
INTERFACE_1=$( echo "${RAW_INPUT}" | awk -F"card1:" '/card1:/{print $2}')
IP_1=$( echo "${RAW_INPUT}" | awk -F"IP1:" '/IP1:/{print $2}')
SUB_1=$( echo "${RAW_INPUT}" | awk -F"NET_1:" '/NET_1:/{print $2}')
INTERFACE_2=$( echo "${RAW_INPUT}" | awk -F"card2:" '/card2:/{print $2}')
IP_2=$( echo "${RAW_INPUT}" | awk -F"IP2:" '/IP2:/{print $2}')
SUB_2=$( echo "${RAW_INPUT}" | awk -F"NET_2:" '/NET_2:/{print $2}')
# Prints the value of ENDECA in case the Symbolic Link has been removed
ENDECA=$( echo "${RAW_INPUT}" | awk -F"ENDECA:" '/ENDECA:/{print $2}')
# Removing existing pre_failover_script file for the ECL2 instance in current directory
rm -rf "${CWD}"/pre_failover_script_"${VM_NAME}".sh
# Define pre_failover_script variable
PRE_FAILOVER_SCRIPT_LOCATION="${CWD}"/pre_failover_script_${VM_NAME}.sh
# Generate ifcfg-eth* file based on information gathered above
echo "#! /bin/bash
PATH_TO_JOB_ID=/opt/dbtk/mnt/${JOB_ID}
PATH_TO_UDEV_NET_RULES_FILES=/etc/udev/rules.d
PATH_TO_GATEWAY_FILE=/etc/sysconfig/network
CLOUD_INIT_LOCAL_STARTUP_SCRIPT=/etc/rc3.d/S50cloud-init-local
CLOUD_INIT_STARTUP_SCRIPT=/etc/rc3.d/S51cloud-init
CLOUD_CONFIG_STARTUP_SCRIPT=/etc/rc3.d/S52cloud-config
CLOUD_FINAL_STARTUP_SCRIPT=/etc/rc3.d/S53cloud-final
PATH_TO_IFCFG_FILES=/etc/sysconfig/network-scripts
rm -f \$PATH_TO_JOB_ID\$PATH_TO_UDEV_NET_RULES_FILES/70-persistent-net.rules*
echo \"GATEWAY=${IP_GATEWAY}\" >> \$PATH_TO_JOB_ID\$PATH_TO_GATEWAY_FILE
rm -f \$PATH_TO_JOB_ID\$CLOUD_INIT_LOCAL_STARTUP_SCRIPT
rm -f \$PATH_TO_JOB_ID\$CLOUD_INIT_STARTUP_SCRIPT
rm -f \$PATH_TO_JOB_ID\$CLOUD_CONFIG_STARTUP_SCRIPT
rm -f \$PATH_TO_JOB_ID\$CLOUD_FINAL_STARTUP_SCRIPT" > "${PRE_FAILOVER_SCRIPT_LOCATION}"
echo "sed '/^BOOTPROTO/d' \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_1} > \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_1}.new
{
echo 'NAME=${INTERFACE_1}'
echo 'IPADDR=${IP_1}'
echo 'NETMASK=${SUB_1}'
echo 'BOOTPROTO=static'
} >> \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_1}.new
cp \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_1} \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_1}.ori
mv \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_1}.new \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_1}
rm \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_1}.ori" >> "${PRE_FAILOVER_SCRIPT_LOCATION}"
echo "sed '/^BOOTPROTO/d' \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_2} > \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_2}.new
{
echo 'NAME=${INTERFACE_2}'
echo 'IPADDR=${IP_2}'
echo 'NETMASK=${SUB_2}'
echo 'BOOTPROTO=static'
} >> \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_2}.new
cp \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_2} \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_2}.ori
mv \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_2}.new \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_2}
rm \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_2}.ori" >> "${PRE_FAILOVER_SCRIPT_LOCATION}"
The script works perfectly, problem is that now I'm having multiple VMs with 3 or 1 NIC instead of 2 and I was wondering if there was a way to make a for loop that does the same thing but for each Network card. As you can see now I'm generating 'manually' the ifcfg-eth for each NC based on the info collected before:
{
echo 'NAME=${INTERFACE_1}'
echo 'IPADDR=${IP_1}'
echo 'NETMASK=${SUB_1}'
echo 'BOOTPROTO=static'
} >> \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_1}.new
cp \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_1} \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_1}.ori
mv \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_1}.new \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_1}
rm \$PATH_TO_JOB_ID\$PATH_TO_IFCFG_FILES/ifcfg-${INTERFACE_1}.ori" >> "${PRE_FAILOVER_SCRIPT_LOCATION}"```
Now it's no longer convenient but I'm not sure if what I'm trying to do is achievable.
I'd like to change my script(s) so that the command output is separated by a comma and is all on one line per host. Any ideas on how I can achieve this:
1st Script:
#!/bin/bash
for i in `cat ~/hostnames.txt`
do
ssh -q $i 'bash -s' < server_info.sh
done
2nd Script (server_info.sh):
#!/bin/bash
echo -n "Name: "
uname -n
echo -n "CPUs: "
cat /proc/cpuinfo* | grep processor | wc -l
echo -n "Memory (kb): "
cat /proc/meminfo | grep MemTotal: | awk '{print $2}'
echo -n "Current Kernel: "
uname -a | awk '{print $3}'
echo -n "IP: "
hostname -i
echo -e
Changing your 1st script:
#!/bin/bash
for i in cat ~/hostnames.txt
do
ssh -q $i 'bash -s' < server_info.sh
done | awk -v RS= '{$1=$1}1'
Note: Your server_info.sh can be a lot more optimized.For example:
cat /proc/meminfo | grep MemTotal: | awk '{print $2}'
could be changed to:
awk '/MemTotal:/{print $2}' /proc/meminfo
Below is my code and I am able to get the ssh connection. But after that it is doing nothing.
log_time="date +%F\%T"
PR_ONE="username#hostname"
file="/home/log.txt"
to_list="myemail"
echo "`$log_time`" >> $file
`ssh $PR_ONE "echo df -hP | grep fs1 | awk '{print $4}'"` >> $file
cat $file | mailx -s "Disk space usages" $to_list
I am getting the email but only with $log_time. I know that I am doing something wrong in quotations in the ssh line.
I am new to shell scripting.
Try this :
echo "df -hP | grep fs1 | awk '{print $4}'" | ssh -tt $PR_ONE >> $file
or prefer this :
ssh -tt $PR_ONE << EOF >> $file
df -hP | grep fs1 | awk '{print $4}'
EOF
Can I use hexdump in a shell script?
When I use it I keep getting an error .
syntax error near unexpected token 'hexdump'
#!/bin/bash
#bash-hexdump
# Quick script to check delay of the shotpoints
echo " please enter the complete line name as mentioned in the RAID2 "
read $line
cd /argus/raid2/"$line"
echo
echo " Entering the directory "
echo
for file in /argus/raid2/"$line"/*.ffid
hexdump -e "16 \"%_p\" \"\\n\"" $FFID | sed -n '68,73p' > list1
done
for filename in 'cat list1'
do
sed -n 6p | awk '{print $1}' = $wd
cat list.txt | sed -n 1p | cut -c13-14 = $hh
cat list.txt | sed -n 1p | cut -c15-16 = $mm
cat list.txt | sed -n 2p | cut -c1-2 = $ss
done
while [ true ]
do
$FFID=`ls -1rt $1 | grep -i ffid | tail -1`
echo " FFID value is : $FFID"
while [ $FFID = `ls -1rt $1 | grep -i ffid | tail -1` ]
do
hexdump -e "16 \"%_p\" \"\\n\"" $FFID | sed -n '68,73p' > list
done
for filename in 'cat list'
do
cat list.txt | sed -n 1p | cut -c13-14 = $hh1
cat list.txt | sed -n 1p | cut -c15-16 = $mm1
cat list.txt | sed -n 2p | cut -c1-2 = $ss1
done
$time1 = "$hh"":""$mm"":""$ss" ;
$time2 = "$hh1"":""$mm1"":""$ss1" ;
$former_seconds = $(date --date= "$time1" +%s);
$later_seconds = $(date --date= "$time2" +%s);
$time_delay = ( "$later_seconds" - "$former_seconds" )
$wb_time = ( "$wd" * 1.33 )
if
(("$wb_time" + "$time_delay")) < 12.0
then
echo "please slow down"
fi
if [ -e EOL.ffid ]
then
echo "EOL.ffid detected, exiting script"
exit
fi
done
I am not able to figure out why the hexdump code is giving me an error . Please help .
You are missing the do in your for loop:
for file in /argus/raid2/"$line"/*.ffid
do
hexdump -e "16 \"%_p\" \"\\n\"" $FFID | sed -n '68,73p' > list1
done
I currently have a script that checks the MAC address, tt looks like this
"m"|"-m" )
if [ ! -n "$2" ]
then
echo "Enter MAC address"
exit 0
fi
out1=`cat $dhcp_files/* | grep -i "$2" 2>/dev/null`
ip=`cat $dhcp_files/* | grep -i "$2" | awk '{print $8}' | sed s/\;//g 2>/dev/null`
out=` grep -i $ip $shapy/* | awk '{print $8}' 2>/dev/null`
echo -en ''$out1' on '$out' \n'
this is done like that: ./mac_check -m 00:00:00:00:00:00
The result of this script:
host Jon_Rosewelt_6_5 { hardware ethernet 00:1d:4e:b5:d4:10; fixed-address 192.168.101.19; } on eth4
but I would like to do to the script received any form of mac address
aka. ./check_mac -m 001d.4eb5.d410 **or** 001d4eb5d410 **or** 00-1d-4e-b5-d4-10
How can I do that?
With GNU sed and Solaris sed:
echo 001d.4eb5.d410 | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//'
echo 001d4eb5d410 | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//'
echo 00-1d-4e-b5-d4-10 | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//'
echo 00:1d:4e:b5:d4:10 | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//'
Output:
00:1d:4e:b5:d4:10
00:1d:4e:b5:d4:10
00:1d:4e:b5:d4:10
00:1d:4e:b5:d4:10
With your code:
"m"|"-m" )
mac="$2" # added
if [ ! -n "$mac" ] # changed
then
echo "Enter MAC address"
exit 0
fi
mac="$(echo "$mac" | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//')" # added
out1=`cat $dhcp_files/* | grep -i "$mac" 2>/dev/null` # changed
ip=`cat $dhcp_files/* | grep -i "$mac" | awk '{print $8}' | sed s/\;//g 2>/dev/null` # changed
out=` grep -i $ip $shapy/* | awk '{print $8}' 2>/dev/null`
echo -en ''$out1' on '$out' \n'