[MacOS]shell执行自动输入密码之<<EOF(重定向)方法

2017年12月15日 其他资源 12条评论 阅读3219次

[MacOS]shell执行自动输入密码之<<EOF(重定向)方法


MacOS下,很多时候经常为提高效率,自己写一些自动化执行的脚本。
自己写一些脚本把流程式的工作都做了,确实再效率上提高了不少,但经常下面一个场景困惑我。

很多操作需要管理员身份的权限,sudo类似的。
一旦出现shell过程中出现让输入密码的情况,就直接把整个批量的shell处理流给终端了,并不符合我们期望的无人值守的结果。

因此,我们希望shell命令在执行处理流时能自动输入密码。

自动输入密码有多张方式,expect和<<(重定向)方式。expect需要Mac机器上额外安装expect的软件支持包,我没有采用,本文主要介绍的是<<EOF(重定向)方式。


<<重定向的最简单基本使用如下。

 EOF只是一个分界符,当然也可以用abcde替换。
 当shell遇到<<时,它知道下一个词是一个分界符。在该分界符以后的内容都被当作输入,直到shell又看到该分界符(结束分解符必须位于单独的一行)。
 此分界符可以是所定义的任何字符串,其实,不一定要用EOF,只要是“内容段”中没有出现的字符串,都可以用来替代EOF,完全可以换成abcde之类的字符串,只是一个起始和结束的标志罢了。
 EOF分解符一起一后配对出现,中间的内容将以标准输入的形式输入到”交互式程序“。


示例:

sudo chmod +x jileniao.net_shell_auto_password_test.sh<<EOF
jileniao.net_mac_admin_password
EOF



文字描述一下就是,在需要输入密码的命令行后面增加<<EOF
接下来一行,就是密码(jileniao.net_mac_admin_password)
在换一行,EOF结束

其他都是正常的了。

这样写,固然使用没有问题,但隐约中还是感觉不舒服,毕竟是密码就以明文的形式直接暴露在shell文件中了,如果涉及登录远程服务器之类的操作,远程服务器的密码也就直接泄露了。

很容易想到一个改良方案,就是把我们的密码在shell执行时,加入到参数中。如,

./jileniao.net_shell_auto_password_test.sh jileniao.net_mac_admin_password



然后shell文件也改造一下,开始时接收一下执行参数,并保存到变量中。

PASSWORD=$1
if [ ! -n "$PASSWORD" ]; then
  echo "PASSWORD is NULL"
  exit 1
fi
sudo chmod +x jileniao.net_shell_auto_password_test.sh<<EOF
"$PASSWORD"
EOF




这样的结果,貌似也很难让人满意,正常在终端中系统提示我们输入密码时,我们输入的密码都是不可见的,连星号*都不会出现在屏幕上,但现在密码虽然没有在shell文件中明文暴露,确在命令行终端直接显示出来了,技术怎么可以止步,在MacOS中,我们知道有个地方是存放用户密码等凭证的。

这时大家知道新的改良思路了吧,我们可以把密码放到系统的钥匙串访问(KeyChain),然后在shell中调用,听起来挺不错的。不过这个方法还是在理论上可行,实际还没操作过,以后可以实践操练一下shell中读取钥匙串访问(KeyChain)中存储的密码。


分享本文至:

WRITTEN BY

avatar
本文标签:shellmac
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

12 条评论

  1. <a href="http://www.facenextday.com/zx-flux-triple-noir-sklep-butya">zx flux triple noir sklep</a> <a href="http://www.mellowsblues.com/nike-blazer-damenn-olive-obuvd">nike blazer damenn olive</a> <a href="http://www.stackdotnet.com/nike-kd-9-gr%C3%A5-sort-scarpec">nike kd 9 gr氓 sort</a> <a href="http://www.donatelia.com/nike-free-3.0-focfly-so-hvit-svart-schuhef">nike free 3.0 focfly so hvit svart</a> <a href="http://www.jafcosupply.com/a-buon-mercato-femminile-nike-shox-free-shipping-scarpef">a buon mercato femminile nike shox free shipping</a> <a href="http://www.kanaldtvizle.com/nike-air-max-tn-brun-gr%C3%A5-scarpeg">nike air max tn brun gr氓</a>
    barrettmark http://www.barrettmark.com/

  2. avatar eiconstrutora

    <a href="http://www.comsoftdesign.com/nike-air-max-97-s%C3%B8lv-bullet-2017-location-schuheg">nike air max 97 s酶lv bullet 2017 location</a> <a href="http://www.eadscs.com/nike-hyperdunk-2013-tb-hommes-basketbtout-chaussures-butye">nike hyperdunk 2013 tb hommes basketbtout chaussures</a> <a href="http://www.exechars.com/nike-zoom-tout-out-flyknit-jaune-rouge-butya">nike zoom tout out flyknit jaune rouge</a> <a href="http://www.finkoin.com/a-buon-mercato-nike-roshe-run-mesh-obuvb">a buon mercato nike roshe run mesh</a> <a href="http://www.alyanskapi.com/adidas-protator-first-model-scarpec">adidas protator first model</a> <a href="http://www.asupsllc.com/nike-kobe-svart-gr%C3%A5-schuhef">nike kobe svart gr氓</a>
    eiconstrutora http://www.eiconstrutora.com/

  3. avatar karbasioun

    <a href="http://www.onlype.com/ray-ban-rb4105-50-folding-wayfarer-tortoise-sunglassesc">ray ban rb4105 50 folding wayfarer tortoise</a> <a href="http://www.thepoolfloats.com/adidas-originals-tubular-shadow-knit-w-skox">adidas originals tubular shadow knit w</a> <a href="http://www.xisymbols.com/nike-dunk-de-la-soul-low-guide-skoc">nike dunk de la soul low guide</a> <a href="http://www.gazmafsali.com/nike-air-max-command-uomo-41-skob">nike air max command uomo 41</a> <a href="http://www.vnptbiz.com/adidas-superstar-2-light-blau-shoesv">adidas superstar 2 light blau</a> <a href="http://www.urevomedia.com/nike-air-max-tn-ultra-kvinners-bl%C3%A5-runninga">nike air max tn ultra kvinners bl氓</a>
    karbasioun http://www.karbasioun.com/

  4. avatar mesmertronic

    <a href="http://www.myfraimz.com/nike-roshe-all-black-nike-running-trainersa">nike roshe all black nike running</a> <a href="http://www.winnercells.com/nike-huarache-utility-gr%C3%A5-oransje-scarpec">nike huarache utility gr氓 oransje</a> <a href="http://www.atppest.com/air-max-1-zebra-bleu-uk-driver-kurpesd">air max 1 zebra bleu uk driver</a> <a href="http://www.windriftbar.com/ray-ban-aviator-sunglasses-measurements-90s-sunglassesg">ray ban aviator sunglasses measurements 90s</a> <a href="http://www.kidtokidboise.com/youth-nike-buffalo-bills-95-kyle-williams-game-royal-blue-team-color-nfl-jersey-nflr">youth nike buffalo bills 95 kyle williams game royal blue team color nfl jersey</a> <a href="http://www.mesmertronic.com/nike-flyknit-lunar-3-flipkart-quit-skoa">nike flyknit lunar 3 flipkart quit</a>
    mesmertronic http://www.mesmertronic.com/

  5. avatar saskostomy

    <a href="http://www.cremontana.com/air-jordan-30-1000-dollar-runninge">air jordan 30 1000 dollar</a> <a href="http://www.holeninegolf.com/nike-zoom-vomero-12-silber-gr%C3%BCn-schuhea">nike zoom vomero 12 silber gr眉n</a> <a href="http://www.boobseze.com/nike-air-huarache-iii-sort-lilla-scarpeb">nike air huarache iii sort lilla</a> <a href="http://www.divyangshop.com/nike-air-max-95-green-spain-shoeso">nike air max 95 green spain</a> <a href="http://www.caveduperreux.com/nike-kobe-10-sort-lyser%C3%B8d-kurpesc">nike kobe 10 sort lyser酶d</a> <a href="http://www.fabloomsilk.com/infant-jordan-shoes-size-2c-zapatost">infant jordan shoes size 2c</a>
    saskostomy http://www.saskostomy.com/

  6. avatar genesispre

    <a href="http://www.pelumasvagina.com/nike-air-max-2017-lilla-gull-butyd">nike air max 2017 lilla gull</a> <a href="http://www.recommenet.com/under-armour-curry-one-or-rouge-kurpesc">under armour curry one or rouge</a> <a href="http://www.weissewelt.com/off-blanco-polo-shirt-polow">off blanco polo shirt</a> <a href="http://www.wileyhyundai.com/adidas-john-wtutti-verde-butye">adidas john wtutti verde</a> <a href="http://www.ariehonders.com/ralph-lauren-match-polo-sort-fl%C3%A5den-polou">ralph lauren match polo sort fl氓den</a> <a href="http://www.instabitgram.com/new-balance-360-caqui-chaussuresf">new balance 360 caqui</a>
    genesispre http://www.genesispre.com/

  7. avatar 6464l4ua

    <a href="http://www.sjkrsnkj.com/nike-lunarglide-6-negro-and-amarillo-nike-butya">nike lunarglide 6 negro and amarillo nike</a> <a href="http://www.uw756gei.com/air-jordan-after-game-i-bl%C3%A5-r%C3%B8d-obuvd">air jordan after game i bl氓 r酶d</a> <a href="http://www.kfpcbsi0.com/hombres-asics-gel-hyper-33-plata-negro-scarpec">hombres asics gel hyper 33 plata negro</a> <a href="http://www.gojfx342.com/nike-shox-hombres-barato-schuhef">nike shox hombres barato</a> <a href="http://www.6k6gzmtk.com/nike-lunarepic-low-flyknit-2-grau-scarpef">nike lunarepic low flyknit 2 grau</a> <a href="http://www.4o249gfo.com/air-jordan-5-raging-bull-3m-nero-varsity-rosso-scarpeg">air jordan 5 raging bull 3m nero varsity rosso</a>
    6464l4ua http://www.6464l4ua.com/

  8. avatar betascgx

    <a href="http://www.emanmoon.com/nike-flyknit-lunar-3-hyper-jade-skos">nike flyknit lunar 3 hyper jade</a> <a href="http://www.mysiatravel.com/limited-benjarvus-greenellis-jersey-cincinnati-bengals-42-home-black-nflg">limited benjarvus greenellis jersey cincinnati bengals 42 home black</a> <a href="http://www.pandoraboxhn.com/bon-march%C3%A9-nike-sb-blazer-high-hommes-or-skoe">bon march茅 nike sb blazer high hommes or</a> <a href="http://www.yourctt.com/polo-ralph-lauren-cashmere-blend-scarf-scarfr">polo ralph lauren cashmere blend scarf</a> <a href="http://www.coccodesigns.com/air-max-light-release-sneakerb">air max light release</a> <a href="http://www.lucasdow.com/billig-adidas-superstar-kvinners-oransje-shoesd">billig adidas superstar kvinners oransje</a>
    betascgx http://www.betascgx.com/

  9. avatar monolitcenter

    <a href="http://www.artikhazirsin.com/adidas-zx-flux-y3-shoesa">adidas zx flux y3</a> <a href="http://www.audioalpaca.com/pittsburgh-steelers-sideline-hat-hatr">pittsburgh steelers sideline hat</a> <a href="http://www.selvasurf.com/bon-march%C3%A9-nike-archive-75-femmes-noir-trainersa">bon march茅 nike archive 75 femmes noir</a> <a href="http://www.coldditch.com/nike-huarache-ultra-black-and-red-wine-skoe">nike huarache ultra black and red wine</a> <a href="http://www.grassmannword.com/black-mens-nike-toki-slip-txt-skoc">black mens nike toki slip txt</a> <a href="http://www.mjchronicles.com/barato-nike-roshe-yeezy-boost-hombres-plata-shoesc">barato nike roshe yeezy boost hombres plata</a>
    monolitcenter http://www.monolitcenter.com/

  10. avatar juliemobley

    <a href="http://www.aprancheta.com/tory-burch-brown-and-black-riding-boots-toryburchr">tory burch brown and black riding boots</a> <a href="http://www.getleanlearn.com/nike-air-max-2016-menns-bl%C3%A5-runningc">nike air max 2016 menns bl氓</a> <a href="http://www.hbshbuick.com/mackage-tika-leather-jacket-mackager">mackage tika leather jacket</a> <a href="http://www.hvactampafl.com/michael-kors-wallets-harper-obituary-texas-mkc">michael kors wallets harper obituary texas</a> <a href="http://www.zinmanlaw.com/moncler-outlet-online-shop-europe-news-monclerb">moncler outlet online shop europe news</a> <a href="http://www.cohenhypnosis.com/ray-ban-jackie-ohh-tortoise-shell-sunglassesr">ray ban jackie ohh tortoise shell</a>
    [url=http://www.juliemobley.com/]juliemobley[/url]

  11. avatar sidestoreid

    <a href="http://www.domcilios.com/nike-air-force-1-puerto-rico-sko-shoesv">nike air force 1 puerto rico sko</a> <a href="http://www.zgzllc.com/coach-woven-hobo-tour-coachc">coach woven hobo tour</a> <a href="http://www.flibroactiv.com/bon-march%C3%A9-adidas-crazylight-boost-hommes-bleu-shoesc">bon march茅 adidas crazylight boost hommes bleu</a> <a href="http://www.tustinvillage.com/adidas-response-tr-boost-skos">adidas response tr boost</a> <a href="http://www.erickasouza.com/authentic-ferragamo-belt-beltr">authentic ferragamo belt</a> <a href="http://www.killersandbox.com/mackage-coat-sale-online-edition-mackagee">mackage coat sale online edition</a>
    sidestoreid http://www.sidestoreid.com/

  12. avatar 八角网赚站

    路过,支持一下

欢迎留言




用户登录

sitemap