From 2a465e98f3c7b6d9a803e217b3a73d708e2d1bd8 Mon Sep 17 00:00:00 2001 From: youyo <1003ni2@gmail.com> Date: Tue, 8 Feb 2022 11:52:51 +0900 Subject: [PATCH] Add option: --port-forward-only --- _awssh | 1 + awssh/cmd/root.go | 1 + cli.go | 16 ++++++++++++---- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/_awssh b/_awssh index 6785640..cb70e2f 100644 --- a/_awssh +++ b/_awssh @@ -12,5 +12,6 @@ _awssh() { '(-i --identity-file)'{-i,--identity-file}'[identity file path.]' \ '--profile[use a specific profile from your credential file.]' \ '(-P --publickey)'{-P,--publickey}'[public key file path.]' \ + '(-f --port-forward-only)'{-f,--port-forward-only}'[Only port-forwarding.]' \ '--select-profile[select a specific profile from your credential file.]' } diff --git a/awssh/cmd/root.go b/awssh/cmd/root.go index 53231b3..517fee7 100644 --- a/awssh/cmd/root.go +++ b/awssh/cmd/root.go @@ -41,6 +41,7 @@ func init() { rootCmd.Flags().Bool("cache", false, "enable cache a credentials.") rootCmd.Flags().String("duration", "1 hour", "cache duration.") rootCmd.Flags().Bool("enable-snapshot", false, "enable snapshot.") + rootCmd.Flags().BoolP("port-forward-only", "f", false, "Only port-forwarding") viper.BindPFlags(rootCmd.Flags()) } diff --git a/cli.go b/cli.go index b2844f8..0b02b82 100644 --- a/cli.go +++ b/cli.go @@ -22,6 +22,7 @@ func Run(cmd *cobra.Command, args []string) (err error) { profile := viper.GetString("profile") cache := viper.GetBool("cache") enableSnapshot := viper.GetBool("enable-snapshot") + portForwardOnly := viper.GetBool("port-forward-only") duration, err := duration.Parse(viper.GetString("duration")) if err != nil { return err @@ -74,7 +75,9 @@ func Run(cmd *cobra.Command, args []string) (err error) { if err != nil { return err } - defer cmdPortForwarding.Process.Kill() + if !portForwardOnly { + defer cmdPortForwarding.Process.Kill() + } if err = sendSSHPublicKey(ctx, awsSession, instanceID, viper.GetString("username"), viper.GetString("publicKey")); err != nil { return err @@ -82,10 +85,15 @@ func Run(cmd *cobra.Command, args []string) (err error) { time.Sleep(1 * time.Second) - cmdSsh, err := execSshCommand(ctx, viper.GetString("username"), ConnectHost, localPort, viper.GetString("identity-file")) - cmdSsh.Wait() + if portForwardOnly { + fmt.Printf("Host: %v\nPort: %v\nIdentityFile: %v\n", ConnectHost, localPort, viper.GetString("identity-file")) + } else { + cmdSsh, err := execSshCommand(ctx, viper.GetString("username"), ConnectHost, localPort, viper.GetString("identity-file")) + cmdSsh.Wait() + return err + } - return err + return nil } func Validate(cmd *cobra.Command, args []string) (err error) {