簡體   English   中英

循環遍歷文件並發送超過預期的命令

[英]Loop through file and send commands over expect

我正在嘗試將命令從文件發送到超出預期的設備。 我嘗試從本地機器一次發送一個,但我所有的文件路徑都是相對於本地的,而不是相對於遠程設備的。 我的解決方案是嘗試將文件上傳到設備並從那里加載命令。 當我嘗試加載文件時,我一直遇到權限問題,即使我從設備中讀取文件我也沒有問題讀取它。 該文件每行有 1 個命令。

devicepath=rsync://root@localhost:$PORT_RSYNC/root/var/root/file.txt
/usr/bin/rsync -Pavr $1 $devicepath
    
expect <<- expect_feed
set  send_slow  {1  .001}
spawn ssh -o NoHostAuthenticationForLocalhost=yes -p $PORT_SSH root@localhost
expect -re "password:"
send -s "password\r"
expect -re $PROMPT_ROOT
send -s "chmod 777 /var/root/file.txt\r"
expect -re $PROMPT_ROOT
set f [cat /var/root/file.txt]
set cmds [split [read $f] "\n"]
close $f
foreach line $cmds {
    send -s "$line\r"
    expect -re $PROMPT_ROOT
expect_feed

這產生:

root# cat: /var/root/file.txt: Permission denied

我也試過

set f [open /var/root/file.txt]

...但它給出了同樣的錯誤。

如果你寄過來的文件中包含shell命令,把它當作這樣的,只是source它在遠程主機上

devicepath=rsync://root@localhost:$PORT_RSYNC/root/var/root/file.txt
/usr/bin/rsync -Pavr "$1" "$devicepath"

export PROMPT_ROOT PORT_SSH

expect << 'EXPECT_FEED'
    set send_slow {1  .001}
    spawn ssh -o NoHostAuthenticationForLocalhost=yes -p $env(PORT_SSH) root@localhost
    expect -re "password:"
    send -s "password\r"
    expect -re $env(PROMPT_ROOT)
    send -s ". /var/root/file.txt\r" ;# <<<<
    expect -re $env(PROMPT_ROOT)
    send "exit\r"
    expect eof
EXPECT_FEED

我更喜歡使用引用的heredocs:shell變量可以通過環境傳遞給expect。

我假設 root 的 shell 是一個 POSIX 類型的 shell,其中. 是“源”命令。

謝謝你的好建議。 它是這樣工作的

cmds=$(cat $1)
export cmds

    expect << 'EXPECT_FEED'
        set send_slow {1  .001}
        spawn ssh -o NoHostAuthenticationForLocalhost=yes -p $env(PORT_SSH) root@localhost
        expect -re "password:"
        send -s "password\r"
        expect -re $env(PROMPT_ROOT)
        send -s ">the name of the process accepting commands<\r"
        expect -re $env(PROMPT_'process')
        send -s "$env(cmds)\r"
        expect -re $env(PROMPT_ROOT)
        send "exit\r"
        expect eof
EXPECT_FEED

暫無
暫無

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

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