• 设备
    • 今日
    • 6

    获取远程服务器日期

    获取远程服务器日期

    1. 实现免密码登录(ssh远程客户端)

    通过命令”ssh-keygen -t rsa“
    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa):  回车
    Enter passphrase (empty for no passphrase):               回车
    Enter same passphrase again:                              回车
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    The key fingerprint is:
    40:98:85:aa:90:b9:ed:3e:d7:44:ee:75:4a:0e:21:d7 root@VM-29cad895-11da-49b7-a970-27c763f52c29
    The key's randomart image is:
    +--[ RSA 2048]----+
    |     =o          |
    |    +.            |
    | o .  ..          |
    |+ . . +.E        |
    |.+   = .S        |
    |o .   + o .      |
    | .   + = o       |
    |  o . o o        |
    | ..o             |
    +-----------------+

    生成之后会在用户的根目录生成一个 “.ssh”的文件夹

    进入/root/.ssh/会生成以下几个文件
      authorized_keys:存放远程免密登录的公钥,主要通过这个文件记录多台机器的公钥  
      id_rsa : 生成的私钥文件
      id_rsa.pub : 生成的公钥文件
      know_hosts : 已知的主机公钥清单

    #复制公钥id_rsa.pub到172.16.0.3 的当前用户的.ssh目录下,同时输入172.16.0.3的root密码(本机)
    ssh-copy-id -i ~/.ssh/id_rsa.pub 172.16.0.3

    scp -p ~/.ssh/id_rsa.pub root@登录端IP:/root/.ssh/authorized_keys 

    -bash: ssh-copy-id: command not found //提示命令不存在
    解决办法:yum -y install openssh-clients

    2. 定时执行sh文件,生成各台服务器日期的txt文件
    crontab -e 
    * * * * * root /appstart/showdate.sh > /tmp/a.txt 2>&1  
    * * * * * root sh /appstart/sd.sh                      //每天每分钟执行一次/appstart/sd.sh
    service crond restart 
    使用命令crontab -u root -l 可以查看当前定时任务
     tail -1000f /var/log/cron //查看定时日志


    3. 通过ssh IP date  获取远程服务器日期

    #!/bin/sh

    . /etc/profile
    echo -n  "MainServer 172.16.0.183 Date:"
    echo "`date +'%Y-%m-%d-%H:%M:%S'`"
    echo "---------------------------------------------------------"
    echo -n  "1. Server 172.16.0.176 Date: "& ssh 172.16.0.167 date "+%Y-%m-%d-%H:%M:%S"
    echo "---------------------------------------------------------"
    echo -n  "2. Server 172.16.0.012 Date: "& ssh 172.16.0.12 date "+%Y-%m-%d-%H:%M:%S"
    echo "---------------------------------------------------------"
    echo -n  "3. Server 172.16.0.165 Date: "& ssh 172.16.0.165 date "+%Y-%m-%d-%H:%M:%S"
    echo "---------------------------------------------------------"
    echo -n  "4. Server 172.16.0.171 Date: "& ssh 172.16.0.171 date "+%Y-%m-%d-%H:%M:%S"
    echo "---------------------------------------------------------"
    echo -n  "5. Server 172.16.0.222 Date: "& ssh 172.16.0.222 date "+%Y-%m-%d-%H:%M:%S"
    echo "---------------------------------------------------------"
    echo -n  "6. Server 172.16.0.227 Date: "& ssh 172.16.0.227 date "+%Y-%m-%d-%H:%M:%S"
    echo "---------------------------------------------------------"
    echo -n  "7. Server 172.16.0.003 Date: "& ssh 172.16.0.3 date "+%Y-%m-%d-%H:%M:%S"
    echo "---------------------------------------------------------"
    echo -n  "8. Server 172.16.0.077 Date: "& ssh 172.16.0.77 date "+%Y-%m-%d-%H:%M:%S"
    echo "--------------------------------------------------------"
    echo -n  "9. Server 172.16.0.086 Date: "& ssh 172.16.0.86 date "+%Y-%m-%d-%H:%M:%S"
    echo "---------------------------------------------------------"
    echo -n  "10.Server 172.16.0.090 Date: "& ssh 172.16.0.90 date "+%Y-%m-%d-%H:%M:%S"
    echo "---------------------------------------------------------"
    echo -n  "11.Server 172.16.0.099 Date: "& ssh 172.16.0.99 date "+%Y-%m-%d-%H:%M:%S"
    来自:PC 广东省广州市
    上一篇: php格式化输出txt文件内容
    您可能还喜欢这些:

    亲,沙发正空着,还不快来抢?

    评论审核已开启 记住我的个人信息 回复后邮件通知我