SSH 명령에 비밀번호를 제공하기 위해 bash 스크립트에서 expect 사용
SSH 키를 사용해야한다고 응답하려는 사람들은 기권하십시오.
SSH 암호를 제공하기 위해 bash 스크립트에서 expect를 사용하려고합니다. 암호를 제공하지만 SSH 세션에서 끝나지 않으면 해협으로 bash로 돌아갑니다.
내 스크립트 :
#!/bin/bash
read -s PWD
/usr/bin/expect <<EOD
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com'
expect "password"
send "$PWD\n"
EOD
echo "you're out"
내 스크립트의 출력 :
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com
usr@$myhost.example.com's password: you're out
SSH 세션을 갖고 싶습니다. 내가 bash 스크립트로 돌아 가기 위해 종료해야합니다. 예상보다 bash를 사용하는 이유는 메뉴를 사용하여 연결할 장치를 선택할 수 있기 때문입니다.
감사
배쉬와 기대를 혼합하는 것은 원하는 효과를 얻는 좋은 방법이 아닙니다. Expect 만 사용하려고합니다.
#!/usr/bin/expect
eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com
#use correct prompt
set prompt ":|#|\\\$"
interact -o -nobuffer -re $prompt return
send "my_password\r"
interact -o -nobuffer -re $prompt return
send "my_command1\r"
interact -o -nobuffer -re $prompt return
send "my_command2\r"
interact
bash의 샘플 솔루션은 다음과 같습니다.
#!/bin/bash
/usr/bin/expect -c 'expect "\n" { eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com; interact }'
이것은 대화식 세션으로 들어가기를 기다리는 것입니다.
가장 쉬운 방법은 sshpass 를 사용하는 것 입니다. 이것은 Ubuntu / Debian repos에서 사용할 수 있으며 bash와 expect 통합을 처리 할 필요가 없습니다.
예를 들면 :
sshpass -p<password> ssh <arguments>
sshpass -ptest1324 ssh user@192.168.1.200 ls -l /tmp
위의 명령은 bash 스크립트와 쉽게 통합 될 수 있습니다.
참고 : 보안 관련 사항man sshpass
을 완전히 이해하려면 보안 고려 사항 섹션을 읽으십시오 .
EOD 바로 앞에 'interact'expect 명령을 추가하십시오.
#!/bin/bash
read -s PWD
/usr/bin/expect <<EOD
spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com
expect "password"
send "$PWD\n"
interact
EOD
echo "you're out"
로그 아웃 할 때까지 원격 시스템과 상호 작용할 수 있어야합니다. 그런 다음 배쉬로 돌아옵니다.
몇 달 동안 질문에 대한 답을 찾은 후에 마침내 간단한 스크립트 작성이라는 가장 좋은 해결책을 찾았습니다.
#!/usr/bin/expect
set timeout 20
set cmd [lrange $argv 1 end]
set password [lindex $argv 0]
eval spawn $cmd
expect "assword:"
send "$password\r";
interact
에 넣으면 /usr/bin/exp
다음을 사용할 수 있습니다.
exp <password> ssh <anything>
exp <password> scp <anysrc> <anydst>
끝난!
또한 사용하십시오
send -- "$PWD\r"
대신 대시 (-)로 시작하는 비밀번호는 실패합니다.
The above wont interpret a string starting with a dash as an option to the send command.
A simple expect script
Remotelogin.exp
#!/usr/bin/expect
set user [lindex $argv 1]
set ip [lindex $argv 0]
set password [lindex $argv 2]
spawn ssh $user@$ip
expect "password"
send "$password\r"
interact
Example:
./Remotelogin.exp <ip> <user name> <password>
Use the helper tool fd0ssh
(from hxtools, not pmt), it works without having to expect a particular prompt from the ssh program.
Another way that I found useful to use a small expect script from a bash script is as follows.
...
bash-script start
bash-commands
...
expect - <<EOF
spawn your-command-here
expect "some-pattern"
send "some-command"
...
...
EOF
...
more bash commands
...
This works because ...If the string "-" is supplied as a filename, standard input is read instead...
sshpass
is broken if you try to use it inside Sublime Text build target, inside a Makefile. Instead of sshpass
you can use passh
: https://github.com/clarkwang/passh
With sshpass
you would do:
sshpass -p ssh user@host
With passh
you would do:
passh -p ssh user@host
Note: Do not forget to use -o StrictHostKeyChecking=no
, otherwise the connection will hang on the first time you use it. For example:
passh -p ssh -o StrictHostKeyChecking=no user@host
References:
- Send command for password doesn't work using expect script in ssh connection
- https://askubuntu.com/questions/87449/how-to-disable-strict-host-key-checking-in-ssh
- https://linuxcommando.blogspot.com/2008/10/how-to-disable-ssh-host-key-checking.html
- https://serverfault.com/questions/330503/scp-without-known-hosts-check
- https://debian-administration.org/article/587/pam_mount_and_sshfs_with_password_authentication
'Programming' 카테고리의 다른 글
파이썬에서 상속 및 __init__ 재정의 (0) | 2020.07.11 |
---|---|
파이썬에서 상속 및 __init__ 재정의 (0) | 2020.07.11 |
SQL (MySQL) 및 NoSQL (CouchDB) (0) | 2020.07.11 |
테이블 행 높이 설정 (0) | 2020.07.11 |
Node.js에서 사용하기위한 간단한 Javascript DB (0) | 2020.07.11 |