如何迭代一系列端口号,从expect中捕获工作端口和SSH

时间:2016-10-11 16:01:59

标签: bash expect

目前下面的脚本示例有效,但是我需要遍历8801和8899的端口范围。(必须在expect部分,标准bash for循环在这里不起作用)。

#!/bin/bash
testFunction(){
/usr/bin/expect << EOF
#!/usr/bin/expect

spawn ssh admin@localhost -p 8802
expect "password"

send "password\r"
expect "$ "
interact
EOF
}

2 个答案:

答案 0 :(得分:0)

尝试使用环境变量。它既快又脏,但它有效:

<button id="array" onclick="Randarray()"> Array </button> 

<script>
function Randarray()
{
randarray = new Array(5)
randarray [0] = new Array(3)
randarray [0][0] = "Toyota"
randarray [0][1] = "1998"
randarray [0][2] = "Black"

randarray [1] = new Array(3)
randarray [1][0] = "Ferrari"
randarray [1][1] = "2006"
randarray [1][2] = "Red"

randarray [2] = new Array(3)
randarray [2][0] = "Ferrari"
randarray [2][1] = "2006"
randarray [2][2] = "Red"

randarray [3] = new Array(3)
randarray [3][0] = "Jeep"
randarray [3][1] = "2006"
randarray [3][2] = "Silver"

randarray [4] = new Array(3)
randarray [4][0] = "Mercedes"
randarray [4][1] = "2016"
randarray [4][2] = "White"

randarray [5] = new Array(3)
randarray [5][0] = "BMW"
randarray [5][1] = "2017"
randarray [5][2] = "Black"

function getArray(row,col){
document.arrayForm.myResult.value=randarray[row][col];

}

document.getElementById('array').addEventListener('click', Randarray);

</script>

我还使用了sshpass而不是在expect脚本中使用ssh密码。我用它来测试这个想法。在这种情况下,文件“x”中包含密码(并且具有高度限制性的权限)。

答案 1 :(得分:-1)

您可以使用变量替换HERE doc中的硬编码8802吗?然后你就可以将整个testFunction放在一个循环中,为每次迭代传入一个不同的端口。