LoginSignup
0
3

More than 5 years have passed since last update.

scpのaliasを作ろう!

Posted at

踏み台サーバなどにscpコマンドでファイルを配置したい時に、毎回サーバのIP等を入力するのがめんどくさいなと思ったこの頃。
scpのエイリアスを作成して、コマンド引数としてファイルを指定するだけで所定のサーバー・パスにファイルをおけるようにしようと思いました

aliasを作って実現しようとしたのですが、エイリアスに対して引数を上手く渡せない
ということで以下のように関数を使うと良いらしいです

$ alias my-echo="echo $1" # ダメ
$ my-echo() { echo $1}      # これはOK

というわけで、bash_profile等に以下のような記述を追加

# scp用の関数を定義
scp-gate(){scp $1 username@example.com:~/}

上記のようにした所、以下のようなエラーがでて上手くscpすることができませんでした

$ scp-gate fileA
scp: /home/username: not a regular file

これはsshの設定が読み込まれていないためらしいです
そこで、sshのconfigファイルを編集して、ドメインと設定を反映させましょう

まずはsshの設定から

~/.ssh/config
Host gate↲
   HostName example.com
   User username
   Port 22

そして、aliasに相当する関数もドメイン指定するように修正

# 踏み台コピー
scp-gate(){scp $1 gate:~/}

再度実行!!

$ scp-gate fileA
ishibashi@10.134.0.21's password: 
fileA                                                                                                             100%  488     0.5KB/s   00:00 

無事scpのエイリアスっぽいものができました。めでたしめでたし。

参考文献

bashのalias に引数を渡すには?
Unable to use scp with a bash alias

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3