使用expect脚本在远程服务器上执行本地shell脚本(无需将其复制到远程服务器上)

发布于 2024-12-19 16:10:27 字数 568 浏览 2 评论 0原文

我是脚本世界的新手。我在使用 Expect 脚本在远程服务器上执行本地 shell 脚本时遇到问题。 我的脚本如下

VAR=$(/home/local/RD/expect5.45/expect -c "
spawn -noecho ssh -q -o StrictHostKeyChecking=no $USER@$HOST $CMD
match_max 100000
expect \"*?assword:*\"
send -- \"$PASS\r\"
send -- \"\r\"
send  \"exit\n\r\"
expect eof
")

如果 CMD 是诸如 df -kh;top 之类的基本命令,则它可以正常工作。 但我需要在远程服务器上收集一些统计信息,我已为其创建了 shell 脚本。 我尝试过以下操作,但没有成功,

spawn -noecho ssh -q -o StrictHostKeyChecking=no $USER@$HOST 'bash -s' < localscript.sh

它无法在远程服务器上选择和执行本地脚本。 请帮助解决这个问题。

I am new to world of scripting. I am getting problem while executing local shell script on remote server using expect script.
my script is following

VAR=$(/home/local/RD/expect5.45/expect -c "
spawn -noecho ssh -q -o StrictHostKeyChecking=no $USER@$HOST $CMD
match_max 100000
expect \"*?assword:*\"
send -- \"$PASS\r\"
send -- \"\r\"
send  \"exit\n\r\"
expect eof
")

It is working fine if CMD is basic commands like df -kh;top.
But I need to collect several stats on remote server for which i have created a shell script.
I have tried following with no luck

spawn -noecho ssh -q -o StrictHostKeyChecking=no $USER@$HOST 'bash -s' < localscript.sh

its not able to pick and execute localscript on remote server.
Please help to resolve this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

输什么也不输骨气 2024-12-26 16:10:27

上次我尝试类似的操作时,我很快就厌倦了使用 expect(1) 尝试正确响应密码提示。当我终于花了十分钟学习如何创建一个ssh密钥< /a>、将密钥复制到远程系统设置 ssh-agent 使基于密钥的登录更容易自动化,我从来没有遇到过远程运行脚本的问题:

ssh remotehost "commands ; go ; here"

首先,检查您是否需要创建密钥或者您是否已经有一个密钥:

ls -l ~/.ssh/id_*

如果没有列出文件,则运行:

ssh-keygen

并回答提示。

生成密钥后,将其复制到远程系统:

ssh-copy-id remote

大多数现代系统都会将 ssh-agent(1) 作为桌面启动的一部分运行;要确定代理是否已启动,请运行:

ssh-add -l

如果您看到“代理没有身份。”,那么您就可以开始了。如果您看到“无法打开与您的身份验证代理的连接。”那么您必须研究一下将 ssh-agent(1) 插入您的环境的最佳位置。或者,完全放弃代理,这只是一个很好的便利。

添加您的密钥,也许有超时,因此它仅在短时间内有效:

ssh-add -t 3600

现在测试一下:

ssh remote "df -hk ; ps auxw ; ip route show ; free -m"

expect(1) 绝对是一个简洁的工具,但远程系统上的身份验证更容易(也更安全) )使用 SSH 密钥完成。

The last time I tried something like this, I quickly grew weary of using expect(1) to try to respond to the password prompts correctly. When I finally spent the ten minutes to learn how to create an ssh key, copy the key to the remote system, and set up the ssh-agent to make key-based logins easier to automate, I never had trouble running scripts remotely:

ssh remotehost "commands ; go ; here"

First, check if you need to create the key or if you already have one:

ls -l ~/.ssh/id_*

If there are no files listed, then run:

ssh-keygen

and answer the prompts.

Once your key is generated, copy it to the remote system:

ssh-copy-id remote

Most modern systems run ssh-agent(1) as part of the desktop start up; to determine if you've got the agent started already, run:

ssh-add -l

If you see "The agent has no identities.", then you're good to go. If you see "Could not open a connection to your authentication agent." then you'll have to do some research about the best place to insert the ssh-agent(1) into your environment. Or, forgo the agent completely, it is just a nice convenience.

Add your key, perhaps with a timeout so it is only valid for a short while:

ssh-add -t 3600

Now test it:

ssh remote "df -hk ; ps auxw ; ip route show ; free -m"

expect(1) is definitely a neat tool, but authentication on remote systems is easier (and more safely) accomplished with SSH keys.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文