修改Windows和linux系统时间

简介:

1、修改本机Windows的系统时间,Java代码实现:

复制代码
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ChangeWindowsDate {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) {
        try {
            Runtime.getRuntime().exec("cmd /c date 2013-11-08") ;
            Runtime.getRuntime().exec("cmd /c time 18:10:00") ;
        } catch (IOException e) {
            e.printStackTrace() ;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
        Date date = new Date() ;
        System.out.println(sdf.format(date));
        
    }

}
复制代码

2、修改远程Linux Server的系统时间,Java代码实现:

复制代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;



public class ChangeLinuxDate {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String host_ip1 = "192.168.1.118" ;
        int port = 22 ;
        String username = "root" ;
        String password = "123456" ;        
        
        String cmd = "date -s '2013-08-04 23:00:00'" ;
        
        Connection conn1 = new Connection(host_ip1, port) ;
        Session session1 = null ;
        
        try {
            conn1.connect() ;
            boolean isconn = conn1.authenticateWithPassword(username, password) ;
            if(!isconn){
                System.out.println("用户名称或者是密码不正确");
            }else{
                System.out.println(host_ip1 + ":" + "已经连接OK");
                session1 = conn1.openSession() ;
                session1.execCommand(cmd) ;

                InputStream is = new StreamGobbler(session1.getStdout());
                BufferedReader brs = new BufferedReader(new InputStreamReader(is));
                while(true){  
                    String line = brs.readLine();  
                    if(line==null){  
                        break;  
                    }  
                    System.out.println(line);  
                }
                is.close() ;
                brs.close() ;
                session1.close() ;
                conn1.close() ;
            }
            
        } catch (IOException e) {
            e.printStackTrace() ;
        }
        
    }

}
复制代码

 








本文转自一米一阳光博客园博客,原文链接:http://www.cnblogs.com/candle806/p/3257044.html    ,如需转载请自行联系原作者



相关文章
|
4月前
|
安全 Linux 生物认证
Nexpose 8.25.0 for Linux & Windows - 漏洞扫描
Nexpose 8.25.0 for Linux & Windows - 漏洞扫描
197 0
Nexpose 8.25.0 for Linux & Windows - 漏洞扫描
|
4月前
|
安全 Linux iOS开发
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
497 53
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
|
4月前
|
安全 Linux 生物认证
Nexpose 8.24.0 for Linux & Windows - 漏洞扫描
Nexpose 8.24.0 for Linux & Windows - 漏洞扫描
263 17
Nexpose 8.24.0 for Linux & Windows - 漏洞扫描
|
4月前
|
Linux API iOS开发
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
340 14
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
|
4月前
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
253 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
4月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
357 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
5月前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
394 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
|
5月前
|
JSON 安全 Linux
Nexpose 8.22.0 for Linux & Windows - 漏洞扫描
Nexpose 8.22.0 for Linux & Windows - 漏洞扫描
206 1
Nexpose 8.22.0 for Linux & Windows - 漏洞扫描
|
5月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
464 1
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
|
4月前
|
Linux 虚拟化 iOS开发
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
945 0
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端