linux, virtualbox and centOS7 Write a script in BASH that automatically writes y
ID: 3840405 • Letter: L
Question
linux, virtualbox and centOS7
Write a script in BASH that automatically writes your system’s private (eth0) and public IP addresses to a file within your home directory called ipaddresses. The automatically generated ipaddresses file should contain the following information:
The date command must be used to provide a timestamp at the top of the file in the following format: Timestamp: (HH:MM) MMDDYYYY. The next line should contain: ETH0 IP: XXX.XXX.XXX.XXX. The last line should contain: Public IP: XXX.XXX.XXX.XXX.
The script should overwrite any current entries with the new entries, not append them to the file.
Don’t forget to change the permissions on the script so that it is executable!
Setup a cron job to run the script every 30 mins, you can verify this works by checking the timestamp in the ipaddresses file to see if it is being updated correctly, make sure you let your VM run for a few hours during testing.
Be sure to include a copy of your .bashrc file, a copy of your ipaddresses script and output file, and your cron job line in your lab report along with a description of how you set it up and what it means.
Explanation / Answer
Your Script goes here :
#!/bin/bash
date=$(date +(%R)"%m%d%Y")
ip=$( ifconfig eth0 | sed -n '2p' | awk '{print $2}' | awk -F : '{print $2}' )
pubip=$(wget -qO- ipinfo.io/ip)
printf $date" "$ip" "$pubip" "i > ~/ip.info ;
#end
Save file in home directory by name "filename.sh" and change permission using following command:
"sudo chmod +x filename.sh"
The edit crontab using command : "crontab -e".
Content of crontab is as follows:
30 * * * * ~/filename.sh
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.