导航:首页 > 操作系统 > 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脚本相关的资料

热点内容
国家反诈中心app注册登记表怎么注册 浏览:972
加密机默认端口 浏览:101
有哪个网站有免费的python源代码 浏览:304
苹果手机如何导入安卓电话 浏览:915
奥利奥双重解压 浏览:388
安卓账号怎么在苹果手机上玩 浏览:798
画画用什么安卓ipad好 浏览:693
手机怎么用pdf 浏览:697
怎么把多个word合并成一个pdf 浏览:542
电学基础pdf 浏览:726
cad插件怎么编译 浏览:617
网校源码软件 浏览:667
思科常用配置命令 浏览:377
水容易被压缩吗 浏览:753
java项目试题 浏览:316
为什么安卓拍照没有苹果成相快 浏览:516
安卓版的苹果手机叫什么 浏览:373
手机怎么解压文件夹压缩包 浏览:459
起源任务咋解压 浏览:976
加密式的监督检查 浏览:549