0

I am a beginner in these things, so please go light on me!

At the suggestion of a community member, I ask this question here, in addition to StackOverflow.

I have a device which I connect to my PC (Windows machine) via USB and after rooting SSH SSH-ing into it as an user, I can change the date and time using the following command:

date -s '2019-08-21 05:12:44'

I can obviously do that by hand, but I would like to learn how can I bring the Windows machine time into that command line.

Thank you very much!

Edit: It is a custom built hardware which runs Linux and it has 3 real-time clocks. I connect it to the PC via USB cable, but it has builtin Bluetooth and WiFi capabilities.

Second edit: I wanted to say that I SSH into the device as the "root" user.

Using Python with the Paramiko library I managed to bring the Windows machine time into the Linux one with the following code:

import datetime
import paramiko
import time

#create SSH client

client = paramiko.SSHClient()

#automatically add the host key

client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

#connect to the Linux device

client.connect('address', username='username', password='password')

#delay subsequent commands by 10s to be sure the connection is established

time.sleep(10)

#execute command

stdin, stdout, stderr = client.exec_command(str(str("date -s '" + str(datetime.datetime.now()))[:-7] + "'")) #bring in the local Windows machine time
stdin.close()

print(stdout.read(),)

Strangely enough, if I don't use the following line of code, it doesn't work:

stdin.close()

Thank you very much for your help!

2
  • superuser.com/a/702751/950948 construct time string in env variable on local host and use it in remote command but time not updated in your variable magically - you need to update variable on host each time before executing command on remote. In other words you cant get actual local time from remote via ssh and shell.
    – gapsf
    Sep 30, 2022 at 3:42
  • (1) "after rooting SSH into it" -- So there's network between the Windows PC and the device, right? (2) What OS runs on the device? Is there a real-time clock? Is the device Raspberry Pi? (3) To "bring the Windows machine time into that command line" would be a poor solution. A better solution may be to run an NTP server on Windows and an NTP client on the device automatically, but we need more information from you to guide you. Please edit the question and tell us more. Responding to my questions is a good start. Do not respond in comments, add information to the question body. Sep 30, 2022 at 6:10

0

You must log in to answer this question.

Browse other questions tagged .