使用ruby查找数据并使用数据执行bash
我想做的是使用Siriproxy来控制我的Sky+盒(我已经在一定程度上工作了) Sky使用频道编号,但将其转换为它理解的十六进制数字。我有所有频道编号、频道名称和十六进制代码的文件。我想制作一个ruby脚本,查找我说的频道号的十六进制,然后通过bash脚本将其作为命令的一部分发送到sky box(除非有更简单的方法) 我目前有一个非常粗糙和冗长的方法,包括多个bash脚本创建.txt文件等,但我相信一定有更好的方法 使用更多信息编辑:使用ruby查找数据并使用数据执行bash,ruby,bash,lookup,Ruby,Bash,Lookup,我想做的是使用Siriproxy来控制我的Sky+盒(我已经在一定程度上工作了) Sky使用频道编号,但将其转换为它理解的十六进制数字。我有所有频道编号、频道名称和十六进制代码的文件。我想制作一个ruby脚本,查找我说的频道号的十六进制,然后通过bash脚本将其作为命令的一部分发送到sky box(除非有更简单的方法) 我目前有一个非常粗糙和冗长的方法,包括多个bash脚本创建.txt文件等,但我相信一定有更好的方法 使用更多信息编辑: #demonstrate capturing data f
#demonstrate capturing data from the user (e.x. "Siri proxy number 15")
listen_for /change the channel number ([0-9,]*[0-9])/i do |number|
#say "Changing the Channel to #{number}"
f = File.new("/root/SiriProxy/sky/channel.txt", "w")
f.puts "SKY#{number}"
f.close
#system("echo [Info - Plugin Manager] Channel number #{number} stored to sky/channel.txt")
system("sh /root/SiriProxy/sky/change_channel.sh")
File.open('/root/SiriProxy/sky/channel_name.txt').each_line{ |s|
#puts s
say "I've changed the channel to #{s}"
}
request_completed
end
然后是bash脚本:
#!/bin/bash
for line in `cat /root/SiriProxy/sky/channel.txt`
do
cat /root/SiriProxy/sky/channel_numbers.txt | grep $line | cut -d" " -f3 > /root/SiriProxy/sky/hex.txt
done
for line in `cat /root/SiriProxy/sky/channel.txt`
do
cat /root/SiriProxy/sky/channel_numbers.txt | grep $line | cut -d" " -f2 > /root/SiriProxy/sky/channel_name.txt
done
for line in `cat /root/SiriProxy/sky/hex.txt`
do
(echo 'POST /SkyPlay HTTP/1.1
SOAPACTION: "urn:schemas-nds-com:service:SkyPlay:2#SetAVTransportURI"
Content-Type: text/xml; charset=utf-8
Content-Length: 399
<?xml version="1.0" encoding="utf-8"?><s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><u:SetAVTransportURI xmlns:u="urn:schemas-nds-com:service:SkyPlay:2"><InstanceID>0</InstanceID><CurrentURI>xsi://'$line'</CurrentURI><CurrentURIMetaData>NOT_IMPLEMENTED</CurrentURIMetaData></u:SetAVTransportURI></s:Body></s:Envelope>') | nc -n 192.168.1.82 49153
done
#/bin/bash
对于'cat/root/SiriProxy/sky/channel.txt中的行`
做
cat/root/SiriProxy/sky/channel_numbers.txt | grep$line | cut-d”“-f3>/root/SiriProxy/sky/hex.txt
完成
对于'cat/root/SiriProxy/sky/channel.txt中的行`
做
cat/root/SiriProxy/sky/channel_numbers.txt | grep$line | cut-d”“-f2>/root/SiriProxy/sky/channel_name.txt
完成
对于'cat/root/SiriProxy/sky/hex.txt中的行`
做
(echo’POST/SkyPlay HTTP/1.1
SOAPACTION:“urn:com:service:SkyPlay:2#SetAVTransportURI”
内容类型:text/xml;字符集=utf-8
内容长度:399
0xsi://“$line”未实现”)| nc-n 192.168.1.82 49153
完成
谢谢你的帮助,这比我的新手身份稍微高一点 我重构了从bash脚本读取的所有文件。请参阅
net/http
文档以发送post请求
这里是一个片段。我不确定这是否有效,因为我没有这些文件。但这应该给你一个公平的想法
#!/usr/bin/env ruby
require 'net/http'
channel_numbers_file = '/root/SiriProxy/sky/channel.txt'
listen_for /change the channel number ([0-9,]*[0-9])/i do |number|
sky_number = "SKY#{number}"
#Check whether there are multiple occurrences. No idea whether there are multiple occurrences.Gets only 1st one.
#Remove commma to get all in an array.
line, = File.open(channel_numbers_file).readlines.select{ |line| line.chomp.include? sky_number } #grep $line part
hex, channel_name = line.split[1..2] #check indices. ruby indices start from 0.
#Refer net/http documentation on POST request.
#send request
end
不确定这是否有帮助。提供文件样本、您与盒子的沟通方式等。此外,您是否有具体问题?这不是一个编写代码的plz论坛。抱歉@KarolyHorvath我已经添加了我当前使用的代码。我是否可以更改代码,以便从Ruby而不是bash发送最后一个SOAP命令?我建议查看此论坛,而不是使用Net::HTTP。HTTP是一个很好的构建块,但是需要大量的代码来提供使用内置POST的HTTPClient自动生成的内容。它看起来很棒。我说这会打开频道号码文件,搜索号码,然后取相应的十六进制键,对吗?为了执行SOAP命令,是否可以将十六进制数输出到bash文件?@user2689371您可以通过将bash(您的SOAP netcat)代码包装在
%x[command]
中,从ruby执行它们HTTPClient
有很多优点。