使用expect脚本在远程服务器上执行本地shell脚本(无需将其复制到远程服务器上)
我是脚本世界的新手。我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上次我尝试类似的操作时,我很快就厌倦了使用
expect(1)
尝试正确响应密码提示。当我终于花了十分钟学习如何创建一个ssh
密钥< /a>、将密钥复制到远程系统和设置ssh-agent
使基于密钥的登录更容易自动化,我从来没有遇到过远程运行脚本的问题:首先,检查您是否需要创建密钥或者您是否已经有一个密钥:
如果没有列出文件,则运行:
并回答提示。
生成密钥后,将其复制到远程系统:
大多数现代系统都会将
ssh-agent(1)
作为桌面启动的一部分运行;要确定代理是否已启动,请运行:如果您看到“代理没有身份。”,那么您就可以开始了。如果您看到“无法打开与您的身份验证代理的连接。”那么您必须研究一下将
ssh-agent(1)
插入您的环境的最佳位置。或者,完全放弃代理,这只是一个很好的便利。添加您的密钥,也许有超时,因此它仅在短时间内有效:
现在测试一下:
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 anssh
key, copy the key to the remote system, and set up thessh-agent
to make key-based logins easier to automate, I never had trouble running scripts remotely:First, check if you need to create the key or if you already have one:
If there are no files listed, then run:
and answer the prompts.
Once your key is generated, copy it to the remote system:
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: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:
Now test it:
expect(1)
is definitely a neat tool, but authentication on remote systems is easier (and more safely) accomplished with SSH keys.