Shell 将 rpm scp 到远程服务器并安装的脚本。需要一个变量来传递远程主机名
Shell script to scp rpm to remote server and install it. Needs a variable to pass remote host name
我正在尝试编写一种菜单脚本,执行后将实现以下目标:
- 问问你user/password你以什么身份执行脚本。
- 询问远程服务器i.p。你想将文件 scp 到。
- 询问本地存放文件的目录
- 询问所有文件要转移到的目录。
- 复制所有文件。
我遇到的第一个障碍是将密码作为变量存储在菜单中。我认为 sshpass 对此很有帮助。我想配置这样的菜单:
title="Select example"
prompt="Pick an option:"
options=("A" "B" "C")
echo "$title"
PS3="$prompt "
select opt in "${options[@]}" "Quit"; do
case "$REPLY" in
1 ) echo "You picked $opt which is option $REPLY";;
2 ) echo "You picked $opt which is option $REPLY";;
3 ) echo "You picked $opt which is option $REPLY";;
$(( ${#options[@]}+1 )) ) echo "Goodbye!"; break;;
*) echo "Invalid option. Try another one.";continue;;
esac
done
但是菜单会要求您输入用户名、文件的本地目录,i.p。远程服务器,远程服务器目录,然后 运行 它构造的 scp 命令。
像这样:
password="your password"
username="username"
Ip="<IP>"
sshpass -p "$password" scp /<PATH>/final.txt $username@$Ip:/root/<PATH>
但是对于如何将所有这些放在一起以便菜单收集所需的信息然后执行收集的输入以便它简单地构造一个 scp 命令来触发我有点困惑。
谁能提供这方面的经验来帮助我构建菜单脚本?
谢谢!
您不需要菜单,只需依次询问每个输入即可。
read -p "Username:" username
read -s -p "Password:" password
read -p "Server IP:" ip
read -p "Local directory:" local
read -p "Remote directory" remote
sshpass -p "$password" scp "/$local/final.txt" "$username@$ip:/root/$remote/"
如果您还需要这里的菜单。我稍微修改了你的脚本,使其成为 construct/print scp 命令,同时一个一个地输入值。
#!/bin/bash
title="Select example"
prompt="Pick an option:"
declare -A options # turn options to an associative array
options[1]='change user name'
options[2]='change user password'
options[3]='change remote host address'
options[4]='change path to source files'
options[5]='change destination path on remote server'
PS3="$prompt "
menu () { # wrap all this to a function to restart it after edition of an element
echo "$title"
select opt in "${options[@]}" "Quit"; do
case "$REPLY" in
1 ) read -p "${options[1]}: " user;;
2 ) read -sp "${options[2]}: " pass;; # read password with option -s to disable output
3 ) read -p "${options[3]}: " addr;;
4 ) read -p "${options[4]}: " from;;
5 ) read -p "${options[5]}: " dest;;
$(( ${#options[@]}+1 )) ) echo "Goodbye!" ; exit;;
*) echo "Invalid option. Try another one.";;
esac
clear # clear screen to remove inputs
printf "Creating scp command:\n"
# we don't want to show password, print *** if password set and 'password not set' if not
[[ $pass ]] && pass2='***' || pass2='[password not set]'
# text in [] will be printed if var is empty or not set, it's usefull to add default values to vars ${var:-default_value}
printf "sshpass -p $pass2 scp -r ${from:-[source not set]}/* ${user:-[user not set]}@${addr:-[host not set]}:${dest:-[destination not set]}/\n\n"
menu
done
}
clear # clear screen
menu # and start menu function
还有here我有一个功能类似(但更丰富)的脚本,请看看。
我正在尝试编写一种菜单脚本,执行后将实现以下目标:
- 问问你user/password你以什么身份执行脚本。
- 询问远程服务器i.p。你想将文件 scp 到。
- 询问本地存放文件的目录
- 询问所有文件要转移到的目录。
- 复制所有文件。
我遇到的第一个障碍是将密码作为变量存储在菜单中。我认为 sshpass 对此很有帮助。我想配置这样的菜单:
title="Select example"
prompt="Pick an option:"
options=("A" "B" "C")
echo "$title"
PS3="$prompt "
select opt in "${options[@]}" "Quit"; do
case "$REPLY" in
1 ) echo "You picked $opt which is option $REPLY";;
2 ) echo "You picked $opt which is option $REPLY";;
3 ) echo "You picked $opt which is option $REPLY";;
$(( ${#options[@]}+1 )) ) echo "Goodbye!"; break;;
*) echo "Invalid option. Try another one.";continue;;
esac
done
但是菜单会要求您输入用户名、文件的本地目录,i.p。远程服务器,远程服务器目录,然后 运行 它构造的 scp 命令。
像这样:
password="your password"
username="username"
Ip="<IP>"
sshpass -p "$password" scp /<PATH>/final.txt $username@$Ip:/root/<PATH>
但是对于如何将所有这些放在一起以便菜单收集所需的信息然后执行收集的输入以便它简单地构造一个 scp 命令来触发我有点困惑。
谁能提供这方面的经验来帮助我构建菜单脚本?
谢谢!
您不需要菜单,只需依次询问每个输入即可。
read -p "Username:" username
read -s -p "Password:" password
read -p "Server IP:" ip
read -p "Local directory:" local
read -p "Remote directory" remote
sshpass -p "$password" scp "/$local/final.txt" "$username@$ip:/root/$remote/"
如果您还需要这里的菜单。我稍微修改了你的脚本,使其成为 construct/print scp 命令,同时一个一个地输入值。
#!/bin/bash
title="Select example"
prompt="Pick an option:"
declare -A options # turn options to an associative array
options[1]='change user name'
options[2]='change user password'
options[3]='change remote host address'
options[4]='change path to source files'
options[5]='change destination path on remote server'
PS3="$prompt "
menu () { # wrap all this to a function to restart it after edition of an element
echo "$title"
select opt in "${options[@]}" "Quit"; do
case "$REPLY" in
1 ) read -p "${options[1]}: " user;;
2 ) read -sp "${options[2]}: " pass;; # read password with option -s to disable output
3 ) read -p "${options[3]}: " addr;;
4 ) read -p "${options[4]}: " from;;
5 ) read -p "${options[5]}: " dest;;
$(( ${#options[@]}+1 )) ) echo "Goodbye!" ; exit;;
*) echo "Invalid option. Try another one.";;
esac
clear # clear screen to remove inputs
printf "Creating scp command:\n"
# we don't want to show password, print *** if password set and 'password not set' if not
[[ $pass ]] && pass2='***' || pass2='[password not set]'
# text in [] will be printed if var is empty or not set, it's usefull to add default values to vars ${var:-default_value}
printf "sshpass -p $pass2 scp -r ${from:-[source not set]}/* ${user:-[user not set]}@${addr:-[host not set]}:${dest:-[destination not set]}/\n\n"
menu
done
}
clear # clear screen
menu # and start menu function
还有here我有一个功能类似(但更丰富)的脚本,请看看。