发布新日志

  • Windows下脚本编写资源WMI

    2011-02-20 14:09:32

    熟悉Linux的人应该已经感受过Bash脚本的强大,同样,在Windows系统下,也可以编写类似的脚本来实现强大的系统管理功能。Windows系统提供了
    以下列出了一些有用的windows 脚本编写资源
    1. http://technet.microsoft.com/zh-cn/scriptcenter/default.aspx
    Microsoft脚本中心,这里有非常丰富的windows脚本资源,教程、示例、专栏文章,应有尽有。

    2. http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true
    这是windows系统下batch脚本的一般语法和命令介绍,有助于学习batch脚本的编写

    3. http://www.microsoft.com/china/technet/community/scriptcenter/default.mspx
    TechNet脚本中心,提供了几千个windows示例脚本,以及其他如Perl, Python, Kixtart, Object REXX等脚本。

    4. http://msdn.microsoft.com/zh-cn/library/ms974579.aspx
    http://msdn.microsoft.com/zh-cn/library/ms974592.aspx
    http://msdn.microsoft.com/zh-cn/library/ms974547.aspx
    几篇入门文章

    5. http://www.microsoft.com/china/technet/community/scriptcenter/tools/wmimatic.mspx
    WMI脚本工具,可以帮助用户编写WMI脚本

  • Windows 2003无法访问samba服务

    2010-07-13 17:43:18

    Windows 2003在访问Samba时,提示“xx.xx.xx.xx 无法访问。您可能没有权限使用网络资源。”“网络不存在或尚未启动”等信息,而Windows XP则可以正常访问Samba

    通过在Windows 2003系统上ping Samba服务器,可以确认2003系统是否与Samba服务器网络联通。如果能够ping通samba服务器,说明Samba服务运行正常,网络畅通,问题出在Windows 2003系统上。

    通过查阅资料,发现,Windows 2003需要启动Workstation(创建和维护远程服务的客户端网络连接)服务,才能远程连接到samba服务。

    打开Windows “服务”,找到Workstation一项,发现该服务确实是停止状态。点击“启动”,该服务启动后再尝试连接samba,成功!

  • nload--linux下网络流量监控工具

    2010-06-25 17:00:10

    nload is a ncurse based network traffic analyser, displays the current network usage.

    nload是一个网络流量分析器,可对流入和流出的网络传输数据进行统计。

    1. 启动

    $nload

    默认情况下,nload将以kBit/s给出eth0网卡的流入和流出流量。

    2. 退出

    q or Ctrl+C

    3. 监控多个网络接口

    例如,监控eth0eth1lo设备,可以使用

    $nload eth0 eth1 lo

    监控时,每屏仅显示一个网络接口的流量情况,可以使用ArrowRight, ArrowDown, ArrowLeft, ArrowUp, PageUp, PageDown, Enter, Tab, n and p等键在不同网络接口之间切换。

    也可以使用-m选项使多个设备的流量状态显示在同一屏幕中。

    $nload -m eth0 eth1 lo

    4. 一些常用的选项

    改变更新数据的时间间隔: -t sss (sss is the time in milliseconds)。默认时间间隔是500ms

    改变流量数据的单位: -u h|b|k|m|g for human, bits, kilo, mega or giga bits

    改变流入和流出带宽的图形显示比例: -i XXX(XXXis given in kBits/s),; -o YYY

    流入流量的显示比例用-i改变,流出流量的用-o改变。默认值都是10240

  • bash脚本中出现[[:not found错误的解决方法

    2010-06-24 16:02:40

    今天在写脚本的时候,发生了一个奇怪的问题:在脚本中使用[[的时候报错“[[: not found”。遇到问题自然是解决问题。


    使用的bash版本太低?

    bash --version查看bash版本信息如下

    lee@lee:~$ bash --version

    GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu)

    Copyright (C) 2007 Free Software Foundation, Inc.

    google bash手册,3.2.39已经不算低了,完全支持[[这样的扩展。看来不是版本问题。

    是脚本中[[使用错误?

    写测试脚本进行测试。test.sh测试脚本内容如下

    #!/bin/bash

    [[ 1 ]] && echo "successful"

    执行后仍然是“[[: not found”。但是,在bash交互模式下执行[[ 1 ]] && echo "successful"命令,却是成功的,执行结果如下

    lee@lee:~$ [[ 1 ]] && echo "successful"

    successful

    lee@lee:~$

    看来bash是支持[[扩展的,那么,问题应该就是出在脚本上。

    脚本里的问题存在于哪里呢?

    显然,那条孤零零的命令是没问题的,因为已经在交互模式下验证过了。脚本里还有一行#!/bin/bash

    用来指定运行该脚本所使用的shell类型。显然,我们这里就是要使用bash,所以这一行也没有问题。


    既然脚本的内容没有问题了,那问题究竟在哪里呢?

    从编写和运行等几个环节仔细思考,脚本既然没问题,那问题是不是出在 运行环节上?出于习惯,我经常喜欢$ sh test.sh这样的运行脚本的 方式,那么,换一种运行方式是不是能解决问题呢?在终端下用./test.sh运行,果然,运行成功!至此,问题的症结找到。


    下面的问题是,为什么sh test.sh./test.sh有着不同的运行结果。

    通过查看(ls -l /bin)得知,sh只是一个符号链接,最终指向是一个叫做dash的程序

    lee@lee:~$ ls -hl /bin | grep sh

    -rwxr-xr-x 1 root root 686K 2008-05-13 02:33 bash

    -rwxr-xr-x 1 root root  79K 2009-03-09 21:03 dash

    lrwxrwxrwx 1 root root    4 2010-03-03 01:52 rbash -> bash

    lrwxrwxrwx 1 root root    4 2010-03-03 01:53 sh -> dash

    lrwxrwxrwx 1 root root    4 2010-03-03 01:53 sh.distrib -> bash


    在运行sh test.sh时,首先调用sh命令,而sh指向dash,因此,sh test.sh相当于/bin/dash test.sh。而dash不管是名称还是程序大小,都与bash不同。那么,sh test.sh./test.sh两种命令有了不同的执行结果也就不足为奇。

    在执行./test.sh命令时,bash会自动生成一个subshell来执行该命令,即执行 filename arguments等 同于执行bash filename arguments


    还剩下的一个问题是,dashbash究竟有什么区别?

    Ubuntu wiki上给出了答案。自Ubuntu 6.10以后,系统的默认shell /bin/sh被改成了dashdash(the Debian Almquist shell) 是一个比bash小很多但仍兼容POSIX标准的shell,它占用的磁盘空间更少,执行shell脚本比bash更快,依赖的库文件更少,当然,在功能上无法与bash相比。dash来自于NetBSD版本的Almquist Shell(ash)

    Ubuntu中将默认shell改为dash的主要原因是效率。由于Ubuntu启动过程中需要启动大量的shell脚本,为了优化启动速度和资源使用情况,Ubuntu做了这样的改动。


    如何避免dash引起的问题?

    1. 如果dash仅仅只影响到几个shell脚本,则最方便的解决方法是修改脚本,在脚本中指定正确的shell解释器

    #!/bin/sh  改为  #!/bin/bash

    2. 如果dash影响到的是Makefile文件,则需要在文件开始指定以下变量

    SHELL = /bin/bash

    3. 如果被影响的范围较广,以至于修改单独的几个文件无法解决问题,此时 可以终止将dash安装为/bin/sh

    sudo dpkg-reconfigure dash

    需要注意的是,这样修改将会影响到系统的启动速度,甚至会影响到一些 依赖于dash独有特性的脚本(这些特性bash没有提供)。


    如何在编写脚本时避免这些影响?

    首先,可以使用checkbashisms命令检查脚本是否是“bash化”(bashism)的。要使用checkbashisms,需先安装devscripts

    aptitude install devscripts

    checkbashisms test.sh

    其次,在编写脚本时需要注意使用符合POSIX标准的语法,从而使脚本成为"POSIX shell"
  • MySQL快速参考手册

    2010-05-31 16:33:39

    Selecting a database:

    mysql> USE database;

    Listing databases:

    mysql> SHOW DATABASES;

    Listing tables in a db:

    mysql> SHOW TABLES;

    Describing the format of a table:

    mysql> DESCRIBE table;

    Creating a database:

    mysql> CREATE DATABASE db_name;

    Creating a table:

    mysql> CREATE TABLE table_name (field1_name TYPE(SIZE), field2_name TYPE(SIZE));
    Ex: mysql> CREATE TABLE pet (name VARCHAR(20), sex CHAR(1), birth DATE);

    Load tab-delimited data into a table:

    mysql> LOAD DATA LOCAL INFILE "infile.txt" INTO TABLE table_name;
    (Use \n for NULL)

    Inserting one row at a time:

    mysql> INSERT INTO table_name VALUES ('MyName', 'MyOwner', '2002-08-31');
    (Use NULL for NULL)

    Retrieving information (general):

    mysql> SELECT from_columns FROM table WHERE conditions;
    All values: SELECT * FROM table;
    Some values: SELECT * FROM table WHERE rec_name = "value";
    Multiple critera: SELECT * FROM TABLE WHERE rec1 = "value1" AND rec2 = "value2";

    Reloading a new data set into existing table:

    mysql> SET AUTOCOMMIT=1; # used for quick recreation of table
    mysql> DELETE FROM pet;
    mysql> LOAD DATA LOCAL INFILE "infile.txt" INTO TABLE table;

    Fixing all records with a certain value:

    mysql> UPDATE table SET column_name = "new_value" WHERE record_name = "value";

    Selecting specific columns:

    mysql> SELECT column_name FROM table;

    Retrieving unique output records:

    mysql> SELECT DISTINCT column_name FROM table;

    Sorting:

    mysql> SELECT col1, col2 FROM table ORDER BY col2;
    Backwards: SELECT col1, col2 FROM table ORDER BY col2 DESC;

    Date calculations:

    mysql> SELECT CURRENT_DATE, (YEAR(CURRENT_DATE)-YEAR(date_col)) AS time_diff [FROM table];
    MONTH(some_date) extracts the month value and DAYOFMONTH() extracts day.

    Pattern Matching:

    mysql> SELECT * FROM table WHERE rec LIKE "blah%";
    (% is wildcard - arbitrary # of chars)
    Find 5-char values: SELECT * FROM table WHERE rec like "_____";
    (_ is any single character)

    Extended Regular Expression Matching:

    mysql> SELECT * FROM table WHERE rec RLIKE "^b$";
    (. for char, [...] for char class, * for 0 or more instances
    ^ for beginning, {n} for repeat n times, and $ for end)
    (RLIKE or REGEXP)
    To force case-sensitivity, use "REGEXP BINARY"

    Counting Rows:

    mysql> SELECT COUNT(*) FROM table;

    Grouping with Counting:

    mysql> SELECT owner, COUNT(*) FROM table GROUP BY owner;
    (GROUP BY groups together all records for each 'owner')

    Selecting from multiple tables:

    (Example)
    mysql> SELECT pet.name, comment FROM pet, event WHERE pet.name = event.name;
    (You can join a table to itself to compare by using 'AS')

    Currently selected database:

    mysql> SELECT DATABASE();

    Maximum value:

    mysql> SELECT MAX(col_name) AS label FROM table;

    Auto-incrementing rows:

    mysql> CREATE TABLE table (number INT NOT NULL AUTO_INCREMENT, name CHAR(10) NOT NULL);
    mysql> INSERT INTO table (name) VALUES ("tom"),("dick"),("harry");

    Adding a column to an already-created table:

    mysql> ALTER TABLE tbl ADD COLUMN [column_create syntax] AFTER col_name;

    Removing a column:

    mysql> ALTER TABLE tbl DROP COLUMN col;
    (Full ALTER TABLE syntax available at mysql.com.)

    Batch mode (feeding in a script):

    # mysql -u user -p < batch_file
    (Use -t for nice table layout and -vvv for command echoing.)
    Alternatively: mysql> source batch_file;

    Backing up a database with mysqldump:

    # mysqldump --opt -u username -p database > database_backup.sql
    (Use 'mysqldump --opt --all-databases > all_backup.sql' to backup everything.)
    (More info at MySQL's docs.)
  • umount时发生Device is busy的问题

    2010-05-27 13:39:43

    如果mount的 内容发生了变化,在访问mount内容时有可能出现僵死的情况,此时需要重新mount。

    mount -o remount dir

    将已mount的 只读状态的文件系统改为读写状态

    mount -o remount, rw dir

    该命令不会改变mount源 或mount点。


    umount
    在卸载设备时,有时会出现Device is busy的情况,导致umount进程一直僵在那里,无法继续。
    umount有两个选项可以尝试用来解决这个问题。

    umount -l /device  #Lazy umount. Detach the filesystem from the filesystem hierarchy now, and cleanup all references to the filesystem as soon as it is not busy anymore.
    umount -f /device  #Force unmount (in case of an unreachable NFS system)


    另外,fuser命 令在解决这种问题时很有用。

    fuser: identify processes using files or sockets
    fuser displays the PIDs of processes using the specified files or file systems.
    -m name specifies a file on a mounted file system or a block device that is mounted.
    fuser -km /home #kills all processes accessing the file system /home in any way.

  • mysql 数据库更改远程访问权限

    2010-05-27 13:19:38

    最近在调试mysql数据库时,发生以下错误

     

    18:04:45,575 ERROR [STDERR] org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: could not execute query; uncategorized SQLException for SQL [select productlib0_.servicelibvalueid as servicel1_7_, productlib0_.productid as productid7_, productlib0_.servicelibtypeid as servicel3_7_, productlib0_.servicelibid as servicel4_7_ from conferencemanagement.productlibtype productlib0_ where (productlib0_.productid=? )]; SQL state [HY000]; error code [1449]; There is no 'root'@'%' registered; nested exception is java.sql.SQLException: There is no 'root'@'%' registered

    18:04:45,577 ERROR [STDERR] Caused by:

    18:04:45,577 ERROR [STDERR] java.sql.SQLException: There is no 'root'@'%' registered

    18:04:45,578 ERROR [STDERR]         at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)

    18:04:45,578 ERROR [STDERR]         at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)

    18:04:45,578 ERROR [STDERR]         at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)

    18:04:45,578 ERROR [STDERR]         at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)

    18:04:45,578 ERROR [STDERR]         at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)

    18:04:45,578 ERROR [STDERR]         at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)

    18:04:45,578 ERROR [STDERR]         at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1467)


    排查错误,最后发现是数据库远程访问权限的问题,使用以下命令即可解决。

    1. 登录mysql:mysql -uroot -ppasswd

    2. 授权:GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION //授权任何主机均有访问数据库的权限。

    3. 重载权限信息:FLUSH PRIVILEGES //reload the grant tables

    4. 退出:quit

    另外,搜索到了其他修改方法,如下。

    修改mysql数据库的user表

    update user set host='%' where user = 'root';

    select host, user from user;

    有关mysql账户和权限管理的问题,可以参见以下文档

    http://dev.mysql.com/doc/refman/5.0/en/adding-users.html

  • Ubuntu系统如何安装双网卡及更改网卡名称(eth0改为eth1)

    2010-03-19 10:23:41

    最近,需要在Ubuntu系统上安装一块千兆网卡,使用到了一些网卡的安装、配置及更改操作。

    网卡可安装到任意一个PCI插槽,网卡型号D-Link DGE-530TUbuntu采用的是8.04 server版本。由于Ubuntu系统带有该网卡的驱动,因此,不必手动安装驱动。安装完成后进入系统,使用lspci查看是否有以下信息

    Ethernet controller: Intel Corporation 82562V-2 10/100 Network Connection

    Ethernet controller: D-Link System Inc DGE-530T Gigabit Ethernet Adapter

    其中,第一行所显示的网卡是系统原有的集成网卡,第二行所显示的网卡是新添加的D-Link网卡。如果仅有一行Ethernet信息,则说明新添加的网卡没有加载,需要手动安装驱动程序。

    新网卡添加后,即可对其进行配置。

    注意,此时新网卡的名称时eth1,因为eth0被原有的网卡占用。如果想将新网卡设置为eth0,则需要进行以下步骤。

    1ifconfig eth0ifconfig eth1查看网卡的MAC地址并记录,MAC地址主要用来区分两块网卡;

    2vi /etc/udev/rules.d/70-persistent-net.rules

    3)上面的文件内容与下面类似

    # This file was automatically generated by the /lib/udev/write_net_rules

    # program run by the persistent-net-generator.rules rules file.

    #

    # You can modify it, as long as you keep each rule on a single line.

     

    # PCI device 0x8086:0x10c0 (e1000e)

    SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:1a:a0:9d:f2:58", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

     

    # PCI device 0x1186:0x4b01 (skge)

    SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:26:5a:81:9f:e7", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

    其中,NAME="eth0"就是网卡的名称,该名称与MAC地址"00:1a:a0:9d:f2:58"对应。只要更改eth0eth1,就可以将MAC地址为"00:1a:a0:9d:f2:58"的网卡从eth0变成eth1.

    4)更改完后重启机器。

    如果无法分清百兆网卡和千兆网卡所对应的eth*,可以使用ethtool eth0ethtool eth1查看。如果ethtool eth0显示的信息类似与下面

    Settings for eth0:

    Supported ports: [ TP ]

    Supported link modes:   10baseT/Half 10baseT/Full

                            100baseT/Half 100baseT/Full

                            1000baseT/Half 1000baseT/Full

    Supports auto-negotiation: Yes

    Advertised link modes:  10baseT/Half 10baseT/Full

                            100baseT/Half 100baseT/Full

                            1000baseT/Half 1000baseT/Full

    说明eth0是千兆网卡,如果ethtool eth0显示的信息类似与下面

    Settings for eth0:

    Supported ports: [ TP ]

    Supported link modes:   10baseT/Half 10baseT/Full

                            100baseT/Half 100baseT/Full

    Supports auto-negotiation: Yes

    Advertised link modes:  10baseT/Half 10baseT/Full

                            100baseT/Half 100baseT/Full

    说明eth0是百兆网卡。

    对网络进行设置,可以编辑vi /etc/network/interfaces,类似与下

    # This file describes the network interfaces available on your system

    # and how to activate them. For more information, see interfaces(5).

    # The loopback network interface

    auto lo

    iface lo inet loopback

    auto eth0    #设置eth0

    iface eth0 inet static

        address 192.168.37.76

        netmask 255.255.255.0

        network 192.168.37.0

        broadcast 192.168.37.255

        gateway  192.168.37.254

        dns-nameservers 192.168.37.254

     

    #auto eth1    #设置eth1

    #iface eth1 inet static

    #address xxx

     

    #auto eth0:1    #单网卡设置多个IP

    #iface eth0:1 inet static

    #address 192.168.1.60

    #netmask 255.255.255.0

    #network x.x.x.x

    #broadcast x.x.x.x

    #gateway x.x.x.x

    设置完后重启网络

    /etc/init.d/networking restart
Open Toolbar