Avatar of missymadi
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
LinuxShell Scripting

Avatar of undefined
Last Comment
missymadi

8/22/2022 - Mon
Ferrosti

1. open a file
2. write
#/bin/bash
route add -host 10.10.10.1 subnet 0.0.0.0 gw 10.10.0.0

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.
point_pleasant

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)
missymadi

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?
Your help has saved me hundreds of hours of internet surfing.
fblack61
wesly_chen

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

Open in new window

missymadi

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/
wesly_chen

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
missymadi

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?
wesly_chen

>  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
#!/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

Open in new window

missymadi

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
#create backup
cp /opt/apps/default/CRC /opt/apps/default/CRCOrig

#Copy new file
cp /home/user/Desktop/newfile  /opt/apps/default/newfile

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
wesly_chen

Does /opt/apps exist?
do
ls -l /opt/apps/
missymadi

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'...
wesly_chen

> I run from the Desktop
Did you run as root, or on the same machine?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
missymadi

ASKER
I ran locally as a user (not root)
wesly_chen

As a user, can you do
ls -l /opt/apps/
missymadi

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
#!/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

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
wesly_chen

> . 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.
missymadi

ASKER
What is the syntax to set access to root in my script?
wesly_chen

su -c "route add ...."

But it will prompt you for root password.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
wesly_chen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
wesly_chen

Please post
ls -l /opt/apps/default/
For simplicity, do (as root)
chmod -R 777 /opt/apps/
missymadi

ASKER
It's all working now! I had to write into the instructions that the installer must be "root"