空调怎么手机遥控(pamosautc空调用手机能控制吗)
空调怎么手机遥控,pamosautc空调用手机能控制吗? 可以的,下载万能遥控。 2.点击添加遥控器。 3.选择需要进行手机遥控的设备。 4.选择设备的品牌。 5...
2023-04-17
python执行shell脚本
1.远程:paramiko
2.本地:subprocess
首先要安装pip install cryptography==2.4.2,不然会报错
#coding:utf-8#python批量执行远程shell脚本import paramikoclass MySQLCon: def __init__(self,name,port,uname,pwd): self.name = name self.port = port self.uname = uname self.pwd = pwd def conn(self): self.ssh = paramiko.SSHClient() #创建SSHClient实例对象 self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #免密登陆 self.ssh.connect(hostname=self.name, port=self.port, username=self.uname, password=self.pwd) def close(self): self.ssh.close() def execommand(self,**shell): result = {} for key in shell: stdin, stdout, stderr = self.ssh.exec_command(shell[key]) #获取输入输出及错误 result[key] = stdout.read().decode(encoding="utf-8") return resultif __name__ == "__main__": mysqlcon = MySQLCon('10.xx.xx.x',22,'root','123456') mysqlcon.conn() command = ''' Name="zhangsan" Age=23 Salary=12000 echo "姓名:$Name; 年龄:$Age; 工资:${Salary-"空"}" ''' #shell命令 res = mysqlcon.execommand(shell=command) print(res) mysqlcon.close()【传文件:sftp = ssh.open_sftp() sftp.put(‘源文件’,“要拷贝的地址”) sftp.get()–从Linux往Windows拷贝】
1、subprocess.call():执行命令,并返回执行状态,其中shell参数为False时,命令需要通过列表的方式传入,当shell为True时,可直接传入命令
>>>> a = subprocess.call(['df','-hT'],shell=False)Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 ext4 94G 64G 26G 72% / tmpfs tmpfs 2.8G 0 2.8G 0% /dev/shm /dev/sda1 ext4 976M 56M 853M 7% /boot >>> a = subprocess.call('df -hT',shell=True)Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 ext4 94G 64G 26G 72% / tmpfs tmpfs 2.8G 0 2.8G 0% /dev/shm /dev/sda1 ext4 976M 56M 853M 7% /boot>>> print a02、subprocess.check_call():用法与subprocess.call()类似,区别是,当返回值不为0时,直接抛出异常
>>>> a = subprocess.check_call('df -hT',shell=True)Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 ext4 94G 64G 26G 72% / tmpfs tmpfs 2.8G 0 2.8G 0% /dev/shm /dev/sda1 ext4 976M 56M 853M 7% >>> print a0 >>> a = subprocess.check_call('dfdsf',shell=True)/bin/sh: dfdsf: command not foundTraceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib64/python2.6/subprocess.py", line 502, in check_call raise CalledProcessError(retcode, cmd)subprocess.CalledProcessError: Command 'dfdsf' returned non-zero exit status 1273、subprocess.check_output():用法与上面两个方法类似,区别是,如果当返回值为0时,直接返回输出结果,如果返回值不为0,直接抛出异常。需要说明的是,该方法在python3.x中才有。
4、subprocess.Popen():
在一些复杂场景中,我们需要将一个进程的执行输出作为另一个进程的输入。在另一些场景中,我们需要先进入到某个输入环境,然后再执行一系列的指令等。这个时候我们就需要使用到suprocess的Popen()方法。该方法有以下参数:
args:shell命令,可以是字符串,或者序列类型,如list,tuple。bufsize:缓冲区大小,可不用关心stdin,stdout,stderr:分别表示程序的标准输入,标准输出及标准错误shell:与上面方法中用法相同cwd:用于设置子进程的当前目录env:用于指定子进程的环境变量。如果env=None,则默认从父进程继承环境变量universal_newlines:不同系统的的换行符不同,当该参数设定为true时,则表示使用\n作为换行符示例1,在/root下创建一个suprocesstest的目录:
>>>> a = subprocess.Popen('mkdir subprocesstest',shell=True,cwd='/root')</pre>示例2,使用python执行几个命令:
>import subprocessobj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)obj.stdin.write('print 1 \n')obj.stdin.write('print 2 \n')obj.stdin.write('print 3 \n')obj.stdin.write('print 4 \n')obj.stdin.close()cmd_out = obj.stdout.read()obj.stdout.close()cmd_error = obj.stderr.read()obj.stderr.close() print cmd_out print cmd_error</pre>也可以使用如下方法:
>import subprocessobj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)obj.stdin.write('print 1 \n')obj.stdin.write('print 2 \n')obj.stdin.write('print 3 \n')obj.stdin.write('print 4 \n')out_error_list = obj.communicate() print out_error_list</pre>示例3,将一个子进程的输出,作为另一个子进程的输入:
>import subprocesschild1 = subprocess.Popen(["cat","/etc/passwd"], stdout=subprocess.PIPE)child2 = subprocess.Popen(["grep","0:0"],stdin=child1.stdout, stdout=subprocess.PIPE)out = child2.communicate()其他方法:
>import subprocesschild = subprocess.Popen('sleep 60',shell=True,stdout=subprocess.PIPE)child.poll() #检查子进程状态child.kill() #终止子进程child.send_signal() #向子进程发送信号child.terminate() #终止子进程
以上内容就是为大家推荐的shell调用python脚本(linux简单的shell编程)最佳回答,如果还想搜索其他问题,请收藏本网站或点击搜索更多问题
内容来源于网络仅供参考版权声明:所有来源标注为小樱知识网www.xiaoyin02.com的内容版权均为本站所有,若您需要引用、转载,只需要注明来源及原文链接即可。
本文标题:shell调用python脚本(linux简单的shell编程)
本文地址:https://www.xiaoyin02.com/shcs/118476.html
相关文章
空调怎么手机遥控,pamosautc空调用手机能控制吗? 可以的,下载万能遥控。 2.点击添加遥控器。 3.选择需要进行手机遥控的设备。 4.选择设备的品牌。 5...
2023-04-17
格力空调用手机怎么打开,华为荣耀二零怎么用蓝牙控制空调? 通过蓝牙或wifi控制空调: 1.确保空调支持蓝牙或者wifi连接,将手机的蓝牙和wifi开关打开。...
2023-04-14
手机app怎么开发的,python能做手机软件吗? 这个真不建议,非常不方便,如果你真想这么做,有2种方法,一种是QPython,一种是Kivy,下面我简单介绍一下这...
2023-04-10
美的空调用手机怎么开,华为荣耀20Pro手机可以开美的空调嘛? 华为荣耀20Pro手机可以开美的空调的啊。首先是可保证售后服务的落实,因为空调是一种大...
2023-03-29
空调用手机怎么开,如何用手机开空调制热? 首先你的手机上有自带红外功能一般小米红米手机都有自带,要是没有就从应用商店下一个,然后打开红外遥...
2023-03-27
热点文章
2021年独生子女补贴新政策是真的吗(独生子女证有有效期吗)
2021年国庆节阅兵仪式几点开始几点结束(2021年国庆节还有阅兵吗)
鼠目寸光一点红是什么生肖动物(鼠目寸光一点红)指什么生肖,紧密
k0到k9的玩法大全(强制gc的玩法和注意事项)
入土为安是什么生肖《入土为安》打一个生肖动物,词语解释
浙江12月底全面停工是真的吗(浙江什么时候放假停工)
如何做t(t怎么把p做哭)
北京口碑最差的三甲医院(北京301医院最擅长什么)