Welcome, guest | Sign In | My Account | Store | Cart

awk with parameters passed from bash

Bash, 65 lines
 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
#!/bin/bash

hostname=${1:?"host name?"}
regexp="(.*):(.*):(.*)"

#set the sshdb data to be default if the database is unspecified
default_sshdb="/home/jing/sshserver/sshdb"
sshdb=${2:-$default_sshdb}

#read the host to be connected
account=$(awk -v host=$hostname '
/^[^#].*/ { 
    if ( $1 ~ host ) {
     printf $2 ":"$3 ":"$4
        exit
    }
}
' $sshdb)

if [[ $account =~ $regexp ]];then
    ip=${BASH_REMATCH[1]}
    username=${BASH_REMATCH[2]}
    password=${BASH_REMATCH[3]}
fi

#make the connection
/usr/bin/expect -f /home/jing/sshserver/expectedssh $username $ip $password











==================================================================
jing@jing-laptop:~$ cat sshserver/expectedssh 
set UserID "[lrange $argv 0 0]"
set Passphrase "[lrange $argv 2 2]"
set remotehost "[lrange $argv 1 1]"

set timeout 30

spawn ssh $UserID@$remotehost
expect -re ".*password:"
send "$Passphrase\r"
expect -re ".+"
interact







==================================================================
#this is the database for ssh connection using expect
#connection-name host username password, delimited by a single space

25 172.16.100.25 liujingyuan aaaaa
26 172.16.100.26 liujingyuan aaaaa
106 172.16.100.106 root root12

2 comments

J Y (author) 12 years, 3 months ago  # | flag

the expectedssh script

set UserID "[lrange $argv 0 0]" set Passphrase "[lrange $argv 2 2]" set remotehost "[lrange $argv 1 1]"

spawn ssh $UserID@$remotehost expect -re ".*password:" send "$Passphrase\r" expect -re ".+" interact exit

J Y (author) 12 years, 3 months ago  # | flag

the sshdb file

this is the database for ssh connection using expect

connection-name host username password, delimited by a single space

25 172.16.100.25 liujingyuan aaaaa 26 172.16.100.26 liujingyuan aaaaa

Created by J Y on Wed, 12 Aug 2009 (MIT)
Bash recipes (41)
J Y's recipes (21)

Required Modules

  • (none specified)

Other Information and Tasks