簡體   English   中英

自動將輸入提供給 Linux 命令行

[英]Automatically feed input to Linux command line

如果我有一個提示輸入 2 個或更多輸入的 linux 命令,如何通過在命令行中定義將這些輸入傳遞給提示? 是否可以在命令后面添加一些內容來執行此操作?

在下面的示例中,如何運行命令並將用戶名和密碼傳遞給它,而無需在系統要求時輸入它們?

要求輸入用戶名和密碼的示例命令

git clone https://github.com/username/repo.git 

只是一個例子,請不要建議使用 ssh 而不是 http 進行 git clone,或者在命令中公開密碼是不安全的

“期望”包就是針對這種情況而構建的。 如果安裝它,請簽出“ autoexpect”。

也許這

{
  echo user
  echo pass
} | git clone https://github.com/username/repo.git

ref

根據您要執行此操作的原因,您可能想探索使用不帶密碼的github存儲庫的方法:

https://help.github.com/articles/managing-deploy-keys

我想要賞金,我不能關閉,因為答案在不同的 SE 上。

引用超級用戶的答案

使用專為滿足您的需求而設計的程序expect ,這些需求在我們這個時代之前已經被用戶感受到,因為它的手冊頁指出:

連接到另一個網絡或 BBS(例如,MCI Mail、 CompuServe )。

Compuserve,是在法國大革命之前還是之后? 反正。

spawn "git clone ssh://git@domain.com/myproject.git"
expect "Enter passphrase for key..." { send "myPassword\r" }
read -p "enter..."

也許最后一行是多余的。

Ceterum ceneo :不要那樣做。

,您可以使用script

script將生成一個tty,您可以在下使用coproc冷交互。 這可以在不需要expect情況下完成:

 NAME script - make typescript of terminal session SYNOPSIS script [options] [file] DESCRIPTION script makes a typescript of everything on your terminal session. The terminal data are stored in raw form to the log file and information about timing to another (optional) structured log file. The timing log file is necessary to replay the session later by scriptreplay(1) and to store additional information about the session.

演示

coproc script -f /dev/null

可以返回:

[1] 2103233

這將啟動一個 subshel​​,您可以與之交互,

while read -t 0 -u ${COPROC[0]};do
    read -t .02 -ru ${COPROC[0]} line
    echo "$line"
done

應該產生類似的東西:

Script started, output log file is 'typescript'.
user@hostname:~$

然后

echo >&${COPROC[1]} ssh user@target
while [ ! "$line" ] || [ "${line//*password*}" ];do
    read -t .02 -ru ${COPROC[0]} line
    echo "$line"
done
echo >&${COPROC[1]} TheStrongPasswordInClear
while [ ! "$line" ] || [ "${line//*\$*}" ];do
        read -t .02 -ru ${COPROC[0]} line
    echo "$line"
done

在那里,你可以看到很多像

ssh user@target
user@target's password:

Linux target 5.00.0-0-amd64 # Distro ...
issue.net content...
Last login: Sun Dec 19 12:23:37 2021 from somewhere

user@target:~$ 

從那里,您可以:

getres() {
    while read -t 0 -u ${COPROC[0]};do
        read -t .02 -ru ${COPROC[0]} line
        echo "$line"
    done
}

echo >&${COPROC[1]} uptime
getres
uptime
12:38:38 up 21 days,  3:52, 122 users,  load average: 3.20, 2.19, 1.59

最后

echo >&${COPROC[1]} exit  
getres 
exit
logout
Connection to target closed.

注意:Git 要求您使用 git 令牌而不是密碼。

empty 是一個實用程序,它提供了一個簡單的界面來在偽終端會話下執行和/或與進程交互。

該工具在編寫 shell 腳本時絕對有用,這些腳本通過 sed 與 telnet 或 FTP 等交互式程序進行通信。

在某些情況下,空可以替代 TCL/expect 或其他類似的編程工具。

將以下代碼復制到您的腳本文件中。

通過從命令行提供以下參數(按照您的要求)使其可執行並運行腳本:

  • Git repo URI(作為第一個參數)
  • Git 用戶名(作為第二個參數)
  • Git 用戶令牌(作為第三個參數)

例子:

假設您的腳本文件名是: git_clone.sh

./git_clone.sh <GIT_REPO_URI> <GIT_USERNAME> <GIT_USER_TOKEN>

#!/bin/bash
git_repo="${1}"
git_user="${2}"
git_token="${3}"

empty -f -i in.fifo -o out.fifo -pempty.pid  git clone  

empty -w -i out.fifo -o in.fifo sername  "${git_user}\n"
empty -w -i out.fifo -o in.fifo assword  "${git_token}\n"

exit 0

有關empty更多詳細信息,請參閱其手冊: http : //manpages.ubuntu.com/manpages/bionic/man1/empty.1.html

不知道你有沒有試過這個。 但這在我們執行任何 sudo cmd 並需要輸入密碼的情況下很有用。 你可以在你的情況下試試這個。

但我認為你至少必須提供一個用戶名。

例如 :

sudo -S <<< "password" sudo yum -y update 

在你的情況下,它應該是這樣的:

sudo -S <<< {password} git clone https://github.com/{username}/repo.git

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM