她能用在单身汉身上吗?
我正试图从https://stackoverflow.com/questions/16928004/how-to-enter-ssh-password-using-bash中运行这个脚本,在一条直线中。
如果我将它放入一个文件中,并与./file一起运行,它就会像预期的那样工作,但是我如何能够以oneliner的身份运行它呢?
这是一个脚本,没有任何问题。
#!/usr/bin/expect -f
spawn ssh user@my.server.com
expect "assword:"
send "mypassword\r"
interact我怎么能从单身汉那里运行呢?
如果我想跑
spawn ssh user@my.server.com; expect "assword:"; send "mypassword\r"; interact
它返回:
bash: spawn: command not found如果我想跑
#!/usr/bin/expect -f; spawn ssh user@my.server.com; expect "assword:"; send "mypassword\r"; interact
然后整件事被当作评论对待,什么也不会发生。
编辑:
试着运行答案中建议的内容:
expect -c `spawn ssh user@server "ls -lh /some/file"; expect "assword:"; send "mypassword\r"; interact`它回来了
bash: spawn: command not found
couldn't read file "assword:": no such file or directory
bash: send: command not found
bash: interact: command not found
expect: option requires an argument -- c跑步时:
$ cat << 'EOF' | /usr/bin/expect -
> spawn ssh user@server "ls -lh file"
> expect "assword:"
> send "password\r"
> interact
> EOF它看起来像是SSH到服务器,但不运行命令,或者它没有输出任何结果:
它返回:
spawn ssh user@server ls -lh file
user@server password当我从脚本中运行它时:
./test
spawn ssh user@server ls -lh file
user@server's password:
-rw-rw-r-- 1 user user 467G Jan 2 00:46 /fileEDIT2:
第一个命令的问题是返回,而不是单引号,下面的命令按预期工作。
expect -c 'spawn ssh user@server "ls -lh file"; expect "assword:"; send "mypassword\r"; interact':
发布于 2016-01-02 00:26:57
expect可以从标准输入读取它的脚本:
cat << 'EOF' | /usr/bin/expect -
spawn ssh user@my.server.com
expect "assword:"
send "mypassword\r"
interact
EOFhttps://unix.stackexchange.com/questions/252777
复制相似问题