bash - running remote script from local machine

2019-06-24 11:42发布

问题:

I tried this:

#!bin/bash
ssh user@host.com  'sudo /etc/init.d/script restart'

But I get this error:

sudo: no tty present and no askpass program specified

How can I run that script? Now when I need to run that script I do these steps:

ssh user@host.com
sudo /etc/init.d/script restart

But I don't want to manually log in to remote server all the time and then enter restart command.

Can I write local script that I could run so I would only need to enter password and it would run remote script and restart the process?

回答1:

You can use -t option in ssh command to attach a pseudo-tty with your ssh command:

ssh -t -t user@host.com 'sudo /etc/init.d/script restart'

As per man ssh:

-t Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.



标签: bash shell