SSH remote execution command method and Shell script instance _linux Shell

Source: Internet
Author: User
Tags ssh port number

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.