ssh remote command execution method and shell script example

-t parameter of ssh

 

copy code code show as below:

-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.  

 

Chinese translation: It is a virtual tty terminal that can provide a remote server. With this parameter, we can enter our own privilege escalation password on the virtual terminal of the remote server. Very safe 
command format

copy code code show as below:

ssh -t -p $port $user@$ip  'cmd'  

 

sample script

 

copy code code show as below:

#!/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  
  
locally via ssh Script for remote server  
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  

 

This method is still very convenient. -t virtualizes the terminal of a remote server, which really saves a lot of time when multiple servers are deployed at the same time!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326482700&siteId=291194637