美文网首页
js 里面咋 ping

js 里面咋 ping

作者: 焜_8899 | 来源:发表于2023-11-28 16:07 被阅读0次
    // syscall.js
    
    const util = require('util');
    const exec = util.promisify(require('child_process').exec);
    
    export const ping = (event, target) => {
        const ipv4Pattern = /((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}/;  // IPv4
        const domainNamePattern = /[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/;       // 域名
    
        if ((!ipv4Pattern.test(target)) && (!domainNamePattern.test(target))) {
            return Promise.reject(new Error(
                target.toString() + ' is neither a valid IPv4 address nor a valid domain name.'
            ));
        }
    
        return exec('ping ' + target);
    };
    
    
    // syscall.test.js
    
    import { ping } from "../../utils/syscall";
    
    describe('Syscall', () => {
        it.concurrent('ping valid ip success', async () => {
            await ping(undefined, '127.0.0.1')
                .then((stdout, stderr) => {
                    expect(stderr).toBeUndefined();
                    expect(stdout).toBeDefined();
                })
                .catch(() => {
                    expect(false).toBe(true);
                });
        });
    });
    
    

    相关文章

      网友评论

          本文标题:js 里面咋 ping

          本文链接:https://www.haomeiwen.com/subject/ykjwwdtx.html