当前位置:首页 » 编程软件 » ssh远程执行本地脚本

ssh远程执行本地脚本

发布时间: 2022-07-28 07:34:52

‘壹’ linux下如何使用ssh远程登录主机 执行shell脚本

知道linux的ip,用户和密码就可以远程登陆了。在你的SSH 客户端会有一个linux的终端。在这执行命令就可以了。

‘贰’ 怎么通过ssh在远程Host执行交互命令

ssh执行远程操作
命令格式

复制代码 代码如下:

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

$port : ssh连接端口号
$user: ssh连接用户名
$ip:ssh连接的ip地址
cmd:远程服务器需要执行的操作

准备工作
基于公私钥认证或者用户名密码认证能确保登录到远程local2服务器(有点基本运维知识的人做这个事情都不是问题)
cmd如果是脚本,注意绝对路径问题(相对路径在远程执行时就是坑)
不足
这个命令可以满足我们大多数的需求,但是通常运维部署很多东西的时候需要root权限,但是有几处限制:
远程服务器local2禁止root用户登录
在远程服务器脚本里转换身份用expect需要send密码,这样不够安全
ssh的-t参数
复制代码 代码如下:

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

中文翻译一下:就是可以提供一个远程服务器的虚拟tty终端,加上这个参数我们就可以在远程服务器的虚拟终端上输入自己的提权密码了,非常安全
命令格式

复制代码 代码如下:

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

示例脚本
复制代码 代码如下:

#!/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[*]}
do
if [ $ip = "192.168.1.1" ]; then
port="7777"
else
port="22"
fi
ssh -t -p $port $user@$ip "remote_cmd"
done

这个方法还是很方便的,-t虚拟出一个远程服务器的终端,在多台服务器同时部署时确实节约了不少时间啊!

‘叁’ 请教如何在Bash里调用ssh远程执行命令

直接将所有可执行的各种命令写在 bash 的 SHELL 脚本文件中即可。当然了,至于说该 SHELL 脚本文件是否可以执行?并不是说只要你的SHELL脚本文件中的命令都是可执行的,就能够正确执行的。必须要使用命令:chmod +x my_shell.txt 将其修改成可执行权限,且 SHELL 脚本文件中的每一个命令都是拥有 x 权限(可执行权限)的,才能够在命令行状态 $ 下面正常执行。

‘肆’ Linux自动登陆expect 远程主机执行本地脚本

两台ssh之间做个密钥,尽量不要使用expect写脚本。因为expect交互式登录是需要你在脚本中写密码的。

‘伍’ shell脚本中怎么ssh上远程机执行命令

你好,
先在两台机子上建立信任,ssh-key
具体方法网络下就有
脚本里的命令是
ssh 用户@ip

‘陆’ shell本地脚本调用远程脚本,当远程脚本执行完再继续执行本地脚本

#!/bin/bash

#调用expect脚本执行远程sh
expect-c'
settimeout10000
spawnsshusr@ip
expect{
"yes/no"{send"yes ";exp_continue}
"*assword"{send"passwd "}
}
expect"#"
send"sh1 "
expect"#"
'
echo
ls-l

其中timeout时间需要设置足够长,不然会连接超时断掉。

或者你配置了rsh,可以把上面的expect过程替换成rsh [Host] [-l UserName] [-n] [Command]

‘柒’ ssh如何远程执行命令

ssh支持远程命令参数
可以类似方式嵌套:
alias pushbaby='cd /Users/xuqiang/Develop/work/tickets/baby/deploy;scp -r -v -i ~/.ssh/pedal/id_rsa baby.tar.gz baby.sql.txt qiang.xu@10.132.64.206:/home/qiang.xu/tmp;ssh qiang.xu@10.132.64.206 -i ~/.ssh/pedal/id_rsa "source ~/.bash_profile;pushbaby";ssh qiang.xu@10.132.64.206 -i ~/.ssh/pedal/id_rsa "ssh bkapps@10.132.24.100 \"source ~/.bash_profile;pushbaby\""'更新要注意的是alias在非交互模式无法使用,需将替换成function

‘捌’ ssh如何远程运行matlab程序脚本

1、不开启图形界面
matlab -nodesktop –nosplash
或者matlab –nodisplay
或者matlab -nojvm –nosplash
2、 matlab程序也可以在命令行里直接运行,只需要使用 -r 选项。比如运行当前目录下的example.m
matlab -nodesktop -nosplash -r example
或者matlab -nojvm -nosplash -r example
或者matlab -nodisplay -r example

‘玖’ linux中ssh如何远程执行一条命令,而且不登录远程服务器

一 SSH命令使用技巧

- 远程登录

ssh user@remote.machine

- 远程执行

ssh user@remote.machine 'command ...'

- 远程复制

scp user@remote.machine:/remote/path /local/path

scp /local/path user@remote.machine:/remote/path

- X forward

ssh -X user@remote.machine

xcommand ...

- Tunnel / Portforward

ssh -L 1234:remote.machine:4321 user@remote.machine

ssh -R 1234:local.machine:4321 user@remote.machine

ssh -L 1234:other.machine:4321 user@remote.machine

二, 实作

1) 禁止 root 登录

# vi /etc/ssh/sshd_config

PermitRootLogin no

2) 废除密码登录, 强迫使用 RSA 验证(假设 ssh 账户为 user1 )

# vi /etc/ssh/sshd_config

RSAAuthentication yes

PubkeyAuthentication yes

AuthorizedKeysFile .ssh/authorized_keys

PasswordAuthentication no

# service sshd restart

# su - user1

$ mkdir ~/.ssh 2>/dev/null

$ chmod 700 ~/.ssh

$ touch ~/.ssh/authorized_keys

$ chmod 644 ~/.ssh/authorized_keys

登入端:

$ ssh-keygen -t rsa

(按三下 enter 完成﹔不需设密码,除非您会用 ssh-agent 。)

$ scp ~/.ssh/id_rsa.pub user1@server.machine:id_rsa.pub

(若是 windows client, 可用 puttygen.exe 产生 public key,

然后复制到 server 端后修改之, 使其内容成为单一一行.)

回到 server 端:

$ cat ~/id_rsa.pub >> ~/.ssh/authorized_keys

$ rm ~/id_rsa.pub

$ exit

3) 限制 su / sudo 名单:

# vi /etc/pam.d/su

auth required /lib/security/$ISA/pam_wheel.so use_uid

# visudo

%wheel ALL=(ALL) ALL

# gpasswd -a user1 wheel

4) 限制 ssh 使用者名单

# vi /etc/pam.d/sshd

auth required pam_listfile.so item=user sense=allow file=/etc/ssh_users ōnerr=fail

# echo user1 >> /etc/ssh_users

热点内容
linux修改记录 发布:2025-03-17 04:13:41 浏览:995
怎么验算法 发布:2025-03-17 04:12:56 浏览:569
c语言申请空间 发布:2025-03-17 04:07:47 浏览:463
安装phpgd 发布:2025-03-17 03:56:00 浏览:631
压缩四元数 发布:2025-03-17 03:54:34 浏览:77
数目数据库 发布:2025-03-17 03:53:52 浏览:571
手机删除文件夹出错 发布:2025-03-17 03:49:02 浏览:604
c语言编译器app官网 发布:2025-03-17 03:48:51 浏览:814
我的世界ec服务器怎么用大喇叭 发布:2025-03-17 03:48:49 浏览:340
如何设置linux代理服务器 发布:2025-03-17 03:47:39 浏览:104