简体   繁体   English

如何在远程服务器上执行本地Shell脚本?

[英]How to execute local shell script on remote servers?

I have a shell script in my local machine and I have to execute that shell script on remote servers. 我的本地计算机上有一个Shell脚本,我必须在远程服务器上执行该Shell脚本。 I have done some steps as follows: 我已经完成了一些步骤,如下所示:

  1. Paired SSH key from local machine and remote server; 来自本地计算机和远程服务器的配对SSH密钥;
  2. ssh user@hostname 'bash - s' < user_add.sh ; ssh user@hostname 'bash - s' < user_add.sh ;

I am getting an error: 我收到一个错误:

standard in must be a tty
: command not found
bash: line 4: useradd: command not found
Usage: /etc/init.d/vsftpd {start|stop|restart|condrestart|status}
Only root can do that.
chmod: cannot access `/102/prsuser\r': Permission denied
mkdir: cannot create directory `/102/prov/\r': Permission denied
bash: line 8: useradd: command not found
Only root can do that.
chmod: cannot access `/102/prov/PROV_LIS_RESP_DIR\r': Permission denied
chown: cannot access `/102/prov/\r': Permission denied
bash: line 12: /etc/vsftpd/chroot_list: Permission denied

Root login is can't be done normally, we can login as root by switching from normal user account by su - command . 无法正常进行root登录,我们可以通过su - command从普通用户帐户切换为root用户登录。

So here is my doubt, how to run the script as a root on remote server and what script used to switch root account at the starting point within my script (user_add.sh)? 因此,这是我的疑问,如何在远程服务器上以根用户身份运行脚本,以及在脚本(user_add.sh)的起始点使用哪个脚本来切换根帐户?

您可以修改user_add.sh以便它可以对具有root特权的用户执行命令:

su $ROOT_USER -c "command arg1 arg2"

You can use sudo instead of su (and define all the commands you need to use as your user in the sudoers file) 您可以使用sudo而不是su(并在sudoers文件中定义需要作为用户使用的所有命令)

You could also enable the root account. 您也可以启用根帐户。 As a side note, the account doesn't necessarily need to be named "root", you can actually create a new user (named whatever) and then edit the passwd file and assign a uid value of 0 to that account. 附带说明,该帐户不一定需要命名为“ root”,您实际上可以创建一个新用户(命名为任何名称),然后编辑passwd文件并为该帐户分配uid值0。

Well, it's a little bit strange, what you try to achieve, but you can do it: 好吧,您尝试实现的目标有点奇怪,但是您可以做到:

ssh user@hostname sudo -S < user_add.sh

The commands that are in user_add.sh will be interpreted at hostname and run within sudo . user_add.sh的命令将以hostname解释,并在sudo运行。 The user user must have correspondent entry in /etc/sudoers at hostname (with NOPASSWD statement). 用户user必须在hostname /etc/sudoers中具有对应的条目(带有NOPASSWD语句)。

If you can't add NOPASSWD you need to run the command this way: 如果您不能添加NOPASSWD ,则需要以这种方式运行命令:

(echo PASSWORD; cat user_add.sh) | ssh user@hostname

Instead of PASSWORD you must write password of user at hostname . 您必须在hostname上输入user密码,而不是PASSWORD。

If your sudo doesn't add /sbin to PATH automatically, you need to do it manually: 如果您的sudo没有自动将/sbin添加到PATH ,则需要手动进行操作:

(echo PASSWORD; echo 'PATH=/sbin:/usr/sbin:$PATH'; cat user_add.sh) \
| ssh user@hostname 

And the last thing. 还有最后一件事。 I noted that you have \\r symbols in your script (edited on Windows?). 我注意到您的脚本中有\\r符号(在Windows上进行过编辑?)。 Better remove them: 最好删除它们:

(echo PASSWORD; echo 'PATH=/sbin:/usr/sbin:$PATH'; cat user_add.sh) \
| tr -d '\r' \
| ssh user@hostname 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM