Java operate file or system between different service

 

设想:能不能在不同服务器之间文件传输,或是执行操作系统的命令的方式来操作服务器?

答案:当然可以

 

首先,在这里介绍一下不用密码怎么去访问别的service。(这是后续操作的基础)

 

 

要让 HostB 允许HostA 上的用户User1 进行操作,需要:

 

1. HostA 上以 User1 登陆,或 su - User1

2. 用ssh-keygen 命令,来生成HostA 的密钥对文件

ssh-keygen -t rsa 

生成目录:/home/User1/.ssh/

id_rsa 是私钥文件

id_rsa.pub 是公钥文件

3. 修改公钥文件属性

chmod 755 /home/User1/.ssh/id_rsa.pub

4. 将 HostA 上的公钥内容 id_rsa.pub 添加到 HostB 上  

如果HostB上没有  /home/User1/.ssh 目录或authorized_keys 则需要新建

将HostA 的 id_rsa.pub 文件内容,添加到 /home/User1/.ssh/authorized_keys 文件中

5. 测试:(注意需要以 User1 登陆)

scp  1.txt UserB@HostB:/****/xxxx/

 

在不需要密码的情况下能把1.txt文件从HostA  copy  至HostB,那么,该配置设置ok。

 

补充:有可能出现按照上述设置,任然不可以的现象,做如下操作:

在HostA上

chmod 700 .ssh

chmod 600 ./ssh/authorized_keys

 

 

其次,网上有很多java实现的ssh,比如jsch等。我们在这里介绍两种底层的操作,更加简明、高效

 

1. Runtime.getRuntime().exec()

 

我想大家对这个不陌生吧,Runtime这个东西很强大,只要是你的操作系统能够完成的,它都能完成。比如linux系统中我们常用的scp命令,跨服务操作

Runtime.getRuntime().exec("scp -r /###/dir  user@HostB:/dir") ;这样就可以把你的服务器上的文件copy至HostB上。强大吧,当然该功能需要借助于上述说的公钥文件的配置。至于采用scp来copy file和directory的具体参数就不多说了。

 

举例:

(1) 用Runtime.getRuntime().exec 打开文件(windows) Runtime.getRuntime().exec("cmd.exe /c start E://1.xlsx") ;

 

(2) Runtime.getRuntime().exec执行结果:

 

Process prs =  Runtime.getRuntime().exec("cmd.exe /c start E://test//1.xlsx") ; 

prs.waitFor() ;

int i = prs.exitValue() ;// i 即为执行结果

prs.destroy() ;

 

当然,在用完该方法后,最好Process prs =  Runtime.getRuntime().exec("") ;  ......  prs.destroy() ;

 

 

 

2. ganymed-ssh2-build210.jar包提供了很多从方法

(API:http://www.ganymed.ethz.ch/ssh2/javadoc/index-all.html#_R_)它的sess.execCommand(command) ;方法依旧能完成上述scp的功能。

 

这两者的共同之处在于它们都是在执行操作系统底层的东西,简洁、高效、非常强大。不仅仅是我在上面介绍的scp的功能,比如删除目录以及该目录下所有的目录&文件呢?rm -rf /###/drectory ......

 

哈哈,大家也许看明白了,只要是你的操作系统所能执行的命令,这里都可以帮你完成,要让我用一句话评价,那就是:强大!

 

 

 

### Event ID 7023 Service Control Manager Description Event ID 7023 in the context of the Windows Service Control Manager indicates that a specific service failed to start or stopped unexpectedly. The event log entry typically includes details about which particular service encountered an issue and any associated error messages. The general format for this event is as follows: "The **{service name}** service terminated with the following error: %%{error code}" This message signifies that there was a problem preventing the specified service from running correctly, leading it either not starting up properly when requested or shutting down prematurely without proper shutdown procedures being completed[^1]. ### Common Causes and Solutions For resolving issues related to Event ID 7023, several common causes exist along with their respective solutions: #### Misconfigured Services Services might fail due to misconfiguration such as incorrect paths set within registry settings where executable files are located. - Verify all configurations through `services.msc` ensuring correct startup parameters including path names pointing towards valid executables. #### Corrupted Executable Files Corruption occurring inside binaries responsible for executing services could lead them into failing states during initialization phases. - Reinstall software packages tied directly back onto affected services; doing so replaces potentially damaged components while also updating others alongside security patches provided by vendors. #### Dependency Issues Some services rely on other underlying ones functioning before they themselves can operate successfully—if dependencies aren’t met then failures occur under these circumstances too. - Use tools like Process Monitor (ProcMon) offered freely via Sysinternals suite developed originally outside Microsoft but now part officially since acquisition—to trace dependency chains between different processes identifying missing links causing disruptions observed here today. ```powershell Get-WinEvent -LogName System | Where-Object { $_.Id -eq 7023 } | Format-List TimeCreated, Message ``` --related questions-- 1. How does one identify dependent services using command line utilities? 2. What steps should be taken if reinstallation doesn't resolve corrupted executable file problems? 3. Can you provide more detailed instructions on utilizing ProcMon for troubleshooting service dependencies? 4. Is there a way to automate checking and fixing configuration errors in Windows services?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值