pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

在 shell 腳本中嵌入可執行二進制文件

Embed a Executable Binary in a shell script(在 shell 腳本中嵌入可執行二進制文件)
本文介紹了在 shell 腳本中嵌入可執行二進制文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

首先,我已經在 Google 上搜索過,但只找到了將壓縮文件(例如 .tar.gz)嵌入到 shell 腳本中的示例.

First, I already googled but only found examples where a compressed file (say a .tar.gz) is embedded into a shell script.

基本上,如果我有一個打印字符串的 C 程序 (hello.c),比如 Hello World!.

Basically if I have a C program (hello.c) that prints a string, say Hello World!.

我編譯它以獲得可執行的二進制文件

I compile it to get an executable binary

gcc hello.c -o hello

現在我有一個 shell 腳本 testEmbed.sh

Now I have a shell script testEmbed.sh

我要問的是是否可以將二進制文件 (hello) 嵌入到 shell 腳本中,以便在我運行時

What I am asking is if it is possible to embed the binary (hello) inside the shell script so that when I run

./testEmbed.sh

它執行二進制文件以打印 Hello World!.

it executes the binary to print Hello World!.

說明:一種替代方法是我將可執行文件壓縮到存檔中,然后在腳本運行時將其解壓縮.我要問的是是否可以在沒有它的情況下運行該程序.

Clarification: One alternative is that I compress the executable into an archive and then extract it when the script runs. What I am asking is if it is possible to run the program without that.

到目前為止,我一直在嘗試這里的方法.但這對我不起作用.我猜作者是在另一個架構上使用其他發行版.所以,基本上這對我不起作用.:P

Up until now, I was trying the method here. But it does not work for me. I guess the author was using some other distribution on another architecture. So, basically this did not work for me. :P

另外,如果 C 程序的工作流程與 Java jar 不同,我也想知道!

Also, if the workflow for a C program differs from a Java jar, I would like to know that too!

推薦答案

是的,可以這樣做.它實際上與您鏈接的文章在概念上非常相似.訣竅是使用 uuencode 將二進制文件編碼為文本格式,然后將其附加到腳本的末尾.

Yes, this can be done. It's actually quite similar in concept to your linked article. The trick is to use uuencode to encode the binary into text format then tack it on to the end of your script.

然后您的腳本以這樣的方式編寫,它在自身上運行 uudecode 以創建二進制文件,更改權限然后執行它.

Your script is then written in such a way that it runs uudecode on itself to create a binary file, change the permissions then execute it.

uuencodeuudecode 最初是為了將二進制內容轉移到互聯網的前身而創建的,互聯網不能很好地處理二進制信息.轉換為文本意味著它也可以作為 shell 腳本提供.如果,由于某種原因,當您嘗試運行 uuencode 時您的發行版報錯,這可能意味著您必須安裝它.例如,在 Debian Squeeze 上:

uuencode and uudecode were originally created for shifting binary content around on the precursor to the internet, which didn't handles binary information that well. The conversion into text means that it can be shipped as a shell script as well. If, for some reason your distribution complains when you try to run uuencode, it probably means you have to install it. For example, on Debian Squeeze:

sudo aptitude install sharutils

將為您獲取相關的可執行文件.這是我經歷的過程.首先創建并編譯你的 C 程序 hello.c:

will get the relevant executables for you. Here's the process I went through. First create and compile your C program hello.c:

pax> cat hello.c

#include <stdio.h>
int main (void) {
    printf ("Hello
");
    return 0;
}

pax> gcc -o hello hello.c

然后創建一個shell腳本testEmbed.sh,它會自己解碼:

Then create a shell script testEmbed.sh, which will decode itself:

pax> cat testEmbed.sh

#!/bin/bash
rm -f hello
uudecode $0
./hello
rm -f hello
exit

第一個 rm 語句表明 hello 可執行文件是由該腳本重新創建的,而不是在您的編譯過程中徘徊.由于您還需要文件中的有效負載,請將編碼的可執行文件附加到文件的末尾:

The first rm statement demonstrates that the hello executable is being created anew by this script, not left hanging around from your compilation. Since you need the payload in the file as well, attach the encoded executable to the end of it:

pax> uuencode hello hello >>testEmbed.sh

之后,當您執行腳本 testEmbed.sh 時,它會提取可執行文件并運行它.

Afterwards, when you execute the script testEmbed.sh, it extracts the executable and runs it.

之所以如此,是因為 uudecode 在其輸入(beginend)中查找由 uuencode,所以它只嘗試解碼編碼的程序,而不是整個腳本:

The reason this works is because uudecode looks for certain marker lines in its input (begin and end) which are put there by uuencode, so it only tries to decode the encoded program, not the entire script:

pax> cat testEmbed.sh

#!/bin/bash
rm -f hello
uudecode $0
./hello
rm -f hello
exit

begin 755 hello
M?T5,1@$!`0````````````(``P`!````$(,$"#0```#`!@```````#0`(``'
M`"@`'@`;``8````T````-(`$"#2`!`C@````X`````4````$`````P```!0!
: : :
M:&%N9&QE`%]?1%1/4E]%3D1?7P!?7VQI8F-?8W-U7VEN:70`7U]B<W-?<W1A
M<G0`7V5N9`!P=71S0$!'3$E"0UR+C``7V5D871A`%]?:38X-BYG971?<&-?
4=&AU;FLN8G@`;6%I;@!?:6YI=```
`
end

您可能還應該擔心其他一些事情,例如您的程序可能需要目標系統上不存在的共享庫,但上述過程基本上就是您所需要的.

There are other things you should probably worry about, such as the possibility that your program may require shared libraries that don't exist on the target system, but the process above is basically what you need.

JAR 文件的過程非常相似,只是運行它的方式不同.它仍然是一個文件,但您需要替換該行:

The process for a JAR file is very similar, except that the way you run it is different. It's still a single file but you need to replace the line:

./hello

帶有能夠運行 JAR 文件的東西,例如:

with something capable of running JAR files, such as:

java -jar hello.jar

這篇關于在 shell 腳本中嵌入可執行二進制文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 耐磨陶瓷管道_除渣器厂家-淄博浩瀚陶瓷科技有限公司 | 磁力抛光机_磁力研磨机_磁力去毛刺机-冠古设备厂家|维修|租赁【官网】 | 龙门加工中心-数控龙门加工中心厂家价格-山东海特数控机床有限公司_龙门加工中心-数控龙门加工中心厂家价格-山东海特数控机床有限公司 | 吊篮式|移动式冷热冲击试验箱-二槽冷热冲击试验箱-广东科宝 | 双相钢_双相不锈钢_双相钢圆钢棒_双相不锈钢报价「海新双相钢」 双能x射线骨密度检测仪_dxa骨密度仪_双能x线骨密度仪_品牌厂家【品源医疗】 | 浙江美尔凯特智能厨卫股份有限公司| 伟秀电气有限公司-10kv高低压开关柜-高低压配电柜-中置柜-充气柜-欧式箱变-高压真空断路器厂家 | 环比机械| 自动售货机_无人售货机_专业的自动售货机运营商_免费投放售货机-广州富宏主官网 | 风信子发稿-专注为企业提供全球新闻稿发布服务 | 断桥铝破碎机_发动机破碎机_杂铝破碎机厂家价格-皓星机械 | 知企服务-企业综合服务(ZiKeys.com)-品优低价、种类齐全、过程管理透明、速度快捷高效、放心服务,知企专家! | 贵州水玻璃_-贵阳花溪闽兴水玻璃厂 | 共享雨伞_共享童车_共享轮椅_共享陪护床-共享产品的领先者_有伞科技 | 土壤养分检测仪_肥料养分检测仪_土壤水分检测仪-山东莱恩德仪器 大型多片锯,圆木多片锯,方木多片锯,板材多片锯-祥富机械有限公司 | 深圳展厅设计_企业展馆设计_展厅设计公司_数字展厅设计_深圳百艺堂 | 茶楼装修设计_茶馆室内设计效果图_云臻轩茶楼装饰公司 | 环讯传媒,永康网络公司,永康网站建设,永康小程序开发制作,永康网站制作,武义网页设计,金华地区网站SEO优化推广 - 永康市环讯电子商务有限公司 | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | 点胶机_点胶阀_自动点胶机_智能点胶机_喷胶机_点胶机厂家【欧力克斯】 | 喷播机厂家_二手喷播机租赁_水泥浆洒布机-河南青山绿水机电设备有限公司 | 废气处理设备-工业除尘器-RTO-RCO-蓄热式焚烧炉厂家-江苏天达环保设备有限公司 | 深圳高新投三江工业消防解决方案提供厂家_服务商_园区智慧消防_储能消防解决方案服务商_高新投三江 | 「钾冰晶石」氟铝酸钾_冰晶石_氟铝酸钠「价格用途」-亚铝氟化物厂家 | 标准光源箱|对色灯箱|色差仪|光泽度仪|涂层测厚仪_HRC大品牌生产厂家 | 999范文网_优质范文下载写作帮手| 压砖机_电动螺旋压力机_粉末成型压力机_郑州华隆机械tel_0371-60121717 | 六维力传感器_六分量力传感器_模腔压力传感器-南京数智微传感科技有限公司 | 学习虾-免费的学习资料下载平台 雪花制冰机(实验室雪花制冰机)百科 | 【MBA备考网】-2024年工商管理硕士MBA院校/报考条件/培训/考试科目/提前面试/考试/学费-MBA备考网 | 广州网站建设_小程序开发_番禺网站建设_佛山网站建设_粤联网络 | 礼至家居-全屋定制家具_一站式全屋整装_免费量房设计报价 | 上海橡胶接头_弹簧减震器_金属软接头厂家-上海淞江集团 | 空气净化器租赁,空气净化器出租,全国直租_奥司汀净化器租赁 | 活动策划,舞台搭建,活动策划公司-首选美湖上海活动策划公司 | 净水器代理,净水器招商,净水器加盟-FineSky德国法兹全屋净水 | 粤丰硕水性环氧地坪漆-防静电自流平厂家-环保地坪涂料代理 | 桑茶-七彩贝壳桑叶茶 长寿茶| 阿尔法-MDR2000无转子硫化仪-STM566 SATRA拉力试验机-青岛阿尔法仪器有限公司 | 绿萝净除甲醛|深圳除甲醛公司|测甲醛怎么收费|培训机构|电影院|办公室|车内|室内除甲醛案例|原理|方法|价格立马咨询 | 液晶拼接屏厂家_拼接屏品牌_拼接屏价格_监控大屏—北京维康 |