gpt4 book ai didi

linux - 在命令中提供密码并执行

转载 作者:太空宇宙 更新时间:2023-11-04 03:57:03 25 4
gpt4 key购买 nike

我是 Linux Shell 脚本的新手。我需要执行一个 shell 脚本,该脚本分别获取用户名、密码、主机名、要执行的命令作为 1、2、3、4 参数。我使用了 ssh 但它提示输入密码。该脚本的主题是从远程服务器执行命令。密码安全问题不是问题。主要问题是我不能被提示输入密码。请帮我解决这个问题。

echo username = $1
echo Password = $2
echo Host-Name = $3
echo Command to Be executed = $4
set -x
expect -c "spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no $1@$3 $4; expect \"*assword:*\"; send "$2\r";expect eof;"

echo "==============="

我的输出:

username = root
Password = root@123
Host-Name = host-name
Command to Be executed = ls
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no root@host-name ls
root@123r===============

我的命令,即 ls 输出未显示。

在expect命令之前使用set -x之后:

输出:

username = root
Password = root@123
Host-Name = host-name
Command to Be executed = ls
+ expect -c 'spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no root@host-name ls; expect "*assword:*"; send root@123r;expect eof;'
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no root@host-name ls
root@123r+ echo
===============
===============
++ printf '\033]0;%s@%s:%s\007' root RHEL5 '~'

最佳答案

您可以使用以下 shell 命令:

expect -c 'spawn ssh USERNAME@SERVER; expect password:; send "PASSWORD\n"; interact'

您的 Linux 系统中可能没有 expect,但我认为您可以轻松安装它。

关于linux - 在命令中提供密码并执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24321418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com