1

Background: I created a VPN service for myself. I have a lot of time on my hands and I enjoy the challenge of creating "business like" services, so I can kind of create a portfolio for myself. I created this script to create a client profile for OpenVPN and want to clean it up. It is sloppy, but I am working on cleaning it up now that it works.

My question: I want to create a pre-determined file for a client to use. Certain data in it has to be added to it, based on previous variables.

The current script:

#!/bin/bash

### Set PATH ###
BACKUP=/var/BACKUPS
OVPN=/etc/openvpn/
CA=/etc/openvpn/ca.crt
RSA=/etc/openvpn/easy-rsa
KEYS=/etc/openvpn/easy-rsa/keys
HTML=/var/dir/for/user/dl

### Move to working directory ###
cd $RSA

### Trial/30 day/90 day/365 day ###
PS3='What kind of subscriber is being created?: '
options=("7 Day Trial" "30 Day Subscriber" "90 Day Subscriber" "1 Year Subscriber" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "7 Day Trial")
            echo -e "You have selected 7 day trial.\nUsing vars7."
            source ./vars7
            break
            ;;
        "30 Day Subscriber")
            echo -e "You have selected 30 day subscriber.\nUsing vars30."
            source ./vars30
            break
            ;;
        "90 Day Subscriber")
            echo -e "You have selected 90 day subscriber.\nUsing vars90."
            source ./vars90
            break
            ;;
        "1 Year Subscriber")
            echo -e "You have selected 1 year subscriber.\nUsing vars365."
            source ./vars365
            break
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
done

### Catch input for user and build/sign keys and certificates ###
echo "Please enter the user's name:"
read USER
echo "Starting process now..." 
./build-key $USER

### Set new PATHs ###
BASECRT=$(sed -n 68,97p $KEYS/$USER.crt)
BASECA=$(cat $CA)
BASEKEY=$(cat $KEYS/$USER.key)

### create stream-lined option file ###
cat > $OVPN/$USER.ovpn <<EOF
client
;dev tap
dev tun
;proto tcp
proto udp
remote SERVER 1194
remote SERVER 1194
remote-random
resolv-retry infinite
nobind
;user nobody
;group nogroup
persist-key
persist-tun
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]
;mute-replay-warnings
ca ca.crt
cert $USER.crt
key $USER.key
ns-cert-type server
;tls-auth ta.key 1
;cipher x
comp-lzo
verb 3
;mute 20
<ca>
$BASECA
</ca>
<cert>
$BASECRT
</cert>
<key>
$BASEKEY
</key>

EOF

### Make Directories needed ###
mkdir $HTML/$USER
mkdir $BACKUP/$USER
mkdir $HTML/$USER/keys

### Store Backups ###
cp $KEYS/$USER.* $BACKUP/$USER
cp $OVPN/$USER.ovpn $BACKUP/$USER
tar -czvf $BACKUP/$USER.tar.gz $BACKUP/$USER

### Move keys and options to user's directory for download ###
cp $KEYS/$USER.* $HTML/$USER/keys
cp $OVPN/$USER.ovpn $HTML/$USER
cp $CA $HTML/$USER/keys
chmod 755 $HTML/$USER/*

I had used cat > $OVPN/$USER.ovpn << EOF to create the stream-lined USER.ovpn. $BASECRT, $BASEKEY, etc are replaced with their respective files encoded in base64.

I want to get rid of this massive block of text from my script. I've toyed with using cat < shellfile > Newfile — but this does not replace the variables, and instead adds them to the new file as $BASECRT, etc

Now, I could use 'sed' to read the first bunch of lines outputted into the client file, cat append to the client file, then sed the next few lines, repeat this process until I get the completed client file.

But there has to be an easier and cleaner way to do this. I've never had to create a long file like this. Thank you. :)

1

You could use the source command, put the create stream-lined option file commands into a separate file, and source it into the main script when all the variables used has been assigned,

# USER.ovpn 

### create stream-lined option file ###
cat > $OVPN/$USER.ovpn <<EOF
client
;dev tap
dev tun
;proto tcp
proto udp
remote SERVER 1194
remote SERVER 1194
remote-random
resolv-retry infinite
nobind
;user nobody
;group nogroup
persist-key
persist-tun
;http-proxy-retry # retry on connection failures
;http-proxy [proxy server] [proxy port #]
;mute-replay-warnings
ca ca.crt
cert $USER.crt
key $USER.key
ns-cert-type server
;tls-auth ta.key 1
;cipher x
comp-lzo
verb 3
;mute 20
<ca>
$BASECA
</ca>
<cert>
$BASECRT
</cert>
<key>
$BASEKEY
</key>

EOF

Your main script could then do,

#!/bin/bash

### Set PATH ###
BACKUP=/var/BACKUPS
OVPN=/etc/openvpn/
CA=/etc/openvpn/ca.crt
RSA=/etc/openvpn/easy-rsa
KEYS=/etc/openvpn/easy-rsa/keys
HTML=/var/dir/for/user/dl

### Move to working directory ###
cd $RSA

### Trial/30 day/90 day/365 day ###
PS3='What kind of subscriber is being created?: '
options=("7 Day Trial" "30 Day Subscriber" "90 Day Subscriber" "1 Year Subscriber" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "7 Day Trial")
            echo -e "You have selected 7 day trial.\nUsing vars7."
            source ./vars7
            break
            ;;
        "30 Day Subscriber")
            echo -e "You have selected 30 day subscriber.\nUsing vars30."
            source ./vars30
            break
            ;;
        "90 Day Subscriber")
            echo -e "You have selected 90 day subscriber.\nUsing vars90."
            source ./vars90
            break
            ;;
        "1 Year Subscriber")
            echo -e "You have selected 1 year subscriber.\nUsing vars365."
            source ./vars365
            break
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
done

### Catch input for user and build/sign keys and certificates ###
echo "Please enter the user's name:"
read USER
echo "Starting process now..." 
./build-key $USER

### Set new PATHs ###
BASECRT=$(sed -n 68,97p $KEYS/$USER.crt)
BASECA=$(cat $CA)
BASEKEY=$(cat $KEYS/$USER.key)

### create stream-lined option file ###

source USER.ovpn     # ". USER.ovpn" also works

### Make Directories needed ###
mkdir $HTML/$USER
mkdir $BACKUP/$USER
mkdir $HTML/$USER/keys

### Store Backups ###
cp $KEYS/$USER.* $BACKUP/$USER
cp $OVPN/$USER.ovpn $BACKUP/$USER
tar -czvf $BACKUP/$USER.tar.gz $BACKUP/$USER

### Move keys and options to user's directory for download ###
cp $KEYS/$USER.* $HTML/$USER/keys
cp $OVPN/$USER.ovpn $HTML/$USER
cp $CA $HTML/$USER/keys
chmod 755 $HTML/$USER/*
I had used cat > $OVPN/$USER.ovpn 
  • Thanks. Using cat > /path/file <<EOF xxxxxxxxxxx EOF works to create a file in /path/file with variables being used – Freedo Oct 17 '17 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.