missymadi
asked on
How to write a Linux script to automate a command?
Experts,
I am working on a pc that I need to type the following command:
route add -host 10.10.10.1 subnet 0.0.0.0 gw 10.10.0.0 (I may not have the syntax exactly correct) How would I write a linux script to automate this? Maybe run it when the system boots?
Thanks, Missymadi
I am working on a pc that I need to type the following command:
route add -host 10.10.10.1 subnet 0.0.0.0 gw 10.10.0.0 (I may not have the syntax exactly correct) How would I write a linux script to automate this? Maybe run it when the system boots?
Thanks, Missymadi
place a shell script to do it in /etc/init.d and place a symlink to it in /etc/rc5.d (assuming that's your default level)
ASKER
"The idea itself is somehow 'no good'. Depending on your distribution there are other way to add a route and make it a permanent add in its routing table."
For the purpose of this, I am testing two hosts with a network emulator in between. I only want host1 and host 2 to go through the gateway. Then I am recording the effects of the noise (delay, jitter, etc) So it wouldn't be permanent.
Is there a better way to write the script or is running the script manually the best way. How about add to the script to undo the route command?
Run the script and ask the user if they want to re route to another gateway, if yes, accept the ip, and gateway (subnet will always be 0.0.0.0 -for this test) or ask the user to undo the current ip route. Is this possible?
For the purpose of this, I am testing two hosts with a network emulator in between. I only want host1 and host 2 to go through the gateway. Then I am recording the effects of the noise (delay, jitter, etc) So it wouldn't be permanent.
Is there a better way to write the script or is running the script manually the best way. How about add to the script to undo the route command?
Run the script and ask the user if they want to re route to another gateway, if yes, accept the ip, and gateway (subnet will always be 0.0.0.0 -for this test) or ask the user to undo the current ip route. Is this possible?
You need to verify "route add" and "route del" command first.
#!/bin/bash
echo "Your current route table"
netstat -nr
echo "Do you want to add route via 10.10.0.0? (Y/n)"
read ANSWER1
case "$ANSWER1" in
Y | y)
echo "Add 10.10.10.0 to your route table"
route add -host 10.10.10.1 subnet 0.0.0.0 gw 10.10.0.0
echo "Your current route table"
netstat -nr
;;
*)
echo "Nothing add to your route table"
;;
esac
echo "Do you want to remove route via 10.10.0.0? (Y/n)"
read ANSWER2
case "$ANSWER2" in
Y | y)
echo "Remove 10.10.10.0 from your route table"
route del -host 10.10.10.1 subnet 0.0.0.0 gw 10.10.0.0
echo "Your current route table"
netstat -nr
;;
*)
echo "Nothing add to your route table"
;;
esac
ASKER
How do I include the VM that I have to change the route for?
Manually I just start up the vm or ssh into it. Can I add a command to ssh and run through the whole script above?
#need to ssh into a local VM
ssh root@00.00.00.00
#reroute traffic
route add –host 10.5.79.96 netmask 0.0.0.0 gw 10.5.112.45
#make backup file
cp /home/rc/file1 /home/rc/file1ORIG
#copy new file
cp /home/rc/
Manually I just start up the vm or ssh into it. Can I add a command to ssh and run through the whole script above?
#need to ssh into a local VM
ssh root@00.00.00.00
#reroute traffic
route add –host 10.5.79.96 netmask 0.0.0.0 gw 10.5.112.45
#make backup file
cp /home/rc/file1 /home/rc/file1ORIG
#copy new file
cp /home/rc/
As your original question, there is no VM.
My script is to run on host1 or host2.
It seems you mix another question to this one.
My script is to run on host1 or host2.
It seems you mix another question to this one.
ASKER
Sorry about that - yes I confused with another question. I am going to stick to running with one script on each OS.
I tried your script but it would route the IP then take it back out.
I was wondering if we could ask the user for an IP address and gateway address (subnet remains the same) in case the IP address ever changed. How would I do that?
I tried your script but it would route the IP then take it back out.
I was wondering if we could ask the user for an IP address and gateway address (subnet remains the same) in case the IP address ever changed. How would I do that?
> your script but it would route the IP then take it back out.
If you type "n" for second question, then the route stay.
> ask the user for an IP address and gateway address (subnet remains the same) in case the IP address ever changed
If you type "n" for second question, then the route stay.
> ask the user for an IP address and gateway address (subnet remains the same) in case the IP address ever changed
#!/bin/bash
echo "Your current route table"
netstat -nr
echo "Please type in your IP address"
read IP
echo "Please type in your gateway IP address"
read GW
echo "Add $GW to your route table"
route add -host ${IP} subnet 0.0.0.0 gw ${GW}
echo "Your current route table"
netstat -nr
ASKER
I closed the other question since I will not be using SSH into the VM. There is just too much that could go wrong.
The above is exactly what I'm looking for! I would also like to add two files that would be backed up (renamed) and a new file with new configurations in its place.
However, I'm getting the following error : cp:cannot stat 'opt/apps :no such file or directory"
How do I fix this error? I'm trying to make it easy for the user and just run a script without moving files around.
thanks! Missymadi
The above is exactly what I'm looking for! I would also like to add two files that would be backed up (renamed) and a new file with new configurations in its place.
However, I'm getting the following error : cp:cannot stat 'opt/apps :no such file or directory"
How do I fix this error? I'm trying to make it easy for the user and just run a script without moving files around.
thanks! Missymadi
#create backup
cp /opt/apps/default/CRC /opt/apps/default/CRCOrig
#Copy new file
cp /home/user/Desktop/newfile /opt/apps/default/newfile
Does /opt/apps exist?
do
ls -l /opt/apps/
do
ls -l /opt/apps/
ASKER
It does exist.
If I run the script from / then it runs fine. ( I also tested successfully manually) But when I run from the Desktop (from command line cp /opt/apps/default/CRC /opt/apps/default/CRCOrig the script errors with 'cp'...
If I run the script from / then it runs fine. ( I also tested successfully manually) But when I run from the Desktop (from command line cp /opt/apps/default/CRC /opt/apps/default/CRCOrig the script errors with 'cp'...
> I run from the Desktop
Did you run as root, or on the same machine?
Did you run as root, or on the same machine?
ASKER
I ran locally as a user (not root)
As a user, can you do
ls -l /opt/apps/
ls -l /opt/apps/
ASKER
user rwx r-x r-x
also, when I ran the below code. The gateway does not set. It seems when I use IP and GW the system does not recognize and the gateway is not set. The original gateway remains.
Thanks, Missymadi
also, when I ran the below code. The gateway does not set. It seems when I use IP and GW the system does not recognize and the gateway is not set. The original gateway remains.
Thanks, Missymadi
#!/bin/bash
echo "Your current route table"
netstat -nr
echo "Please type in your IP address"
read IP
echo "Please type in your gateway IP address"
read GW
echo "Add $GW to your route table"
route add -host ${IP} subnet 0.0.0.0 gw ${GW}
echo "Your current route table"
netstat -nr
> . It seems when I use IP and GW the system does not recognize and the gateway is not set
"route" command need to run as "root" in Linux world.
Without "root" privilege, you can not set the route.
"route" command need to run as "root" in Linux world.
Without "root" privilege, you can not set the route.
ASKER
What is the syntax to set access to root in my script?
su -c "route add ...."
But it will prompt you for root password.
But it will prompt you for root password.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Please post
ls -l /opt/apps/default/
For simplicity, do (as root)
chmod -R 777 /opt/apps/
ls -l /opt/apps/default/
For simplicity, do (as root)
chmod -R 777 /opt/apps/
ASKER
It's all working now! I had to write into the instructions that the installer must be "root"
2. write
Open in new window
3. execute: chmod 755 <filename>4. execute file ./<filename>
For startup you need to write a startupscript, but this depends a little on your linux distribution.
The idea itself is somehow 'no good'. Depending on your distribution there are other way to add a route and make it a permanent add in its routing table.