linux - 用于获取转发端口并插入到程序配置文件的 Bash 脚本

标签 linux bash ubuntu

我已经编写了一些 bash 脚本,但这超出了我的范围,我不知道从哪里开始。我正在运行 Xubuntu。我正在使用一个 VPN,它允许我转发一个转发端口,但只能通过运行以下行来生成下面的输出,每次都使用不同的端口。

Command:
./port_forward.sh user password
Output:
Loading port forward assignment information..
{"port:58661}

我正在寻找一个脚本来运行该进程,获取端口输出,然后将其放入 Deluge 的配置文件中,然后运行 ​​Deluge。我在下面粘贴了我当前的配置,32157 的两个实例需要更改为新端口。此文件位于/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"
}

这可能吗?如果有办法使用命令的输出,我想我可以将给定的端口号存储在一个单独的文件中,然后脚本将使用该文件来查找并替换为新的端口号,然后更改数字在下次运行的文件中。

预计到达时间:

谢谢!我不太确定你的意思是我会得到新的端口,所以我想到了这个:

#!/bin/bash
OLDPORT=`cat core.conf | grep -A2 listen_ports | tail -n1`
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' "core.conf"
deluge-gtk %U

它有效,但我确信有更简洁的方法可以做到这一点。如果有一种方法可以简单地获取 port_forward.sh 命令的输出中包含的任何 5 位数字并将其设为变量,我会很感兴趣。显然,我上面的内容并不理想,如果输出发生任何变化就会中断。是的,它确实只有一个双引号,不知道为什么。

ETA2:没关系,我想通了。再次感谢您的帮助!

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

最佳答案

例如,您可以使用 sed 将旧端口号更改为新端口号。

sed 's/32157/58661/g' core.conf > core.new
mv core.new core.conf

sed 会将旧端口号更改为新端口号并将输出重定向到名为 core.new 的新文件,然后 mv 将用新文件覆盖旧文件。

你也可以在脚本中完成,你只需要将旧端口和新端口设置为变量。

要将旧端口作为变量,您可以执行类似的操作。

oldport=`cat core.conf | grep -A2 listen_ports | tail -n1`

这将 cat core.conf 文件,查找 listen_ports 字符串并打印它,后面的两行和 tail 将得到最后一行,这将是端口号,当然,考虑到它总是两个端口号。

要获取新端口,您可以将其作为脚本的第一个参数传递,这样您就会得到类似的东西。

#!/bin/bash
oldport=`cat core.conf | grep -A2 listen_ports | tail -n1`
sed 's/$1/$oldport/g' core.conf > core.new
mv core.new core.conf

那个 port_forward.sh 的输出是对的?看起来它缺少双引号。不应该是这样吗?

{"port:58661"}

您还可以使用 awk 从该输出中获取端口值。

关于linux - 用于获取转发端口并插入到程序配置文件的 Bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29908313/

相关文章:

linux - msync 是否像 ext3 上的 fsync 一样将文件系统上的所有文件同步到磁盘?

c - Linux 终端 - 错误 : label at end of compound statement

python - osquery-python 扩展导致 osqueryi 错误

bash - 导出无法更改执行路径

linux - Awk 命令无法正常工作,输出错误,sed 命令?

bash - 你如何返回到源 bash 脚本?

linux - 为什么通过 "scriptName"调用脚本不起作用?

python-3.x -/usr/bin/python3 和/bin/python3 有什么区别

node.js - NodeJS 如何从 ubuntu 运行两个服务器文件?

c++ - 在 Ubuntu 12.04 上运行一个在 Windows 上的 MS Visual Studio 中用 c++11 编写的项目