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(使用線程逐塊處理文件)
主站蜘蛛池模板: 礼仪庆典公司,礼仪策划公司,庆典公司,演出公司,演艺公司,年会酒会,生日寿宴,动工仪式,开工仪式,奠基典礼,商务会议,竣工落成,乔迁揭牌,签约启动-东莞市开门红文化传媒有限公司 | 标准件-非标紧固件-不锈钢螺栓-非标不锈钢螺丝-非标螺母厂家-三角牙锁紧自攻-南京宝宇标准件有限公司 | 中高频感应加热设备|高频淬火设备|超音频感应加热电源|不锈钢管光亮退火机|真空管烤消设备 - 郑州蓝硕工业炉设备有限公司 | 臭氧实验装置_实验室臭氧发生器-北京同林臭氧装置网 | 青岛成人高考_山东成考报名网 | 阻垢剂,反渗透阻垢剂,缓蚀阻垢剂-山东普尼奥水处理科技有限公司 真空粉体取样阀,电动楔式闸阀,电动针型阀-耐苛尔(上海)自动化仪表有限公司 | 粉末包装机,拆包机厂家,价格-上海强牛包装机械设备有限公司 | 一点车讯-汽车网站,每天一点最新车讯!| 防爆电机生产厂家,YBK3电动机,YBX3系列防爆电机,YBX4节防爆电机--河南省南洋防爆电机有限公司 | 数显恒温培养摇床-卧式/台式恒温培养摇床|朗越仪器 | 铁艺,仿竹,竹节,护栏,围栏,篱笆,栅栏,栏杆,护栏网,网围栏,厂家 - 河北稳重金属丝网制品有限公司 山东太阳能路灯厂家-庭院灯生产厂家-济南晟启灯饰有限公司 | 量子管通环-自清洗过滤器-全自动反冲洗过滤器-北京罗伦过滤技术集团有限公司 | 航空连接器,航空插头,航空插座,航空接插件,航插_深圳鸿万科 | 直读光谱仪,光谱分析仪,手持式光谱仪,碳硫分析仪,创想仪器官网 | 酒店品牌设计-酒店vi设计-酒店标识设计【国际级】VI策划公司 | 成都APP开发-成都App定制-成都app开发公司-【未来久】 | 扬子叉车厂家_升降平台_电动搬运车|堆高车-扬子仓储叉车官网 | 中空玻璃生产线,玻璃加工设备,全自动封胶线,铝条折弯机,双组份打胶机,丁基胶/卧式/立式全自动涂布机,玻璃设备-山东昌盛数控设备有限公司 | 编织人生 - 权威手工编织网站,编织爱好者学习毛衣编织的门户网站,织毛衣就上编织人生网-编织人生 | 盘扣式脚手架-附着式升降脚手架-移动脚手架,专ye承包服务商 - 苏州安踏脚手架工程有限公司 | 海日牌清洗剂-打造带电清洗剂、工业清洗剂等清洗剂国内一线品牌 海外整合营销-独立站营销-社交媒体运营_广州甲壳虫跨境网络服务 | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | 电脑知识|软件|系统|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网 | 手持式浮游菌采样器-全排二级生物安全柜-浙江孚夏医疗科技有限公司 | 304不锈钢无缝管_不锈钢管厂家 - 隆达钢业集团有限公司 | 制丸机,小型中药制丸机,全自动制丸机价格-甘肃恒跃制药设备有限公司 | Magnescale探规,Magnescale磁栅尺,Magnescale传感器,Magnescale测厚仪,Mitutoyo光栅尺,笔式位移传感器-苏州连达精密量仪有限公司 | 智能家居全屋智能系统多少钱一套-小米全套价格、装修方案 | 布袋除尘器-单机除尘器-脉冲除尘器-泊头市兴天环保设备有限公司 布袋除尘器|除尘器设备|除尘布袋|除尘设备_诺和环保设备 | 电动打包机_气动打包机_钢带捆扎机_废纸打包机_手动捆扎机 | 红立方品牌应急包/急救包加盟,小成本好项目代理_应急/消防/户外用品加盟_应急好项目加盟_新奇特项目招商 - 中红方宁(北京) 供应链有限公司 | 医用酒精_84消毒液_碘伏消毒液等医用消毒液-漓峰消毒官网 | 电销卡_稳定企业大语音卡-归属地可选-世纪通信 | 盐水蒸发器,水洗盐设备,冷凝结晶切片机,转鼓切片机,絮凝剂加药系统-无锡瑞司恩机械有限公司 | 车充外壳,车载充电器外壳,车载点烟器外壳,点烟器连接头,旅行充充电器外壳,手机充电器外壳,深圳市华科达塑胶五金有限公司 | 东莞韩创-专业绝缘骨架|马达塑胶零件|塑胶电机配件|塑封电机骨架厂家 | 动力配电箱-不锈钢配电箱-高压开关柜-重庆宇轩机电设备有限公司 聚天冬氨酸,亚氨基二琥珀酸四钠,PASP,IDS - 远联化工 | 专业的压球机生产线及解决方案厂家-河南腾达机械厂 | 品牌设计_VI设计_电影海报设计_包装设计_LOGO设计-Bacross新越品牌顾问 | 创富网-B2B网站|供求信息网|b2b平台|专业电子商务网站 | 新疆系统集成_新疆系统集成公司_系统集成项目-新疆利成科技 |