summaryrefslogtreecommitdiffstats
path: root/deploy/trustme.sh
blob: 1a599be6eba8d5b73fbda72222789e3249dde23e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/sh
##############################################################################
# Copyright (c) 2018 ZTE Corporation and others.
# hu.zhijiang@zte.com.cn
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

#to be trusted by other host and no password needed when use ssh command

#check parameters legality
logfile=/var/log/trustme.log
function print_log
{
   local promt="$1"
   echo -e "$promt"
   echo -e "`date -d today +"%Y-%m-%d %H:%M:%S"`  $promt" >> $logfile
}
ip=$1
if [ -z $ip ]; then
  print_log "Usage: `basename $0` ipaddr passwd"
  exit 1
fi

passwd=$2
if [ -z $passwd ]; then
  print_log "Usage: `basename $0` ipaddr passwd"
  exit 1
fi

rpm -qi sshpass >/dev/null
if [ $? != 0 ]; then
  print_log "Please install sshpass first"
  exit 1
fi

#ping other host
unreachable=`ping $ip -c 1 -W 3 | grep -c "100% packet loss"`
if [ $unreachable -eq 1 ]; then
  print_log "host $ip is unreachable"
  exit 1
fi

#generate ssh pubkey
if [ ! -e ~/.ssh/id_dsa.pub ]; then
  print_log "generating ssh public key ..."
  ssh-keygen -t dsa -f ~/.ssh/id_dsa -N "" <<EOF
n
EOF
  if [ $? != 0 ]; then
    print_log "ssh-keygen failed"
    exit 1
  fi
fi

#clear old public key
print_log "clear old info in known_hosts file on localhost ..."
test ! -f ~/.ssh/known_hosts || ssh-keygen -R $ip
if [ $? != 0 ]; then
  print_log "clear old info in known_hosts file on localhost failed"
  exit 1
fi

#copy new public key
print_log "copy my public key to $ip ..."
sshpass -p $passwd ssh-copy-id -i ~/.ssh/id_dsa.pub -o StrictHostKeyChecking=no root@$ip
if [ $? != 0 ]; then
    print_log "ssh-copy-id failed"
    exit 1
fi

print_log "trustme ok!"