AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题

问题[expect](unix)

Martin Hope
Stéphane Chazelas
Asked: 2023-10-29 23:20:48 +0800 CST

为什么 unbuffer -p 会破坏其输入?

  • 23
$ seq 10 | unbuffer -p od -vtc
0000000   1  \n   2  \n   3  \n   4  \n   5  \n   6  \n   7  \n   8  \n

9去了哪里10?

$ printf '\r' | unbuffer -p od -An -w1 -vtc
  \n

为什么\r改为\n?

$ : | unbuffer -p printf '\n' | od -An -w1 -vtc
  \r
  \n
$ unbuffer -p printf '\n' | od -An -w1 -vtc
  \r
      \n

搞什么?

$ printf foo | unbuffer -p cat
$

为什么没有输出(并且延迟一秒)?

$ printf '\1\2\3foo bar\n'  | unbuffer -p od -An -w1 -vtc
$

为什么没有输出?

$ (printf '\23'; seq 10000) | unbuffer -p cat

为什么它挂起并且没有输出?

$ unbuffer -p sleep 10

为什么我看不到我输入的内容(为什么即使我sleep没有阅读它也会被丢弃)?

顺便说一下,还有:

$ echo test | unbuffer -p grep foo && echo found foo
found foo

为什么grep找到foo但没有打印包含它的行?

$ unbuffer -p ls /x 2> /dev/null
ls: cannot access '/x': No such file or directory

为什么错误没有转到/dev/null?

另请参阅取消缓冲将所有字符转换为响铃?

$ echo ${(l[10000][foo])} | unbuffer -p cat | wc -c
4095

那是:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux trixie/sid
Release:        n/a
Codename:       trixie
$ uname -rsm
Linux 6.5.0-3-amd64 x86_64
$ expect -c 'puts "expect [package require Expect] tcl [info patchlevel]"'
expect 5.45.4 tcl 8.6.13
$ /proc/self/exe --version
zsh 5.9 (x86_64-debian-linux-gnu)

在 Ubuntu 22.04 或 FreeBSD 12.4-RELEASE-p5 上也是如此(除了od命令必须在那里进行调整,我得到 2321(那里的所有 BEL 字符)而不是上面的 4095)。

expect
  • 1 个回答
  • 1252 Views
Martin Hope
VBtheHun
Asked: 2022-09-30 05:41:32 +0800 CST

使用 IF 语句在期望中创建分支

  • 1

我正在编写一个expect脚本来处理过多的提示。

bash我正在使用的脚本可以遵循以下两个序列之一:

  1. prompt1 -> prompt2 -> prompt3
  2. prompt1 -> prompt3

也就是说,有时我不明白prompt2。因此,我的问题是:我如何编写一个expect检查 的脚本prompt2,如果它在那里发送 ay并继续进行,否则它只是继续。

编辑:感谢@glenn 的输入和答案!我将尝试使问题更清楚,以防将来对其他人有所帮助。bash我正在处理的特定脚本是从服务器下载一些数据。所以通常,当我执行脚本时会发生什么:

Do you want to download data from *server name*?
y
Do you want to download data from *some directory within server*?
y
...

但是,有时我在执行此脚本时没有连接到服务器。因此,我在脚本中包含了一部分,用于检查我是否已连接到服务器。如果我没有,它会给我另一个提示,如下所示:

Do you want to download data from *server name*?
y
Looks like you are not connected to *server name*! Want to connect now?
y
Do you want to download data from *some directory within server*?
y
...
bash expect
  • 1 个回答
  • 37 Views
Martin Hope
cr001
Asked: 2022-06-02 19:41:18 +0800 CST

期望:如何使用无限期望生成正确关闭bash中的while循环?

  • 0

我有以下脚本:

#!/usr/bin/expect

set timeout 20

set cmd [lrange $argv 1 end]
set password [lindex $argv 0]

eval spawn $cmd

while {1} {
  expect "id_rsa"
  send "$password\r";
}

interact

它打算期待“id_rsa”并在生成的 $cmd 打开时重复输入密码。

现在脚本可以工作,但总是以错误结束

send: spawn id expx not open while executing ...

我想以正确的方式编写我的代码,以便通过检查正确退出while循环,而不是通过expect中的错误捕获,这样错误就不会出现。

提前致谢。

bash expect
  • 1 个回答
  • 185 Views
Martin Hope
Erwann
Asked: 2022-03-10 17:52:29 +0800 CST

如何正确使用 spawn-expect-send 进行“git push”?

  • 0

下面的代码改编自“在 Bash 脚本中使用 Expect 为 SSH 命令提供密码”的解决方案,以便将参数传递给git push. 我没有因为传递错误的 uname+pwd 而遇到任何异常,相反,传递正确的 uname+pwd 并不会真正推动任何事情。如何纠正?

git_push.sh

if (( $# == 2 ))
then
    :
else
    echo "expecting 'username pass', got $@"
    exit 1
fi

user="$1"
pass="$2"
expect - <<EOF
 spawn git push
 expect 'User*'
 send "$user\r"
 expect 'Pass*'
 send "$pass\r"
EOF

终端:

$ [path]/git_push.sh
spawn git push
Username for 'https://github.com': foo
Password for 'https://[email protected]': 

或者(没有通配符):

 spawn git push
 expect "Username for 'https://github.com': "
 send "$user\r"
 expect "Password for 'https://[email protected]': "
 send "$pass\r"
git expect
  • 1 个回答
  • 226 Views
Martin Hope
elbarna
Asked: 2021-12-26 12:19:46 +0800 CST

期望脚本启动 ilo2,工作正常但没有退出

  • 0

这是我启动 ILO2 HP 服务器的脚本。工作正常(服务器电源打开),但在 iLO 控制台电源打开后,我只能手动使用 ctrl+c 退出脚本,因此脚本不是完全非交互式的,如何将正确的 ^D 传递给从控制台退出?

#!/usr/bin/expect -f

set force_conservative 0  ;# set to 1 to force conservative mode even if
              ;# script wasn't run conservatively originally
if {$force_conservative} {
    set send_slow {1 .1}
    proc send {ignore arg} {
        sleep .1
        exp_send -s -- $arg
    }
}

set timeout -1
set send_slow {1 .1}
spawn csh
match_max 100000
expect -exact "% "
sleep .1
send -s -- "ssh hpilo2\r"
expect -exact "ssh hpilo2\r\r
This is a private system. It is to be used solely by authorized users and may be monitored for all lawful purposes. By accessing this system, you are consenting to such monitoring.\r
\rmyuser@hpilo2's password: "
sleep .1
send -s -- "mypass\r"
expect -exact "\r
User:myuser logged-in to hpilo2.(******* / *********)\r
\r
iLO Advanced **** at  Feb 28 2041\r
Server Name: \r
Server Power: Off\r
\r
</>hpiLO-> "
sleep .1
send -s -- "power on\r"
expect -exact "
\r\r
status=0\r
status_tag=COMMAND COMPLETED\r
Sat Dec 6 12:38:17 2031
\r
\r
\r
Server powering on .......\r
\r
\r
\r
</>hpiLO-> "
sleep .1
send -- "^D"
expect eof
expect
  • 1 个回答
  • 68 Views
Martin Hope
was_777
Asked: 2020-02-29 19:55:41 +0800 CST

期望命令:设置主机的多行环境变量

  • 0

所以我在我的环境变量中设置了一个 RSA 令牌。我想在远程机器的环境中设置它。我在下面尝试了这个脚本来设置,但它只设置第一行-----BEGIN OPENSSH PRIVATE KEY-----:

#!/usr/bin/expect -f

spawn ssh -tt ec2-user@$::env(EC2_INSTANCE)

send "export RSA_KEY=$::env(MY_KEY)\r"
expect -re ".*\$"

如何设置整个内容?

shell-script expect
  • 1 个回答
  • 1561 Views
Martin Hope
t013463
Asked: 2019-01-18 06:22:02 +0800 CST

Expect 脚本中的变量文件

  • 0

我正在使用这个期望脚本作为交互式 shell 脚本的应答文件:

#!/usr/bin/expect

spawn ./interactivescriptname
expect "Question 1?"
send "something\r"
expect "Question 2?"
send "yes\r"
expect "Password?"
send "somepassword\r"
interact

以上有效,但我想使用输入文件中的变量。

我的变量文件如下所示:

var1="something"
var2="yes"
var3="somepassword"

通常在 bash 中我会使用 source 命令,但这在预期中不起作用。

有任何想法吗?

expect suse
  • 1 个回答
  • 1328 Views
Martin Hope
KuboMD
Asked: 2018-11-16 05:19:42 +0800 CST

文件权限 - 只允许脚本读取?

  • 0

我有一个以明文形式存储的密码。我只希望我的期望脚本“LoadList”能够读取密码以生成 SFTP 会话。在任何情况下我都希望能够读取密码。这可以通过将脚本分配给用户并仅授予该用户读取权限来以某种方式完成吗?

files expect
  • 1 个回答
  • 63 Views
Martin Hope
KuboMD
Asked: 2018-11-15 10:56:36 +0800 CST

如何避免此错误“无法执行“des”:没有这样的文件或目录”?

  • -1

我正在尝试在 Ubuntu 16.04 上的本 wiki 中概述的Expect 中进行加密。我已经安装了tcllib

root@alarmux:/home/abdmin# apt-get install tcllib
Reading package lists... Done
Building dependency tree       
Reading state information... Done
tcllib is already the newest version (1.17-dfsg-1).
0 upgraded, 0 newly installed, 0 to remove and 77 not upgraded.

但是,当我运行我的脚本时,我得到:

couldn't execute "des": no such file or directory
    while executing
"exec echo "$pd" | des -e -k $key -b > /home/abdmin/$filename"
    (procedure "utility_encrypt" line 3)
    invoked from within
"utility_encrypt pswd encrypted_Pass"
    (file "./tclTest" line 22)

为什么des命令不能执行?

expect tcl
  • 3 个回答
  • 1196 Views
Martin Hope
Duke Dougal
Asked: 2018-08-23 15:49:05 +0800 CST

期望 - 如果命令失败,如何以状态码 1 退出?

  • 3

我的目标是使用期望重命名 sftp 服务器上的文件,如果重命名失败,退出期望返回状态代码 1

我的期望脚本工作正常 - 它成功重命名。

如果重命名失败,我无法弄清楚如何使用状态码退出。

ubuntu@ip-10-0-0-138:~$> cat expect_script.txt
spawn sftp -o "StrictHostKeyChecking no" [email protected]
expect "password:"
send "somesupersecretpassword\n"
expect "sftp>"
send "rename /var/www/static/tmp-93121715.mp4  /var/www/static/91329728.mp4\n"
expect "sftp>"
send "rename /var/www/static/91329728.mp4  /var/www/static/tmp-93121715.mp4\n"
expect "sftp>"
send "bye\n"
expect "#"
exit

这是我运行脚本时的输出:

ubuntu@ip-10-0-0-138:~$> expect -f expect_script.txt
spawn sftp -o StrictHostKeyChecking no [email protected]
[email protected]'s password:
Connected to nginx.localvpc.
sftp> rename /var/www/static/tmp-93121715.mp4  /var/www/static/91329728.mp4
rename /var/www/static/tmp-93121715.mp4  /var/www/static/91329728.mp4
sftp> rename /var/www/static/91329728.mp4  /var/www/static/tmp-93121715.mp4
rename /var/www/static/91329728.mp4  /var/www/static/tmp-93121715.mp4
sftp> bye
bye
ubuntu@ip-10-0-0-138:~$>

如果重命名失败,任何人都可以建议我可以做什么以返回代码零退出?

额外的问题......在我的期望脚本结束时退出的正确方法是什么?是其中之一吗?

exit
close
expect eof

谢谢!

expect
  • 2 个回答
  • 11468 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve