如何在通过脚本登录远程服务器时传递标准输入

如何在通过脚本登录远程服务器时传递标准输入

当登录到我的远程服务器时,它需要用户输入,它的 bash_profile 以这种方式被修改。

[nikhil]$ cat .bash_profile
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi
read -p "enter your name " Name
echo $Name

我如何使用 ssh 登录到该远程服务器并通过脚本传递该输入。

答案1

安装expect

apt-get install expect

运行以下脚本:

$ expect <<EOF
set timeout -1

spawn ssh -o "StrictHostKeyChecking=no" root@server

expect "root@server's password:"
send -- "12345678PASSWORD\r"
expect "enter your name "
send -- "susan\r"
expect "root@server"
send -- "echo $$\r"
send -- "exit\r"
expect eof
EOF

调整如下:

  • root@server使用您的实际服务器连接参数
  • 显然密码
  • .bash_profile您想要在目标服务器上提供给脚本的名称
  • 您想要在目标服务器上运行的实际命令(而不是echo $$

相关内容