Python for Linux and Unix System Administration -1 ping

本文探讨了Python中os模块的基础用法,以及如何通过Shell脚本进行系统操作。实例包括设置服务器属性、ping操作和文件系统检查。同时展示了不同编程环境下命令执行和路径操作的对比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

OS module

os — Miscellaneous operating system interfaces — Python 3.10.4 documentation 

OS Module in Python with Examples - GeeksforGeeks

#!/usr/bin/env python

class Server(object):
    def __init__(self,ip,hostname):
        self.ip = ip
        self.hostname = hostname
    def set_ip(self,ip):
        self.ip = ip
    def set_hostname(self,hostname):
        self.hostname = hostname
    def ping(self,ip_addr):
        print("Pinging %s from %s(%s)"%(ip_addr,self.ip,self.hostname))
        
if __name__ =="__main__":
    server = Server('192.168.1.20','bumbly')
    server.ping('192.168.1.15')

 

wes@wes:~/Documents/python/ch1$ cat shell-1
#!/bin/bash
for a in 1 2; do
        for b in a b; do
                echo "$a $b"
        done
done

wes@wes:~/Documents/python/ch1$ sh shell-1
1 a
1 b
2 a
2 b
wes@wes:~/Documents/python/ch1$ cat python-1
#!/usr/bin/env python
for a in [1, 2]:
        for b in ['a','b']:
                print a,b

wes@wes:~/Documents/python/ch1$ python python-1
1 a
1 b
2 a
2 b
wes@wes:~/Documents/python/ch1$ cat shell-2
#!/bin/bash

if [ -d "/tmp" ]; then
        echo "/tmp is a directory"
else
        echo "/tmp is not a directory"
fi

wes@wes:~/Documents/python/ch1$ sh shell-2
/tmp is a directory
wes@wes:~/Documents/python/ch1$ cat python-2
#!/usr/bin/env python

import os
if os.path.isdir("/tmp"):
    print("/tmp is a directory\n")
else:
    print("/tmp is not a directory\n")


wes@wes:~/Documents/python/ch1$ python python-2
/tmp is a directory


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值