Preface O & M is a magical task. It is easy to handle complicated tasks. The daily O & M of SB may not be able to solve the problem (PS: because he spent all his time on repetitive tasks.) before writing this blog, I Google a bunch of related articles, mostly about modifying/etc/sudoers, then nopasswd: the specified cmd, but it does not really work. If there is no remote virtual terminal, this method is the cloud, ubuntu10.04 server test !! Format of remote command execution using SSH
Ssh-p $ port $ user @ $ P 'cmd' $ port: SSH connection port number $ User: SSH connection user name $ IP: IP address of the SSH connection cmd: operations to be performed on the remote server
Preparations
- Based on public/private key authentication or user name/password authentication, you can log on to the remote local2 server. (this is not a problem if you have basic O & M knowledge)
- If it is a script, pay attention to the absolute path problem (the relative path is pitfall during Remote execution)
This command is insufficient to meet most of our needs, but usually the root permission is required for O & M to deploy many things, but there are several restrictions:
- Remote Server local2 prevents Root User Logon
- In the remote server script, sending a password is required for converting the identity using the login CT, which is not safe enough.
Execute scripts that require sudo permissions on remote servers
-T parameter of 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.
You can provide a remote server virtual tty terminal. With this parameter, you can enter your own Authorization password on the remote server virtual terminal. This is a very safe command format.
Ssh-T-p $ port $ user @ $ IP 'cmd'
Sample script
#! /Bin/bash # variable definition ip_array = ("192.168.1.1" "192.168.1.2" "192.168.1.3 ") user = "test1" remote_cmd = "/home/test/1.sh" # execute the remote server script locally through SSH for IP in $ {ip_array [*]} doif [$ IP =" 192.168.1.1 "]; thenport = "7777" elseport = "22" fissh-T-p $ port $ user @ $ IP "remote_cmd" done
Note that this method is very convenient.-T virtualizes a remote server terminal, which saves a lot of time when multiple servers are deployed at the same time!