Write this blog before, I Google a bunch of related articles, mostly said to modify/etc/sudoers, and then nopasswd: designated CMD, but really no use, no remote virtual terminal This method is cloud, ubuntu10.04 Server Pro-Test!!
Remote operation with SSH
command format
Copy Code code as follows:
Ssh-p $port $user @ $p ' cmd '
$port: SSH Connection port number
$user: SSH connection user name
$IP: IP address for SSH connection
CMD: What the remote server needs to do
Preparatory work
Based on the public key authentication or user name password authentication can ensure that log on to the remote LOCAL2 server (a bit basic operational knowledge of the people do this thing is not a problem)
CMD if it is a script, pay attention to the absolute path problem (the relative path is in the remote execution is the pit)
Insufficient
This command can meet most of our requirements, but typically requires root permissions when you deploy many things, but there are several limitations:
Remote server local2 prevents root user from logging in
Converting identities in remote server scripts expect requires a send password, which is not safe enough.
SSH-T parameter
Copy Code code as follows:
-T Force pseudo-tty allocation. This can is used to execute arbitrary screen-based programs on a remote machine, which can is very useful, e.g. when imple Menting Menu Services. MULTIPLE-T options force TTY allocation, even if SSH has no local tty.
can provide a remote server virtual TTY terminal, plus this parameter we can in the remote server virtual terminal to enter their own password, very safe
Command format
Copy Code code as follows:
SSH-T-P $port $user @ $ip ' cmd '
Sample Script
Copy Code code as follows:
#!/bin/bash
#变量定义
ip_array= ("192.168.1.1" "192.168.1.2" "192.168.1.3")
User= "Test1"
Remote_cmd= "/home/test/1.sh"
#本地通过ssh执行远程服务器的脚本
For IP in ${ip_array[*]}
Todo
if [$IP = "192.168.1.1"]; Then
Port= "7777"
Else
Port= "22"
Fi
SSH-T-P $port $user @ $ip "Remote_cmd"
Done
This method is also very convenient,-t virtual out of a remote server terminal, in multiple servers deployed at the same time did save a lot of time Ah!