[linux] ssh远程执行本地脚本

  1.ssh密钥登录

 略

  2.免确认机器指纹,ssh -o StrictHostKeyChecking=no

[root@XM-v125 ~]# ssh hjfeng@192.168.0.110
The authenticity of host '192.168.0.110 (192.168.0.110)' can't be established.
ECDSA key fingerprint is SHA256:vV9qNhRA/1/nDkjI0kUiwQBV86MyvfBjEY3V/7IOYZA.
ECDSA key fingerprint is MD5:37:95:b7:c0:fa:e4:76:63:2d:5e:cb:54:be:85:6e:08.
Are you sure you want to continue connecting (yes/no)? no
Host key verification failed.
[root@XM-v125 ~]# ssh -o StrictHostKeyChecking=no hjfeng@192.168.0.110
Warning: Permanently added '192.168.0.110' (ECDSA) to the list of known hosts.
Last login: Mon Sep  3 15:23:24 2018 from 192.168.1.180
[hjfeng@XM-v110 ~]$ 

  3.执行本地脚本,< /root/local.sh

[root@XM-v125 ~]# cat /root/local.sh 
echo $PATH
hostname
echo $1
ip a show dev eth0
[root@XM-v125 ~]# ssh -o StrictHostKeyChecking=no hjfeng@192.168.0.110  < /root/local.sh 
Pseudo-terminal will not be allocated because stdin is not a terminal.
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/local/jdk/bin:/home/hjfeng/.local/bin:/home/hjfeng/bin
XM-v110

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether ae:07:76:1d:76:70 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.110/23 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::ac07:76ff:fe1d:7670/64 scope link 
       valid_lft forever preferred_lft forever
[root@XM-v125 ~]# 

  4.脚本加参数,bash -s < /root.local.sh arg1

[root@XM-v125 ~]# ssh -o StrictHostKeyChecking=no hjfeng@192.168.0.110 < /root/local.sh arg1
bash: arg1: command not found
[root@XM-v125 ~]# ssh -o StrictHostKeyChecking=no hjfeng@192.168.0.110 bash -s < /root/local.sh arg1
/usr/local/bin:/usr/bin:/usr/local/jdk/bin
XM-v110
arg1
bash: line 4: ip: command not found
[root@XM-v125 ~]# 

  5.以sudo权限执行

[root@XM-v125 ~]# ssh -o StrictHostKeyChecking=no hjfeng@192.168.0.110 sudo bash -s < /root/local.sh arg1
/sbin:/bin:/usr/sbin:/usr/bin
XM-v110
arg1
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether ae:07:76:1d:76:70 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.110/23 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::ac07:76ff:fe1d:7670/64 scope link 
       valid_lft forever preferred_lft forever

  6.特殊符号的脚本参数

[root@XM-v125 ~]# ssh -o StrictHostKeyChecking=no hjfeng@192.168.0.110 sudo bash -s < /root/local.sh -d
bash: -d: invalid option
Usage:	bash [GNU long option] [option] ...
	bash [GNU long option] [option] script-file ...
GNU long options:
	--debug
	--debugger
	--dump-po-strings
	--dump-strings
	--help
	--init-file
	--login
	--noediting
	--noprofile
	--norc
	--posix
	--protected
	--rcfile
	--rpm-requires
	--restricted
	--verbose
	--version
Shell options:
	-irsD or -c command or -O shopt_option		(invocation only)
	-abefhkmnptuvxBCHP or -o option
[root@XM-v125 ~]# ssh -o StrictHostKeyChecking=no hjfeng@192.168.0.110 'cat| sudo bash /dev/stdin -d' < /root/local.sh
/sbin:/bin:/usr/sbin:/usr/bin
XM-v110
-d
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether ae:07:76:1d:76:70 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.110/23 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::ac07:76ff:fe1d:7670/64 scope link 
       valid_lft forever preferred_lft forever

  

 

posted @ 2018-09-28 09:44  金枪语  阅读(8863)  评论(0编辑  收藏  举报