- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我不太擅长 bash,我尝试编写一个脚本来使用 openSSH 连接到我的所有交换机,以便进行一些配置。
我创建了一个包含所有 25 个交换机的数组,然后使用循环打开与每个交换机的 SSH 连接。
由于我使用的是 Windows 并使用 bash,因此我刚刚安装了 Cygwin。
但是,我必须使用 Expect 并以纯文本形式编写密码,因为交换机非常差,这对我来说是最好的方法(我不会手动将 RSA key 放在每个交换机上,因为这会花费我与在每个交换机上手动编写配置一样多的时间)。
我使用 shebang #!/usr/bin/expect -f 使 bash 识别 Expect。当我这样做时,期望语法(spawn、expect、interact)完美地工作,但我的数组不起作用。
我收到以下错误消息:
extra characters after close-quote while executing "arrayname=("172.21.21.20" "172.20.55.55" ... "
当我更改shebang并使用#!/bin/bash时,不再找到expect:
./stationsnmp.sh: line 20: spawn : command not found couldn't read ./stationsnmp.sh: line 24: send : command not found couldn't read file "assword": no such file or directory ./stationsnmp.sh: line 27: send : command not found ./stationsnmp.sh: line 28: interact : command not found
我真的不是 bash 专业人士,这说明我无法解决这个小问题...欢迎提供一些帮助。
编辑:下面是我的代码的一部分
#!/bin/bash
switch=("172.20.0.229" "172.20.0.232" "172.20.0.233" "172.21.0.15" "172.21.0.16" "172.21.2.1" "172.20.2.250" "172.21.3.1" "172.20.3.250" "172.21.4.1" "172.20.4.250" "172.21.6.1" "172.20.6.250" "172.21.7.1" "172.20.7.250" "172.21.8.1" "172.20.8.250" "172.20.9.250" "172.21.9.1" "172.21.10.1" "172.20.10.250" "172.21.11.1" "172.20.11.250" "172.21.12.1" "172.21.12.250")
nmb=`echo ${#switch[@]}`
set timeout 3
for ((ii=0; ii<=$nmb; ii++))
#for ii in {0..${#switch[@]}}
do
if [ ${switch[$ii]:5:1} -eq 1 ]
then
ipdc=`echo ${switch[ii]} | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.'`"10"
spawn ssh admin@switch[$ii]
expect "*assword*"
send "PASS\r"
interact
exit
fi
done
最佳答案
您正在混合 bash
和 expect
,它们是两种完全不同的语言。您可能希望有一个具有适当选项处理功能的 bash 包装器脚本(请参阅 getopts),该脚本获取 IP 地址列表并为传递到 bash 包装器的每个 IP 地址执行您的期望脚本。如果您的 expect
脚本很小,您可能希望将其嵌入到您的 shell 脚本中,而不是将其放在单独的文件中。
编辑:
#!/bin/bash
switches=("172.20.0.229" "172.20.0.232")
for ip in "${switches[@]}"; do
expect "${ip}" <<-'EOT'
set host [lindex $argv 0]
set timeout 3
spawn ssh -l admin $host
expect "*assword*"
send "PASS\r"
interact
exit
EOT
done
关于linux - Shebang 导致脚本失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22779464/
我试图了解 perl 如何处理 shebang 行。 我曾经认为命令行“命令位置”中提到的任何解释器都会优先于 shebang 行中提到的解释器。例如,如果一个名为 demo 的可执行脚本看起来像这样
我无法获取 Gforth当我有 shebang 时运行在脚本的顶部,例如: #!/usr/bin/gforth : pinoke ." I'm a real boy!!! " pinoke Unix
考虑以下代码: #!/usr/bin/env python import sys print "Hello! I've got %r as input." % sys.stdin.read() 这是
我正在尝试在 CentOS 6 中运行 bash 脚本。如果没有 shebang header ,它就无法工作。但是,当我包含 shebang 时,它会显示一条错误消息! 这是一个名为 test.sh
我得到了一个正则表达式,它使用这个正则表达式代码删除了 bash 中的注释 '/^\#/d' 但它也删除了 #!这不应该是因为它不是评论标签而是 shebang 那么忽略#! 怎么办呢?在正则表达式中
我打算在 php 脚本中添加一个 shebang 行 #!/bin/env php 但我环顾四周,似乎人们并没有这样做。 http://github.com/zurb/foundation/blob/
是Shebang #! ,例如 #!/bin/sh 在 Linux 标准库或任何 The Open Group 标准或其他地方正式标准化的脚本可执行文件之前?如果是,请提供引用资料和详细信息。 注意:
与使用 set 在 shebang 行上传递给脚本的标志有关的行为是否存在差异?内置? 例如: #!/bin/bash -e # do stuff 对比 #!/bin/bash set -e # do
这个问题已经有答案了: Why do people write "#!/usr/bin/env python" on the first line of a Python script? (22 个回
$ cat hello.ml #!/usr/bin/env ocamlrun ocaml let rec main = print_string "Hello World!\n" $ ./hello.
我的电脑上有/bin/bash (3.2) 和/usr/local/bin/bash (4.0)。在 $PATH 环境变量中,bash 4.0 早于 bash 3.2。 在我的 bash 脚本 she
是否可以向文件添加 shebang 行,以便它引用远程计算机上的程序? 例如,我没有在我的本地机器上安装 wolframscript,但我在远程机器上安装了。所以不是 #!/usr/bin/env w
我有一个简单的 cli 将时间戳转换为人类可读的日期时间。出于某种原因,当我尝试调用它时,我改为从 imagemagick 调用 import 命令。 > ts 1400029200000 Versi
要使 Python 脚本在 Linux 和 bash 上可执行,可以使用 shebang #! /usr/bin/env python 作为脚本的第一行。如果这一行恰好以 Windows 风格的换行符
我不太擅长 bash,我尝试编写一个脚本来使用 openSSH 连接到我的所有交换机,以便进行一些配置。 我创建了一个包含所有 25 个交换机的数组,然后使用循环打开与每个交换机的 SSH 连接。 由
我在 django 生产环境中使用 PythonAnyhwere。我有一个应该按计划运行的脚本。 由于我在 virtualenv 中安装了 django,因此脚本开始如下 #!/usr/bin/env
我在 Solaris 环境中运行 Python 脚本时遇到以下问题。 看来我在 shebang 行上做了一些不正确的事情,但我无法判断这是 Python 3 问题还是命令行问题。 但我怀疑它与 she
要使 Python 脚本在 Linux 和 bash 上可执行,可以使用 shebang #! /usr/bin/env python 作为脚本的第一行。如果这一行恰好以 Windows 风格的换行符
我在一台 linux 机器上执行了下面的代码,它执行得很好,但是当我在另一台 linux 机器上执行时,它给出了如下错误 Error:( declaration is not correct --
我目前正在编写一个 awk 脚本,如下所示: #!/usr/bin/awk -f BEGIN { print "Starting extracting data" } { print
我是一名优秀的程序员,十分优秀!