导航:首页 > 操作系统 > linux远程执行shell脚本

linux远程执行shell脚本

发布时间:2022-08-13 18:57:18

linux 中怎样执行脚本

首先脚本需要有执行权限

chmo+xfile.sh

执行脚本有三种方法:

1../file.sh

特点:开启bash子进程来执行,也就是开启额外的进程来进行,不影响原进程的变量、配置等

2.bashfile.sh

特点:和./file.sh相同

3.sourcefile.sh或者.file.sh

特点:在原bash进程中执行脚本。


第三种方法主要用于在脚本中切换用户su、切换目录cd等命令

source和.命令是相同的。

你可以搜索source

补充,如何查看脚本运行是否开启了bash子进程

  1. vim file.sh

  2. 写入

    #!/bin/bash

    #echo $$命令会输出bash进程ID

    echo $$

  3. 保存并赋予可执行权限chmod u+x file.sh

  4. 在你的shell中输入,echo $$ 屏幕输出4176

  5. ./file.sh 屏幕输出3600

  6. bash file.sh 屏幕输出3984

  7. source file.sh 屏幕输出4176 和 你直接在shell中输出的一样,说明是在同一个bash进程

② linux 如何自动远程执行脚本

1、安装sshpass
2、sshpass -p “passwd” ssh -p22 root@$dst_ip “a.sh”
注意a.sh是目标主机里的脚本。

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

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

④ shell脚本 ,在linux 下运行一个shell脚本登陆远程unix 服务器,请问这个脚本如何写

#!/bin/bash
tmptty=`tty`
tmptty=`basename $tmptty`
tmpname=`whoami`
ip="xxx" #目标主机地址
inp1="xxx^M" #主机的用户名,,注意必须有^M
inp2="xxx^M" #主机的密码,注意必须有^M
inp3="ls^M"
inp4="pwd^M"
inputfile=in
outputfile=out.log
rm -fr $inputfile
rm -fr $outputfile
mknod $inputfile p
touch $outputfile
#file description 7 for out and 8 for in
exec 7<>$outputfile
exec 8<>$inputfile
telnet $ip <&8 >&7 &
sleep 2; echo $inp1 >> $inputfile
sleep 2; echo $inp2 >> $inputfile
sleep 2; echo $inp3 >> $inputfile
sleep 2; echo $inp4 >> $inputfile
tail -f $outputfile &
while true
do
read str
if [[ $str = "quit" || $str = "exit" ]]
then echo $str >> $inputfile exit
else echo $str >> $inputfile
fi
done
ps -ef | grep telnet | grep -v grep | grep -v telnetd | grep $tmptty | grep $tmpname | awk '{print " kill -9", $2}' | sh
ps -ef | grep tail | grep -v grep | grep -v telnetd | grep $tmptty | grep $tmpname | awk '{print " kill -9", $2}' | sh

⑤ linux远程登陆的shell脚本for循环无结果

我比较认同“ssh之后是远程主机执行的命令
本地变量不起作用”这种说法
你的脚本之所以$aaa能回显,是因为在本地定义了这个变量,但本地没有定义$i这个变量,所以无法显示变量值,把aaa="u1 u2"一块塞<<ff.......ff这个内联重定向数据中去。再试试看。

阅读全文

与linux远程执行shell脚本相关的资料

热点内容
怎么显示android的APP 浏览:121
c编译器怎么删除空格 浏览:695
php自动释放内存 浏览:219
golang编译库 浏览:794
oracle数据字符串加密 浏览:603
研究生去上海当程序员 浏览:90
u8电脑服务器连接失败怎么解决 浏览:569
bat脚本创建日期命名文件夹 浏览:104
将图片转换为pdf格式 浏览:980
java中形参 浏览:83
枚举类型编译器 浏览:519
oraclejava包 浏览:568
手机定位手机怎么定位安卓 浏览:523
在哪个app买欧莱雅最便宜 浏览:495
程序员吃零食好吗 浏览:261
php工程师主要做什么 浏览:356
tvp保存到哪个文件夹 浏览:197
怎么把空调里面的压缩机拆卸掉 浏览:943
linux4k对齐 浏览:968
单片机与开关电源 浏览:276