Linux Shell腳本實現自動交互

方法一:(以OpenSSH編譯完成之後的 拷貝和生成key 爲例)

#!/bin/bash

DIR_PATH=$HOME/sshdfile

cp sshd $DIR_PATH/sbin

cp scp sftp ssh ssh-add ssh-agent ssh-keygen ssh-keyscan $DIR_PATH/bin
cp sftp-server ssh-keysign $DIR_PATH/libexec
cp sshd_config ssh_config $DIR_PATH/etc

chmod 755 $DIR_PATH/var/empty

ssh-keygen -t rsa1 -f ssh_host_key -N "" << EOF
y
EOF

ssh-keygen -t rsa -f ssh_host_rsa_key -N "" << EOF
y
EOF

ssh-keygen -t dsa -f ssh_host_dsa_key -N "" << EOF
y
EOF

cp ssh_host_* $DIR_PATH/etc

方法二:(以FTP用戶自動登錄爲例)

#!/usr/bin/expect
set file [lindex $argv 0];
set timeout -1;
  spawn ftp 192.168.1.***
  expect "Name"
  send "username***\r"
  expect "Password"
  send "passwd***\r"
expect "ftp>"
send "hash\r"
expect "*ftp>*"
send "put $file\n"
expect "*ftp>*"
send "bye\r"
expect "*Goodbye*"
expect eof
exit
【測試】:./autoftp.sh filename
【效果】:自動登陸FTP用戶”username***“, 將”filename“ 文件上傳至服務器。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章