ssh执行命令
- 执行一条命令
-
ssh user@ip command
-
- 执行多条命令
-
ssh user@ip "command1;command2" #分号隔开
-
- 关于转义
-
ssh user@ip 'echo $HOME' #单引号,本机不转义,到目标机器执行 ssh user@ip "echo $HOME" #双引号,先在本机转义,到目标ip执行
-
[root@ricky01 ~]# ssh ricky02@192.168.56.101 'echo $HOME' #单引号,先不转义,到目标机器执行echo $HOME
ricky02@192.168.56.101's password:
/home/ricky02[root@ricky01 ~]# ssh ricky02@192.168.56.101 "echo $HOME" #双引号,先把$HOME转成/root,到目标机器执行echo /root
ricky02@192.168.56.101's password:
/root
-
- 关于awk
-
ip a|grep 192.168*|awk '{ print $2 }' 192.168.56.102/24 #本机可以正确执行
-
[root@ricky01 ~]# ssh 192.168.56.101 ip a|grep 192.168*|awk '{ print $2 }' #单条命令,不加引号,可以执行 root@192.168.56.101's password: 192.168.56.101/24
-
[root@ricky01 ~]# ssh 192.168.56.101 'ip a|grep 192.168*|awk '{ print $2 }'' #加单引号,报错 root@192.168.56.101's password: awk: cmd. line:1: { awk: cmd. line:1: ^ unexpected newline or end of string
-
[root@ricky01 ~]# ssh 192.168.56.101 "ip a|grep 192.168*|awk '{ print $2 }'" #加双引号,没有执行awk root@192.168.56.101's password: inet 192.168.56.101/24 brd 192.168.56.255 scope global dynamic enp0s8
-
[root@ricky01 ~]# ssh 192.168.56.101 "ip a|grep 192.168*|awk '{ print \$2 }'" #双引号,$前加转义符号\,正确执行 root@192.168.56.101's password: 192.168.56.101/24
-
[root@ricky01 ~]# ssh 192.168.56.101 'ip a|grep 192.168*|awk '{ print \$2 }'' #单引号,$前加转义符号\,报错
root@192.168.56.101's password: awk: cmd. line:1: { awk: cmd. line:1: ^ unexpected newline or end of string
-