What are the ssh remote command execution methods and Shell scripts
This article focuses on "what ssh remote command execution methods and Shell scripts", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what ssh remote command execution methods and Shell scripts are available.
Ssh performs remote operations
Command format
The code is as follows:
Ssh-p $port $user@$p 'cmd'
$port: ssh connection port number
$user: ssh connection user name
Ip address of $ip:ssh connection
Cmd: actions to be performed by the remote server
Preparatory work
Based on public and private key authentication or user name password authentication can ensure login to the remote local2 server (it is not a problem for people with basic knowledge of operation and maintenance to do this)
If cmd is a script, pay attention to the problem of absolute path (relative path is a pit when executed remotely)
Deficiency
This command can meet most of our needs, but usually when OPS deploys a lot of things, it requires root permission, but there are several restrictions:
Remote server local2 forbids root users from logging in
Changing identities to expect in a remote server script requires a send password, which is not secure enough
-t parameter of ssh
The code is as follows:
-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.
That is, we can provide a virtual tty terminal of a remote server. With this parameter, we can enter our own authorization password on the virtual terminal of the remote server, which is very secure.
Command format
The code is as follows:
Ssh-t-p $port $user@$ip 'cmd'
Sample script
The code is as follows:
#! / 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 remote server scripts locally through ssh
For ip in ${ip_array [*]}
Do
If [$ip = "192.168.1.1"]; then
Port= "7777"
Else
Port= "22"
Fi
Ssh-t-p $port $user@$ip "remote_cmd"
Done
At this point, I believe you have a deeper understanding of "what ssh remote command execution methods and Shell scripts". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!