到目前为止,我已经能够使用ssh创建一个小的脚本,并希望将单个命令传递给我正在使用的dd-wrt路由器。现在已经完成了这一步,我希望通过ssh登录多次传递同一命令,而不是仅从文本文件传递一次。
完成此操作的另一种方法是创建一个循环,然后一次又一次地传递命令。我将不得不使用一个变量,因为文本文件中命令的数据已更改。
这是我到目前为止所拥有的
#!/bin/expect -f
设置密码
spawn ssh -l root x.x.x.x -p "command"
expect "*password:*"
send -- "$password\r"
send -- "\r"
从我看到的情况来看,创建循环是最简单的方法,但是我可能错了。 注意,我要传递的“命令和变量”位于单独的文本文件中,并且它需要读取/采用每一行并将每一行插入循环中。除非有一种方法可以一次全部发送出去。
#!/bin/expect -f
设置密码
spawn ssh -l root x.x.x.x -p "command Variable" <-- Command to be passed through
expect "*password:*"
send -- "$password\r"
send -- "\r"
每次在文本文件中都是相同的命令,只有变量会更改。
test.txt
command xxxxxxx
command xxxxxxx
command xxxxxxx
command xxxxxxx
谢谢
最佳答案
我认为您应该做这样的事情。
start.sh
#!/bin/bash
password="your_password"
cat test.txt|while read line
do
for i in $line
do
ssh.exp $i $password
done
done
ssh.exp
#!/usr/bin/expect
set command [lrange $argv 0 0]
set password [lrange $argv 1 1]
spawn ssh -l root x.x.x.x -p "$command"
expect "*password:*"
send -- "$password\r"
send -- "\r"
并带有命令列表的test.txt。每个都在不同的行上。
关于bash - 如何使用文本文件中的变量通过ssh将命令通过ssh传递到dd-wrt?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15812818/