Help writing an Ubuntu script

talleymj

Distinguished
Apr 2, 2012
96
0
18,660
I've written some bash scripts, but this is just beyond me and I have no idea where to start. I'm using a VPN that allows me to have a forwarded port forwarded, but only by running the following line, which produces the output below, with a different port each time.

Command:
./port_forward.sh <user> <password>

Output:
Loading port forward assignment information..
{"port:58661}

I'm looking to make a script that runs that process, takes the port output, and then puts it into the configuration file for Deluge, and then runs Deluge. I've pasted my current configuration below, with the two numbers that need to be replaced in bold. This file is located at /home/user/.config/deluge/core.conf

{
"file": 1,
"format": 1
}{
"info_sent": 0.0,
"lsd": true,
"send_info": false,
"move_completed_path": "/media/sf_Storage2/Download folder",
"enc_in_policy": 1,
"queue_new_to_top": false,
"ignore_limits_on_local_network": true,
"rate_limit_ip_overhead": true,
"daemon_port": 58846,
"natpmp": true,
"max_active_limit": 8,
"utpex": true,
"max_active_downloading": 5,
"max_active_seeding": 0,
"allow_remote": false,
"max_half_open_connections": 50,
"download_location": "/media/sf_Storage2/Temp Download Folder",
"compact_allocation": false,
"max_upload_speed": -1.0,
"cache_expiry": 60,
"prioritize_first_last_pieces": false,
"auto_managed": true,
"enc_level": 2,
"max_connections_per_second": 20,
"dont_count_slow_torrents": true,
"random_outgoing_ports": true,
"max_upload_slots_per_torrent": -1,
"new_release_check": false,
"enc_out_policy": 1,
"outgoing_ports": [
0,
0
],
"seed_time_limit": 180,
"cache_size": 512,
"share_ratio_limit": 2.0,
"max_download_speed": -1.0,
"geoip_db_location": "/usr/share/GeoIP/GeoIP.dat",
"torrentfiles_location": "/home/user/Downloads",
"stop_seed_at_ratio": false,
"peer_tos": "0x00",
"listen_interface": "",
"upnp": true,
"max_download_speed_per_torrent": -1,
"max_upload_slots_global": 4,
"enabled_plugins": [],
"random_port": false,
"autoadd_enable": false,
"max_connections_global": 200,
"enc_prefer_rc4": true,
"listen_ports": [
32157,
32157
],
"dht": true,
"stop_seed_ratio": 2.0,
"seed_time_ratio_limit": 7.0,
"max_upload_speed_per_torrent": -1,
"copy_torrent_file": false,
"del_copy_torrent_file": false,
"move_completed": true,
"proxies": {
"peer": {
"username": "",
"password": "",
"type": 0,
"hostname": "",
"port": 8080
},
"web_seed": {
"username": "",
"password": "",
"type": 0,
"hostname": "",
"port": 8080
},
"tracker": {
"username": "",
"password": "",
"type": 0,
"hostname": "",
"port": 8080
},
"dht": {
"username": "",
"password": "",
"type": 0,
"hostname": "",
"port": 8080
}
},
"add_paused": false,
"max_connections_per_torrent": -1,
"remove_seed_at_ratio": false,
"autoadd_location": "/home/user/Downloads",
"plugins_location": "/home/user/.config/deluge/plugins"
}

Is this possible? If there's a way to use the output from the command, I would think I could store a given port number in a seperate file that would then be used by the script to find the and replace with the new port number, and then change the number in the file for the next run.
 
Solution
Figured it out. In case anyone's interested:

#!/bin/bash
OLDPORT=`cat core.conf | grep -A2 listen_ports | tail -n1`
PORT=$(~/port_forward.sh user password | grep -o "[0-9][0-9][0-9][0-9][0-9]")
sed -i "s/$OLDPORT/$PORT/g" "core.conf"
deluge-gtk

talleymj

Distinguished
Apr 2, 2012
96
0
18,660
I wouldn't claim this is super important. And I'll probably end up spending more time figuring out how to do this than I'd ever spend doing it manually, but this is how I learn. SED is looking kind of daunting, but I'm definitely willing to go research that to figure out how to edit the file now that you've given me a starting point. Any similar tip about how to extract data from the original terminal output I showed above? Because I feel like grep might be the way to go, but I'm not quite finding a way to do that.
 

talleymj

Distinguished
Apr 2, 2012
96
0
18,660
This turned out to be easier than I expected.

#!/bin/bash
OLDPORT=`cat /home/user/port.var`
A=$(~/port_forward.sh user password)
B=$(echo -e "$A" | sed -n '2p')
PORT=`echo $B| cut -c9-13`
sed -i 's/'$OLDPORT'/'$PORT'/g' "/home/user/.config/deluge/core.conf"
sed -i 's/'$OLDPORT'/'$PORT'/g' "/home/user/port.var"
deluge-gtk %U

It works, though I'm certain there are cleaner ways to do it. I would be interested if anyone could tell me how to simply take whatever 5 digit number is contained in the output of the port_forward.sh command and make it a variable. Obviously what I have above is not ideal and will break if the output is changed at all.
 

talleymj

Distinguished
Apr 2, 2012
96
0
18,660
Figured it out. In case anyone's interested:

#!/bin/bash
OLDPORT=`cat core.conf | grep -A2 listen_ports | tail -n1`
PORT=$(~/port_forward.sh user password | grep -o "[0-9][0-9][0-9][0-9][0-9]")
sed -i "s/$OLDPORT/$PORT/g" "core.conf"
deluge-gtk
 
Solution